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?
    1. y = 0;
    2. y = 1;
    3. y = 2;
    4. y = 3;
    5. 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?
    1. LetterGrade = 'a';
    2. LetterGrade = 'A';
    3. LetterGrade = 'b';
    4. LetterGrade = 'C';
    5. 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?
    1. What = 'c';
    2. What = 'C';
    3. What = 'p';
    4. What = 'P';
    5. What = 'x';
    6. What = 'X';
    7. What = 'z';
    8. What = 'Z';
    9. 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?
    1. x = 4; y = -2;
    2. x = 3; y = 6;
    3. x = 1; y = -4;
    4. x = 7; y = -2;
    5. x = 2; y = 5;


PART II

F. Rewrite the Calculator program using the switch statement.



G. Write a switch statement that does the following: