switch Java lab 8 CS110 Lab # 8 switch statement PART I A. Consider the following switch statement: int x = 2; switch (y) { case 0 : System.out.print('1'); break; case 1 : System.out.print(x); break; case 2 : System.out.print( x * x); break; case 3 : System.out.print( x * x * x ); break; case 4 : System.out.print( x * x * x * x); break; default : System.out.print("No match exists."); } What will be displayed for each of the following values of y? y = 0; y = 1; y = 2; y = 3; y = 4; B. Consider the following switch statement: char LetterGrade; switch (LetterGrade) { case 'a' : case 'A' : System.out.print( "Excellent"); break; case 'b' : case 'B' : System.out.print("Superior"); break; case 'C' : case 'c' : System.out.print("Average"); break; case 'd' : case 'D' : System.out.print(" Poor"); break; case 'f' : case 'F' : System.out.print( " Try again"); break; default : System.out.print("This is not a recognized letter grade."); } What will be displayed for each of the following values of LetterGrade? LetterGrade = 'a'; LetterGrade = 'A'; LetterGrade = 'b'; LetterGrade = 'C'; LetterGrade = 'Z'; C. Consider the following switch statement: char What; switch (What) { case 'c' : case 'C' : System.out.print(" Bobo "); case 'y' : case 'P' : System.out.print(" Is a Dog "); break; case 'x' : case 'X' : System.out.print(" Try But "); case 'z' : case 'Z' : System.out.print(" Cannot"); default : System.out.print(" Help You."); } What will be displayed by the code for each of the following values of LetterGrade? What = 'c'; What = 'C'; What = 'p'; What = 'P'; What = 'x'; What = 'X'; What = 'z'; What = 'Z'; What = 'y'; D. Convert the following if statement to a single switch statement. Don't forget to employ the default option. int Year; if (Year == 1) System.out.print(" Freshman "); else if (Year == 2) System.out.print(" Sophomore "); else if (Year == 3) System.out.print(" Junior "); else if (Year == 4) System.out.print(" Senior "); else System.out.print(" Graduate "); E. Consider the following nested switch statement: int x, y; switch (x) { case 2 : case 4 : case 6 : switch (y) { case 1: case 2: case 3: x = x + y; break; case -1: case -2: case -3: x = x - y; break; } break; case 1 : case 3: case 5 : switch (y) { case 2: case 4: case 6: x = x * y; break; case -1: case -4: case -6: x = y * y; break; } break; } System.out.print("x = " + x ); System.out.print("y = " + y); What will be displayed by the code for each of the following values of LetterGrade? x = 4; y = -2; x = 3; y = 6; x = 1; y = -4; x = 7; y = -2; x = 2; y = 5; PART II F. Rewrite the Calculator program using the switch statement. Prompt the user for two intergers a and b and an operator ( '*', '-'. '+', or '/'). Compute and display a+b, a-b, a*b, or a/b according to the operator. G. Write a switch statement that does the following: Increases balance by adding amount to it if the value of the character variable transCode is 'D'. Decreases balance by subtracting amount from it if the value of transCode is 'W'. Display the value of balance if the value of transCode is 'P'. Display "Illegal transaction" otherwise.