Anda di halaman 1dari 7

Array and String

[Question 1 - Array Year]


Write a program to read 3 student records. Each records contain an id and year
of birth as shown in the sample input. Then print the same record but in reverse
order using table format as shown in the sample output.
Sample input:
1
1988
2
2001
3
2000
Sample output:
ID: 2 (2001)
ID: 3 (2000)
ID: 1 (1998)
import java.util.Scanner;
public class syukriah {
public static void main (String args[]){
int id[] = new int[3];
int year[] = new int[3];
int i,j,temp;
Scanner in = new Scanner(System.in);
for(i=0;i<3;i++)

//System.out.print("ID"+i+"=");
id[i] = in.nextInt();
year[i] = in.nextInt();

}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(year[i]>year[j])
{
temp = year[i];
year[i] = year[j];
year[j] = temp;
temp = id[i];
id[i] = id[j];
id[j] = temp;
}

for(i=0;i<3;i++)
System.out.println("ID: "+id[i]+" ("+year[i]+")");

[Question - String of AIU]


Write a program to read a string. Then your program should count how many
letter of A, I and U in the string. Your output must follow the sequence of the
letters as shown in sample output.
Sample input:
TAWAKKAL ALA KULLI HAALIN
Sample output:
A=7
I=2
U=1

import java.util.Scanner;
public class syukriah
{
public static void main (String args[])
{
String text;
int countA=0,countI=0,countU=0,i,length;
Scanner in=new Scanner(System.in);
text=in.nextLine();
length=text.length();
for(i=0;i<length;i++)
{
if(text.charAt(i)=='A')
countA++;
else if (text.charAt(i)=='I')
countI++;
else if(text.charAt(i)=='U')
countU++;
}
System.out.println("A="+countA);
System.out.println("I="+countI);
System.out.println("U="+countU);
}

[Question - Terbalik la pulok]


Write a program that will receive numbers. Your first number will determine how
many numbers to be entered after that. After your system received the last
number, print all the numbers in reverse. The total number is limited to 10 only.
Sample Input:
5
3
55
23
56
43
Sample Output:
43
56
23
55
3

import java.util.Scanner;
public class syukriah
{
public static void main (String args[])
{
int count;
int number[] = new int[10];
int i,j;

Scanner in = new Scanner(System.in);

count = in.nextInt();
for (i=1; i<=count; i++)
number[i] = in.nextInt();

}
{
}

for (j=count; j>=1; j--)


System.out.println(number[j]);

[Question - Tatasusunan]
Write a program that will receive numbers and output the biggest number
followed by all the numbers that have been entered. The first input will
determine how many numbers to be entered and sum.
Sample Input:
4
20
23
30
2
Sample Output:
BIGGEST:30
20
23
30
2

import java.util.Scanner;
public class syukriah
{
public static void main (String args[])
{
int bil,i,j;
int biggest=-32767;
int num[] = new int[10];

Scanner in = new Scanner(System.in);


bil = in.nextInt();
for (i=1; i<=bil; i++)
{
num[i]= in.nextInt();
if(num[i]>biggest)
biggest=num[i];
}

}
}

System.out.println("BIGGEST:"+biggest);
for (j=1; j<=bil; j++)
System.out.println(num[j]);

[Question - Hangman Question]


Write a program that will read string. Then your program should show all the
string character using * except for character 2, output its real character.
sample input:
Apology
sample output:
*p*****

[Notes]Refer to lab activity, on print string in reverse. Use charAt() and string
length looping.

import java.util.Scanner;
public class syukriah
{
public static void main (String args[])
{
String text;
int i,length;
Scanner in=new Scanner(System.in);
text=in.nextLine();
length=text.length();

}
}

for(i=0;i<length;i++)
{
if(i!=1)
text=text.replace(text.charAt(i),'*');
}
System.out.println(text);

[Question - How many letters?]


Write a program that will ask user to enter a string and TWO letter as the input.
Then your program must count and print how many times the input letter exists
in the entered string.
Sample input:
putihputihmelatimerahmerahdelima
a
s
Sample output:
a=4
s=0

import java.util.Scanner;
public class syukriah
{
public static void main (String args[])
{
String text;
int i,length,count1=0,count2=0;
char letter1,letter2;
Scanner in=new Scanner(System.in);
//System.out.println("Enter the sentence =");
text=in.nextLine();
//System.out.print("First Letter=");
letter1=in.next().charAt(0);
//System.out.print("Second Letter=");
letter2=in.next().charAt(0);
length=text.length();
for(i=0;i<length;i++)
{
if(text.charAt(i)==letter1)
count1++;
if(text.charAt(i)==letter2)
count2++;
}
System.out.println(letter1+" = "+count1);
System.out.println(letter2+" = "+count2);

[Question - First Name Last Name]


Write a program that will read TWO input strings of name. First input is the first
name, and the second input is the last name. Output your program by printing
the first letter from the second input followed by dot, a space and the first
name.
Sample input:
Suhailan Safei
Sample output:
S. Suhailan

import java.util.Scanner;
public class syukriah
{
public static void main (String args[])
{
String FirstName;
String LastName;
Scanner in = new Scanner(System.in);
//System.out.println("Your First Name= ");
FirstName = in.next();
//System.out.println("Your Last Name= ");
LastName = in.next();
}
}

System.out.println(LastName.charAt(0) + ". " + FirstName);

Anda mungkin juga menyukai