Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Intro to Java 5. Convert the following as directed: a. // switch to if else 2 sw

ID: 3908728 • Letter: I

Question

Intro to Java

5.    Convert the following as directed:

a. // switch to if else 2

switch (ch)

{

case 1: System.out.println(”Yes”);

          break;

case 2: System.out.println(”No”);

               break;

default: System.out.println(”Wrong choice”);

}

b. // do…while to while 2

char ans;

int num;

do

{

System.out.println(”Enter a number”);

num=input.nextInt();

System.out.println(”The square of the number is : “num*num<<endl;

System.out.println(”Do you want to continue?”;

ans=input.nextChar();

} while (ans == ‘Y’);

6.    What is the output if the following program segments : 2

     int x=9, y=5;

int c = (float) x/y;

System.out.println(c);

7.    Evaluate the following conditional expression as true or false for the give values of a, b, c and d:

(( a >= b) && (b < d)) && ((c < d + 4) || (a < c))   

a)     a = 6, b = 2, c = 5, d = 3 2

b)    a = 3, b = 7, c = 4, d = 5

Explanation / Answer

Answers for all of your questions are given below. Answers are highlighted in bold text. Please check. Thanks.

5.    (Convert the following as directed):

a. // switch to if else 2

switch (ch)

{

case 1: System.out.println(”Yes”);

          break;

case 2: System.out.println(”No”);

               break;

default: System.out.println(”Wrong choice”);

}

Answer:

if (ch == 1) {

                System.out.println("Yes");

} else if (ch == 2) {

                System.out.println("No");

} else {

                System.out.println("Wrong choice");

}

b. // do…while to while 2

char ans;

int num;

do

{

System.out.println(”Enter a number”);

num=input.nextInt();

System.out.println(”The square of the number is : “num*num<<endl;

System.out.println(”Do you want to continue?”;

ans=input.nextChar();

} while (ans == ‘Y’);

Answer:

/**

* setting initial value of ans as Y, so the loop will execute atleast

* once just like do while loop

*

*/

char ans = 'Y';

int num;

while (ans == 'Y') {

                System.out.println("Enter a number");

                num = input.nextInt();

                System.out.println("The square of the number is : " + num * num);

                System.out.println("Do you want to continue? ");

                ans = input.next().charAt(0);

}

6.    What is the output if the following program segments : 2

     int x=9, y=5;

int c = (float) x/y;

System.out.println(c);

Answer:

This will display a syntax error as float cannot be converted to an int value. If the datatype of c was float, then it would display 1.8 (integer to float type casting), if there was no (float) casting, then it would show 1 as the result of integer division between 9 and 5.

7.    Evaluate the following conditional expression as true or false for the give values of a, b, c and d:

(( a >= b) && (b < d)) && ((c < d + 4) || (a < c))  

a)     a = 6, b = 2, c = 5, d = 3

This will return true.

Explanation:

Substituting the values, we get the expression as

(( 6 >= 2) && (2 < 3)) && ((5 < 3 + 4) || (6 < 5))

which implies

(( true) && (true)) && ((5 < 7) || (false))

which implies

(( true) && (true)) && ((true) || (false))

which is

true

b)    a = 3, b = 7, c = 4, d = 5

This will return false.

Explanation:

Substituting the values, we get the expression as

(( 3 >= 7) && (7 < 5)) && ((4 < 5 + 4) || (3 < 4))

which implies

(( true) && (false)) && ((4 < 9) || (true))

which implies

(( true) && (false)) && ((false) || (true))

which is

false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote