Java Course Answers

Exercise A

public class Main {

public static void main(String[] args) {

String breadth = JOptionPane.showInputDialog("Rectangle Breadth");
String height = JOptionPane.showInputDialog("Rectangle Height");

int area = Integer.parseInt(breadth) * Integer.parseInt(height);

JOptionPane.showMessageDialog(null, "Area=" + area,"", JOptionPane.WARNING_MESSAGE);

System.exit(0);

}

}


Exercise B

String breadth = JOptionPane.showInputDialog("Rectangle Breadth");
String height = JOptionPane.showInputDialog("Rectangle Breadth");

float area = Float.parseFloat(breadth) * Float.parseFloat(height);

JOptionPane.showMessageDialog(null, "Area=" + area,"", JOptionPane.WARNING_MESSAGE);

System.exit(0);


Exercise C

Scanner user_input = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = user_input.nextInt();

switch ( age ) {

case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:

System.out.println( age + " is between 0 and 10");
break;

case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20:

System.out.println(age + " is between 11 and 20");
break;

case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30:

System.out.println(age + "is between 21 and 30");
break;

default:

System.out.println("You are over 30");

}


Exercise D

Scanner user_input = new Scanner(System.in);
System.out.println("Choose a colour: Black, White, Red, Blue ");
String colour = user_input.next();

if (colour.equals("Black")) {

System.out.println("You must be a Goth!");

}

else if (colour.equals("White")) {

System.out.println("You are a very pure person!");

}

else if (colour.equals("Red")) {

System.out.println("You are fun and outgoing ");

}

else if (colour.equals("Blue")) {

System.out.println("You’re not a Chelsea fan, are you?");

}

else {

System.out.println("Sorry, didn't catch that!");

}


Exercise E

int loopVal;
int end_value = 11;
int addition = 0;
int times_table = 0;

Scanner user_input = new Scanner(System.in);
System.out.println("Which times table do you want?");
times_table = user_input.nextInt();

for (loopVal = 1; loopVal < end_value; loopVal++) {

addition = loopVal * times_table;

System.out.println(loopVal + " times " + times_table + " = " + addition);

}


Exercise F

int loopVal;
int end_value = 11;
int oddNum = 0;

for (loopVal = 1; loopVal < end_value; loopVal++) {

oddNum = loopVal %2;

if (oddNum == 1) {

System.out.println("odd number = " + loopVal);

}

}


Exercise G

int[ ] aryNums = { 24, 6, 47, 35, 2, 14 };

int i;
int arrayTotal = 0;
int average = 0;

for (i=0; i < aryNums.length; i++) {

arrayTotal = arrayTotal + aryNums[ i ];

}

average = arrayTotal / aryNums.length;
System.out.println("total: " + average);


Exercise H

int[ ] aryNums = { 24, 6, 47, 35, 2, 14 };

Arrays.sort(aryNums);
int lastArrayNumber = aryNums.length - 1;
System.out.println("Highest Number: " + aryNums[lastArrayNumber]);


Exercise I

int[ ] aryNums = { 24, 6, 47, 35, 2, 14 };
int i;
int oddNum = 0;

for (i=0; i < aryNums.length; i++) {

oddNum = aryNums[ i ] %2;

if (oddNum == 1) {

System.out.println("odd number = " + aryNums[i]);

}

}


Exercise J

int[ ][ ] aryNumbers = new int[6][5];

aryNumbers[0][0] = 10;
aryNumbers[0][1] = 12;
aryNumbers[0][2] = 43;
aryNumbers[0][3] = 11;
aryNumbers[0][4] = 22;

aryNumbers[1][0] = 20;
aryNumbers[1][1] = 45;
aryNumbers[1][2] = 56;
aryNumbers[1][3] = 1;
aryNumbers[1][4] = 33;

aryNumbers[2][0] = 30;
aryNumbers[2][1] = 67;
aryNumbers[2][2] = 32;
aryNumbers[2][3] = 14;
aryNumbers[2][4] = 44;

aryNumbers[3][0] = 40;
aryNumbers[3][1] = 12;
aryNumbers[3][2] = 87;
aryNumbers[3][3] = 14;
aryNumbers[3][4] = 55;

aryNumbers[4][0] = 50;
aryNumbers[4][1] = 86;
aryNumbers[4][2] = 66;
aryNumbers[4][3] = 13;
aryNumbers[4][4] = 66;

aryNumbers[5][0] = 60;
aryNumbers[5][1] = 53;
aryNumbers[5][2] = 44;
aryNumbers[5][3] = 12;
aryNumbers[5][4] = 11;

int rows = 6;
int columns = 5;

int i, j;

for (i=0; i < rows ; i++) {

for (j=0; j < columns ; j++) {

System.out.print( aryNumbers[ i ][ j ] + " " );

}

System.out.println( "" );

}