Assignment: Interfaces

As usual, submit a zipped folder of your .java files.

  1. Complete problem 13.6 from the textbook:
  2. Redo the class Person in Display 5.19 so that it implements the Cloneable interface. This may require that you also redo the class Date so it implements the Cloneable interface. Also, do a suitable test program.

    Here is boilerplate for PersonDemo and Date. You will notice that PersonDemo.java contains the definitions of two classes, Person and PersonDemo. This is legal, and not uncommon. A file must contain exactly one public class, and the name of that class must be the same as the file. It may, in addition, contain non-public classes. Here, because PersonDemo is the public class, the name of the file must be PersonDemo.java.

    Hint: If you are getting "not visible" errors from the compiler, consider that the clone() method of YourCloneableClass in Display 13.7 is public. How about in your Date class?..... [What's going on is that Object's clone() method is declared to be protected, so if we rely on using the implicitly inherited clone(), we can be stung by its being protected.]

    Remember to consider the possibility that a Date field might be null. If that's the case, you wouldn't be able to invoke clone() on it. Make sure that your code will work correctly if a Person contains a null Date field.

    Your driver class (the "test code" problem 13.6 refers to) should be called TestClone. It should demonstrate that your clone method is making a deep copy by applying setDate to the birth date of a Person, and then showing that the birth date of the Person's clone has not changed.

  3. Complete problem 13.10 from the textbook. Place the "test code" (see below) in a public class called TestArea.
  4. Define an interface named Shape with a single method named area that calculates the area of the geometric shape: public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, accessor/ mutator methods for the radius, and an implementation of the area method. Also define a class named Rectangle that implements Shape. The Rectangle class should have instance variables for the height and width, a constructor that sets the height and width, accessor and mutator methods for the height and width, and an implementation of the area method. The following test code should then output the area of the Circle and Rectangle objects:

    public static void main(String[] args) { 
      Circle c = new Circle(4); // Radius of 4 
      Rectangle r = new Rectangle(4,3); // Height = 4, Width = 3 
      ShowArea(c); 
      ShowArea(r); 
    } 
    public static void ShowArea(Shape s) { 
      double area = s.area(); 
      System.out.println("The area of the shape is " + area); 
    }