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:

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
* disadvantage: not as readable as if
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:

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


Solution:

PrintWriter outFile = new PrintWriter(new FileWriter("mouse.dat"));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String firstName="";
String lastName;
String name;
String letter;
while (!firstName.equals("Mouse Day")){
  System.out.println("Please enter your first name");
  firstName = reader.readLine();
  System.out.println("Please enter your last name");
  lastName = reader.readLine();
  System.out.println("Please enter a letter describing "+
    "your favorite topping");
  letter = reader.readLine();
  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:

BufferedReader inFile = new BufferedReader(new FileReader("pizza.dat"));
String[] firstName = new String[100];
String[] lastName = new String[100];
char[] letter = new char[100];
String line;
int i = 0;
line = inFile.readLine();
while (line != null){
  firstName[i] = line;
  lastName[i] = inFile.readLine();
  letter[i] = inFile.readline().charAt(0);
  line = inFile.readline();
  i++;
}
inFile(close);
5. After the arrays are filled, use the for loop to compute the following three numbers:


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);