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.

Solution:

public class HusbandOfCleopatra{
  protected String name;
  protected int start;
  protected int end;
  public HusbandOfCleopatra(String aname, int astart, int aend)
    {name = aname; start = astart; end = aend;}
  public String getName(){return name;}
  public int getStart(){return start;}
  public int getEnd(){return end;}
  public void setName(String aname){name = aname;}
  public void setStart(int astart){start = astart;}
  public void setEnd(int aend){end = aend;}
  public int duration(){return start - end;}
  public String toString(){return "Cleopatra was married to " + name +
    " from " + start + " B.C.E. to " + end + " B.C.E.";}
}

public static void main(String[] args){
  HusbandOfCleopatra ceasar = new HusbandOfCleopatra("Julius Ceasar",
    48, 44);
  int time = ceasar.duration();
  Systems.out.println(time);
}

Tracing: first, we call a constructor method:

 -----------------
|  -------------  |
| |Julius Ceasar| |
|  -------------  |
|      aname      |
|  ------------   |
| |    48      |  |
|  ------------   |
|    astart       |
|  ------------   |
| |     44     |  |
|  ------------   |
|     aend        |
|-----------------|
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
 -----------------
 HusbandOfCleopatra

this method generates a new object

 -----------------
|  -------------  |
| |Julius Ceasar| |
|  -------------  |
|       name      |
|  ------------   |
| |    48      |  |
|  ------------   |
|     start       |
|  ------------   |
| |     44     |  |
|  ------------   |
|      end        |
 -----------------
      ceasar

after applying the method duration to this object, we get

 ----------
|    4     |
 ----------
   time

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.

Solution: public class HusbandOfCleopatra2 extends HusbandOfCleopatra{ private String position; public HusbandOfCleopatra2(String aname, int astart, int aend, String aposition){super(aname, astart, aend); position = aposition;} public String getPosition(){return position;} public void setPosition(String aposition){position = aposition;} } HusbandOfCleopatra2 ceasar = new HusbandOfCleopatra2("Julius Ceasar", 48, 44, "Roman Emperor"); 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.

Solution:

public double shortest(HusbandOfCleopatra[] input){
  double shortestSoFar = input[0].duration();
  for (int i = 1; i < input.length; i++){
    {if (input[i].duration() < shortestSoFar)
      {shortestSoFar = input[i].duration();}
    }
  return shortestSoFar;
}

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.


Scanner read = new Scanner(new FileReader("durations"));
int duration;
int total = 0;
int number = 0;
while(read.hasNext()){
  duration = read.nextInt{};
  total += duration;
  number++;
}
try{
  int average = total / number;
  }
catch(ArithmeticException e){
  System.out.println("The file was empty, so we could not compute "+
    "the average.");
  }