Fall 2013, The College of Saint Rose

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

Agenda

• Announcements
• Final exam next week: 1:30 PM on Thursday in our regular classroom.
• same ground rules as first two exams: closed book, closed notes, closed neighbors
• practice questions out
• we can review some on Thursday, again the night before the exam
• Lecture 22 assignment recap
• The switch statement
• In-class Exercise 23-1 - (15 lecture assignment points) due before the end of class.

We will work together to write a program LongShortWords.java to read in the words from a file, and write them to two files: one that contains all words at most 4 characters long, and another that contains all words longer than 4 characters, printed out along with their lengths.

Please submit only the file LongShortWords.java in Submission Box under "InClass23-1" before you leave class.

• In-class Exercise 23-2 - (10 lecture assignment points) due before the end of class.

We will then practice writing a method by modifying the MonteCarloPi example. We will make this modification:

Create a method called estimatePi that takes a parameter of the number of points to use for the approximation as its parameter, and which performs the random point generation algorithm and reports its approximation of pi. Then modify the main method so it calls this new method at an appropriate time.

Your new version of the program should have a main method that reads in the number of points from the Scanner, then calls your new estimatePi method. Note that you will need to decide which variables are ones needed by main and which are needed by estimatePi. Information that exists in main which is needed by estimatePi should be passed as parameters (in this case, only the number of points).

Please submit only the file MonteCarloPi.java in Submission Box under "InClass23-2" before you leave class.

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

// 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