Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

Lecture 23: The switch Statement; More File I/O
Date: Tuesday, December 3, 2013


Agenda

Lecture 23 Assignment

Due at the start of class, Thursday, December 5.

Please submit answers to these questions in Submission Box under "LA23" by the start of our next class. We will discuss these questions at the start of class, so no late submissions are accepted. Please be sure that your name is clearly indicated in all submissions.

  1. Write a Java switch statement that sets the variable answer below to 17 when another variable input is equal to 8, 23, 31, or 40, to 13 when input is equal to 1, 3, or 5, and to 11 for all other values of input. (5 points)
      int input;
      // input gets a value here somehow, we don't care how
      int answer;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
      // answer should have the value as described above when we get here
    
  2. Convert the following Java conditional to use a switch statement that accomplishes the exact same thing. (5 points)
    if ((year == 2008) || (year == 2010)) {
        place = 1;
    }
    else if ((year == 2009) || (year == 2011)) {
        place = 2;
    }
    else if (year == 2012) {
        place = 3;
    }
    else if (year == 2004) {
        place = 4;
    }
    else {
        place = 5;
    }
    

Examples