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, it 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.

2. Write a piece of code that does the same as in Problem 1 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 ?:

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.

4. Write 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).

5. After the arrays are filled, use the for loop to compute the following three numbers:

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.