Homework 2

You submit a draft on Wednesday and a final version on Saturday.

A. Add toString and equals methods to the Point class of homework 0 and the following subclass:

public class LabeledPoint extends Point
{
   private String label;
   public LabeledPoint(double x, double y, String label)
   { 
      super(x, y);
      this.label = label;
   }

   // Add toString and equals
}

The toString method of Point produces a string such as Point[x=3.0,y=4.0] (no spaces), and LabeledPoint produces a string such as LabeledPoint[x=3.0,y=4.0][label=Fred].

Two points are equal if their x- and y-coordinates are equal. Two labeled points are equal if they are equal as points and their labels are equal. A point is never equal to a labeled point.

Read through Special Topic 10.4 and 10.5 for guidance.

Visit this link and paste in your classes. Submit Point.java and LabeledPoint.java. Here is a tester.

The draft should compile and pass the test for points.

B. Provide a subclass CountingArrayList of ArrayList<String> that counts how often the get and set methods are called. For example,

CountingArrayList myList = new CountingArrayList();
myList.add("Fred");
myList.add("Wilma");
String x = myList.get(0);
System.out.println(myList.count("get")); // Prints 1
System.out.println(myList.count("set")); // Prints 0

Visit this link and paste in your classe. Submit CountingArrayList.java. Here is a tester.

The draft should compile with the tester.

C. Define an abstract class CountingComparator that implements Comparator with an instance variable counter. In the compare method, increment the counter and call doCompare.

Here, int doCompare(Object o1, Object o2) is an abstract method that you declare but don't define.

Supply a getCounter method that yields the counter.

Visit this link and paste in your class. Submit CountingComparator.java. Here is a tester.

The draft should compile with the tester.