CS 1401, Exam #4

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

Tomorrow is the Earth Day, the day when people all over the Earth join in activities that help us preserve the environment. The students of an unnamed department of an unnamed university decided to join by taking care of the cactuses and other plants in front of the departmental building and on the nearby hill.

1-2. Volunteer students are asked what activity they want to join on the Earth Day. Depending on their answers, they are given labels: F corresponds to plants in Front of the building, H for plants on the Hill, and B stands for Both.

1. Use switch to write down a code that, based on the letter, prints out the appropriate task:

Do not forget to print an error message if the letter does not correspond to any of these options.

2. Write the 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 ?:

3-5. As students register for the Earth Day activities, each student types in his or her name and the corresponding letter (F, H, or B). In the resulting file:

3. Write a code for filling in the file; use "that's all folks" as the sentinel.

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

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

6-7. In addition to taking care of the plants, the local chapter for the Sierra Club also decided to collect donations to take care of the plants in the Amazon River Rainforest. The information about the donations is stored in the array amazon[], so that amazon[0] is the amount donated by the first person, amazon[1] is the amount donated by the second person, etc. Let members be the total number of persons who donates money for this cause. 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 folks donate: a professor donates 100 dollars and a student donates 1 dollar.