CS 1401, Exam #4, 11:30 am version

Date: Friday, November 30, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________

On this same day in the year 30 B.C.E., the famous Cleopatra died. Her husbands included pharaohs, Julius Caesar (from 48 to 44), Marc Anthony (NOT the modern singer :-), and many others. Let us help historians process the data about her husbands.

1-3. Let us start helping the historians.

  1. Define a class HusbandOfCleopatra in which each object has three parameters: name, the year when the marriage started, and the year when it ended. Include a constructor method, accessor methods, mutator methods, a method which computes the duration of the marriage, and an appropriate toString method.

  2. In the main method, define Julius Ceasar as a new object of this class, and compute and print the period of time during which he was married to Cleopatra (should be 4 years).

  3. Trace your code from Problem 2 step-by-step.

4. When we defined our class in Problems 1-3, we did not include an important field: the social position of the husband. Define a subclass of the class HusbandOfCleopatra which contains this new field. In the main method, define a new object corresponding to Julius Ceasar.

5. Write a method that, given an array of objects of type HusbandOfCleopatra (defined in Problems 1-3), computes the shortest duration of Cleopatra's marriages.

6. Suppose that the historians already have duration of different Cleopatra's marriages in a file durations. These durations are stored line by line:

Write a code that reads the values line by line, adds them up, computes the number of lines, and then computes the average. Use try-catch block to produce the meaningful error message when we are trying to divide by 0.