Skip to content
October 7, 2013 / TDS Software

Java Q&A

Write a Java program to extract and display the user id when an e-mail address is inserted as
a keyboard input to a program. Save your answer as “Question09.java”.
Eg:
Email : abc_123@gmail.com
User Id : abc_123
Note : You may use substring() and indexOf() methods in String class and methods in
Scanner class.

 

import java.io.*;

class Question09{
public static void main(String[] args) {
String em =””;
try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
System.out.print(“Enter Email : “);
em = bf.readLine();

}catch (IOException e){
e.printStackTrace();
}

int start = em.indexOf (“@”);
if (start==-1){
System.out.println(“Error : Please enter valid email”);
}else{
System.out.println(“User ID : ” + em.substring(0,start));
}
}
}

Advertisements
October 7, 2013 / TDS Software

Java Q&A

Write a Java program to add two 2×3 matrices as given in the following example.
matrix 1 matrix 2 result
In your program you MUST declare 3 Two dimensional arrays to store matrix 1, matrix 2 and
the result respectively. Use a nested for loop to add matrix 1 and matrix 2 and to store the
result. Then display the result as given below. Save your answer as “Question08.java”.
Matrix 1 + Matrix 2 = Result
| 0 1 2 |      | 6 5 4 |         | 6   6    6   |
| 9 8 7 | +   | 4 3 5 |  =     | 12 12 12 |

 

class Question08{
public static void main(String[] args) {
int matrix1[][] = {{0,1,2},{9,8,7}};
int matrix2[][] = {{6,5,4},{3,4,5}};
int matrix3[][] = new int[2][3];
for (int x = 0 ; x < matrix1.length ; x++){
for (int y = 0 ; y < matrix1[x].length ; y++){
matrix3[x][y]= matrix1[x][y] + matrix2[x][y];
}
}

for (int x = 0 ; x < matrix1.length ; x++){
String m1 =”” ,m2 = “” ,m3 =””;
for (int y = 0 ; y < matrix1[x].length ; y++){
m1 += matrix1[x][y] + ” “;
m2 += matrix2[x][y] + ” “;
m3 += matrix3[x][y] + ” “;
}
System.out.println (“| ” + m1 + “| | ” + m2 + “| | ” + m3 );
}

}
}

October 7, 2013 / TDS Software

Java Q&A

Write a Java program to get the gpa of a student as a keyboard input. Then display the class
of the degree according to the following criteria using a switch-case statement. If the user
insert an invalid gpa you should display a message “Invalid GPA”.

 

gpa ≥ 3.50 “First Class Hons”
3.49 ≥ gpa ≥ 3.00 “Upper Second Class Hons”
2.99 ≥ gpa ≥ 2.50 “Lower Second Class Hons”
2.49 ≥ gpa ≥ 2.00 “Pass”
2.00 ≥ gpa “Fail”

 

import java.io.*;
class Question07{
public static void main(String[] args) {
double gpa=0.0;
try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
System.out.print(“Enter gpa : “);
gpa = Double.parseDouble(bf.readLine());

}catch (IOException e){
e.printStackTrace();
}

double num = (double) Math.ceil (gpa/0.5)* 0.5 ;
int i = (int)(num * 10 );
String out;
switch(i){
case 40:
out = “First Class Hons”;
break;
case 35:
out = “Upper Second Class Hons”;
break;
case 30:
out = “Lower Second Class Hons”;
break;
case 25:
out = “Pass”;
break;
case 20:
case 15:
case 10:
case 5:
out = “Fail”;
break;
default:
out = “Invalid GPA”;
break;
}
System.out.println(out);

}
}

October 7, 2013 / TDS Software

Java Programming Q&A

Write a java program to obtain the following shape. You must use for loops in Java to get
the pattern.

 

999999999999999999
88888888   88888888
7777777      7777777
55555               55555
4444                  4444
333                      333
22                         22
1                             1

 

class Question06{
public static void main(String[] args) {
for (int i = 9 ; i > 0 ; i–){
for (int x = 0 ; x <i; x++ ){
System.out.print(i);
}
for (int x = 0 ; x <9-i; x++ ){
System.out.print(” “);
}
for (int x = 0 ; x <9-i; x++ ){
System.out.print(” “);
}
for (int x = 0 ; x <i; x++ ){
System.out.print(i);
}
System.out.print(“\n”);
}
}

}

October 7, 2013 / TDS Software

Java Q&A

Write a Java program to get the gpa of a student as a command line argument. Then display
the class of the degree according to the following criteria. 
gpa ≥ 3.6 “First Class Hons”
3.6 > gpa ≥ 3.4 “Upper Second Class Hons”
3.4 > gpa ≥ 3.00 “Lower Second Class Hons”
3.00> gpa ≥ 2.00 “Pass”

 

class Question05{
public static void main(String[] args) {
double gpa = Double.parseDouble (args[0]);
if(gpa >= 3.6){
System.out.println(“First Class Hons”);
}else if(gpa >= 3.4){
System.out.println(“Upper Second Class Hons”);
}else if(gpa >= 3.0){
System.out.println(“Lower Seconds Class Hons”);
}else if(gpa >= 2.0){
System.out.println(“Pass”);
}
}
}

October 7, 2013 / TDS Software

Java Q&A

Write a java program to obtain the following shape. You must use while loops in Java to
generated the output. 
               &&&&&&&&&&&&&&
           &&&&&&&&&&&&&
       &&&&&&&&&&&&
   &&&&&&&&&
 &&&&&&&&&&
&&&&&&&&&&

 

class Question04{
public static void main(String[] args) {
int y = 0;
while (y<6){
int x = 0;
int z = 10-y;
while(x<10-y){
if (x < z -5 )
System.out.print(” “);
else
System.out.print(“& “);
x++;
}
y++;
System.out.print(“\n”);
}
}
}

October 7, 2013 / TDS Software

Java Q&A

Write a program to get the distance between two cities in miles and yards as keyboard
inputs. Then convert the distance in to metric form (kilometers). Display the distance in
kilometers. You must use BufferedReader class to get keyboard inputs.

 

import java.io.*;
class Question03{
public static void main(String[] args) {
final double mileInKm = 1.6;
final double yardsPerMile = 1760;

int miles = 0, yards =0;
try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
System.out.print(“Enter distance in miles : “);
miles = Integer.parseInt (bf.readLine());
System.out.print(“How many yards? : “);
yards = Integer.parseInt (bf.readLine());
}catch (IOException e){
e.printStackTrace();
}
double disKm;
disKm = (miles + (yards/yardsPerMile))* mileInKm;
System.out.println(“Distance in Kilometers : ” + disKm );

}
}