Answers

1

a) What type of references can a Car hold?
b) What type of references can a Boat hold?

(a) A Car can hold a reference to a Car (obviously?), a Ferry (since Ferry extends Car) or a SuperFerry (since SuperFerry has Car as a superclass through Ferry).

(b)A Boat can only hold references to a Boat. We don't have any classes that extend Boat. There are no subclasses of Boat at the moment.

Go back.

2

Suppose we had the following statement:

Vehicle v = new SuperFerry(this);

State which of the following statements would be valid. For those that are invalid state why.

  1. v.setVisible(true);
  2. v.moveRelative(20, 30);
  3. v.setSize(50, 80);

(a) and (c) are valid because they are defined in Vehicle. (b) is invalid because moveRelative was only defined in SuperFerry. You can only use moveRelative if the variable is of type SuperFerry or a subclass (if any exist). All is not lost however. If you really want to use moveRelative you can do it by using casting. Here is code to do it:

SuperFerry sf = (SuperFerry)v

Even though it was stored as a Vehicle all the SuperFerry information still exists. We just have to promote it to a SuperFerry by using a cast. That was done by putting the type in brackets.

Go back.

3

  1. List all the fields available in Bunny. Indicate which class they were defined in.
  2. Ignoring the methods from Object, which methods are available in Bunny. Indicate which class they were defined in or if they were overridden in Bunny.

  1. Fields from Face:
       x, y, diameter, colour, eyeColour, isVisible
    Fields from Bunny:
       earColour, surpriseStatus
  2. Methods from Face:
       setVisible(boolean), move(int, int), setSize(int), setColor(Color),
       setEyeColor(Color), getX(), getY()
    Methods from Bunny:
       hop(int, int), setEarColor(Color), setSurprise(boolean)
    Methods overridden in Bunny:
       draw(), erase()

Go back.

4

Consider a class that has the following form:
public class MyDate
{
   int year, month, day;

   /** Only the method signatures are given **/
   public MyDate() // a method to intialize a date to Jan. 1, 1900
   //set all fields. 1=Jan.,2=Feb.,...
   public void setDate(int yr, int mn, int dy)
   // returns a string in the form July 5, 2001 
   public String toString()
   // allows the entry of a date from the keyboard
   public void enterDate() 
}

Answer the following questions:

  1. Implement the setDate method.
  2. Implement the toString() method.
  3. If you were implementing the enterDate method what consideration would you need to make?
  4. Write a fragment of code that will have a MyDate object called date. Set the date to today's date. Print the date. Read a new date from the keyboard. Print the date.
  5. Suppose you were going to create a new class DateTime that will inherit from MyDate. In addition to storing a date the new class will allow you to store a time in 24 hour format. It will have three additional fields: hour, minute and second. It will override the toString method to produce a string in the format: July 5, 2001 - 4:52:11 p.m..
    1. Give the definition of the class (just give method signatures - don't fully implement)
    2. Would the following be legal? Explain.
      DateTime dt = new MyDate();
  1. public void setDate(int yr, int mn, int dy)
    {
       year = yr;
       month = mn;
       day = dy;
    }
  2. public String toString()
    {
       String[] months={"January","February","March","April","May","June",
                "July","August","September","October","November","December"};
       String s;
       s = months[month-1] + " " + day + ", " + year;
       return s;
    }
    
  3. You would need to ensure that the year, month and date that the user entered was valid before you store it. Otherwise other MyDate methods might get into trouble (for instance toString()).
  4. MyDate date = new MyDate();
    date.setDate(2001, 7, 5);
    System.out.println(date.toString());
    date.enterDate();
    System.out.println(date.toString());
    
    1. public class DateTime extends MyDate
      {
         int hour, minute, second;
      
         public DateTime()
         public void setTime(int hr, int mn, int sc)
         public String toString()
         public void enterTime()
      }
      
    2. This is illegal since DateTime is a subclass of MyDate. It would need to be the other way around. MyDate dt = new DateTime(); would be legal.

Go back.

5

Consider the following class:
public class Sequence
{
   double a;
   public Sequence() {a = 0;}
   public Sequence(double t1) {a = t1;}
   public void setT1(double t1) {a = t1;}
   public double t(int n) {return ((n - 1) + a)* n;}
   public String toString() {return ""+a+", "+t(2)+", "+t(3)+", ...";}
}

Answer the following questions:

  1. Write a Java application that makes the first term in a Sequence be 4. Print the first three terms in the sequence. Find and print the sum of the first 5 terms in the sequence.
  2. Define a new class MySequence that uses a formula of tn = n2 to generate sequences.
  3. Define a new class Series that extends Sequence. It has a method double sum(int n) that finds the sum of the first n terms in the series. It will override toString() to insert "+" instead of "," in the string. It will have a method String sumString(int n) that will make a String of the form: t1 + t2 + t3 + ... + tn = Sn. For example, if we have a sequence as in part (a) then sumString(5) would generate a String of the form: "4.0 + 10.0 + 18.0 + ... + 40.0 = 100.0". If n <= 4 just print the whole series.

  1. public class TestSequence
    {
       public static void main(String args[])
       {
          Sequence s = new Sequence(4);
          System.out.println("The sequence is " + s);
          double sum = 0;
          for (int i = 1; i <= 5; i++)
             sum += s.t(i);
          System.out.println(""+s.t(1)+" + "+s.t(2)+" + "+ s.t(3)+" + "+
                             s.t(4) + " + " + s.t(5) + " = " + sum);
       }
    }
  2. public class MySequence extends Sequence
    {
       // only need this constructor as formula ensures a = 1
       public MySequence() {a = 1;} 
       public double t(int n) {return n*n;}
    }
  3. public class Series extends Sequence
    {
       public Series() {super();}
       public Series(double t1) {super(t1);}
       public double sum(int n)
       {  
          double total = 0;
          for (int i = 1; i <= n; i++)
             total += t(i);    
          return total;
       }
       public String toString() {return ""+a+" + "+t(2)+" + "+t(3)+" + ...";}
       public String sumString(int n)
       {
          String s = "";
          for (int i = 1; i <= n && i < 4 ; i++)
          {
             s = s + t(i);
             if (i != n)
                s = s + " + ";
          }
          if (n == 4)
             s = s + t(4);
          else if (n > 4)
             s = s + " ... + " + t(n);
          s = s + " = " + sum(n);
          return s;
       }
    }

Go back.