## CS 1401, Exam #4

Date: Thursday, April 27, 2006
Class section (9:00-10:20 am or 10:30-11:50 am): ______________________
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Today, on April 27, 2006, is exactly 25 years since the computer mouse has been invented in 1981. To celebrate this anniversary, computer science students decided to order the favorite food of computer scientists: pizza.

1-2. The celebration organizers ask each student which pizza topping they prefer. Depending on their answers, they are given labels: P stands for pepperoni, V for vegetarian, and J for jalapenos.

1. Use switch to write down a piece of code that, based on the letter, prints out the corresponding type of pizza topping:

• for P, pepperoni;
• for V, vegetarian;
• for J, jalapenos.
Do not forget to print an error message if the letter does not correspond to any of these options.

```
Solution:

switch(letter){
case `P': System.out.println("pepperoni"); break;
case `V': System.out.println("vegetarian"); break;
case `J': System.out.println("jalapenos"); break;
default:  Systems.out.println("Invalid entry");
}
```
2. Write a piece of code that does the same but uses if instead of switch. Explain the comparative advantages and disadvantages of using if and switch. For extra credit: explain comparative advantages and disadvantages of using ?:

```
Solution:

if (letter == `P')
System.out.println("pepperoni");
else if (letter == `V')
System.out.println("vegetarian");
else if (letter == `J')
System.out.println("jalapenos");
else
Systems.out.println("Invalid entry");

Switch:
* advantage: it can handle cases when there are more than two options
* disadvantage: it is difficult to use for complex nested conditions
If:
* advantage: can handle complex nested conditions easily
* disadvantage: if we have more than two options, using if is
cumbersome
?:
* advantage: faster to write, takes less space
```
3-5. As students register for the celebration, each student types in his or her name and the corresponding letter (P, V, or J). In the resulting file:
• the 1st line is the first name of the first student,
• the 2nd line is the last name of the first student,
• the 3rd line is the first student's letter;
• the 4th line is the first name of the second student,
• the 5th line is the last name of the second student,
• the 6th line is the second student's letter;
• etc.

3. Write a code for filling in the file; use "Mouse Day" as the sentinel.

```
Solution:

PrintWriter outFile = new PrintWriter(new FileWriter("mouse.dat"));
String firstName="";
String lastName;
String name;
String letter;
while (!firstName.equals("Mouse Day")){
System.out.println("Please enter a letter describing "+
if (!firstName.equals("Mouse Day")){
outFile.println(firstName);
outFile.println(lastName);
outFile.println(letter);
}
}
```
4. Wtite a code that transfers this information from the file into three arrays: array of first names, array of last names (String arrays), and array of letters (character array).

```
Solution:

String[] firstName = new String[100];
String[] lastName = new String[100];
char[] letter = new char[100];
String line;
int i = 0;
while (line != null){
firstName[i] = line;
i++;
}
inFile(close);
```
5. After the arrays are filled, use the for loop to compute the following three numbers:
• the overall number of students who selected pepperoni;
• the overall number of students who selected vegetarian pizza;
• the overall number of students who selected pizza with jalapenos.

```
Solution:

int pepperoni = 0;
int vegetarian = 0;
int jalapenos = 0;
for (i = 0; i < letter.length; i++){
if (letter[i] == `P')
pepperoni++;
else if (letter[i] == `V')
vegetarian++;
else if (letter[i] == `J')
jalapenos++;
}
```
6-7. Local high school kids learned about the mouse celebrations and decided to join. To feed these kids, students decided to collect donations for extra pizza. The information about the donations is stored in the array donations[], so that donations[0] is the amount donated by the first student, donations[1] is the amount donated by the second student, etc. Let students be the total number of students who donates money for the kids' pizza. Write down a method that computes the average donation; use += and for loop. Your code should catch the exception corresponding to division by 0. Trace your method on the example when only two students donate: one student donates 2 bucks, and the second student donates sixty cents.

```
Solution:

public static double average(double[] donations, int students){
try{
double sum = 0.0;
for(int i = 0; i < students; i++)
sum += donations[i];
if(students == 0)
throw new ArithmeticException();
else
return sum/students;
}
catch(ArithmeticException ex){
System.out.println("You entered 0 students, " +
"so we cannot compute the average");
return 0;
}
}

Trace: e.g., call it in the main program with

int[] donations = {2.0,0.60};
double averageDonation = average(donations,2);
```