CS 1401, Exam #4

Date: Tuesday, November 22, 2005
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. An automatic phone system for student registration asks a student to press 1 if he or she wants to register for a CS class, 2 if he or she wants to register for a Math class, and 3 if the student wants to register for a core curriculum class.

Use switch to write down a code that, based on the digit, prints out the corresponding class:

Do not forget to print an error message if the digit is different from 1, 2, or 3.





































2. Write a code that does the same but uses if instead of switch. Explain the comparative advantages and disadvantages of using if, switch, and ?.

























































3. The information about the classes that a student wants to take is stored in a file classes.dat, one class per line. Write a code that, given this file, transfers all its information into an array classes of classes, so that:

















































4. Write a method that, given an array of strings, returns the number of elements that contain the word "computer". Start with number = 0, and, inside a for-loop, add 1 if the current element of the array contains the word "computer". Trace your method on the example of an array consisting of two strings: "Introduction to Computer Science" and "Discrete Mathematics".























































5. Write a method that, given an array of integers, returns the average of all the elements from this array. Trace this method on the example of an array of 2 elements: 0 and 10.