IT 401 Assignment 2 Solution


Question One

Identify and correct the error(s) in each of the following:

1. double X = 12.5;

int Y =X+4;

2. if (x = 1)

y++;

else if (x = 2)

y = y + 2;

3. final double QUARTER_VALUE = 0.25;

QUARTER_VALUE = 0.30;


Question Two

2.1Given the code:

what is the output in each of the following cases?

1. x=5 , y=2 , z=2

2. x=2 , y=0 , z=0

3. x=-1 , y=0 , z=0

2.2 what is the output of the following code :

Question Three

Write complete Java programs, provide a screen snapshot

showing your program in execution to receive full credit.

Write a program that reads a Celsius degree in double from the console, then converts it to Fahrenheit and displays the result.

The formula for the conversion is as follows:

𝐹𝑎ℎ𝑟𝑒𝑛ℎ𝑒𝑖𝑡=(9÷5 )×𝐶𝑒𝑙𝑠𝑖𝑢𝑠+ 32


Typical run of the program:

Enter a Celsius degree: 35.5

35.5 degree Celsius is about 67.5 Fahrenheit.

Question Four

Write the following mathematical expressions in Java.

Question Five

Consider the code:

String s;

Scanner in = new Scanner(System.in);

System.out.print("Enter a word: ");

s = in.next();

5.1 write one statement to test if the user enters the string "Hello".

5.2 assume s = "I love KSA " ;

What is the output the following code ?

String S1= s.substring(4,10);

System.out.println(S1);