Implementing Comparable


The job of the compareTo method in the Comparable interface is to indicate which of two objectsthe message receiver and the message parametershould appear first. The code you will supply in compareTo should result in what is known as the natural, or default, sort order for CourseSessions. In other words, how do you normally want to sort a collection of CourseSession objects?

The compareTo method must return an int value. If this return value is 0, then the two objects are equal for purposes of sorting. If the return value is negative, then the receiver (the object to which code sends the compareTo message) should come before the parameter in the sort order. If the value is positive, then the parameter should come before the receiver in the sort order.

The String class implements the compareTo method in order to sort strings in alphabetical order. The following language test demonstrates the values returned from the compareTo method in the three possible scenarios.

 public void testStringCompareTo() {    assertTrue("A".compareTo("B") < 0);    assertEquals(0, "A".compareTo("A"));    assertTrue("B".compareTo("A") > 0); } 

You will need to modify the CourseSession class to implement the Comparable interface and supply a definition for compareTo. An appropriate test to add to CourseSessionTest:

 public void testComparable()  {    final Date date = new Date();    CourseSession sessionA = CourseSession.create("CMSC", "101", date); 

    CourseSession sessionB = CourseSession.create("ENGL", "101", date);    assertTrue(sessionA.compareTo(sessionB) < 0);    assertTrue(sessionB.compareTo(sessionA) > 0);    CourseSession sessionC = CourseSession.create("CMSC", "101", date);    assertEquals(0, sessionA.compareTo(sessionC)); } 

CourseSession must then declare that it implements the Comparable interface. It must also specify a bind type of CourseSessionyou want to be able to compare CourseSession objects to CourseSession objects.

 public class CourseSession implements Comparable<CourseSession> { ... 

More on this

In the Student compareTo method, the return statement includes the expression this.getDepartment(). As you learned in Lesson 1, the this reference is to the current object. Scoping method calls with this is usually unnecessary, but in this case it helps differentiate the current object and the parameter object.

Another use of the this keyword that you haven't seen is for constructor chaining. You can invoke another constructor defined on the same class by using a slightly different form of this:

 class Name {    ...    public Name(String first, String mid, String last) {       this.first = first;       this.mid = mid;       this.last = last;    }    public Name(String first, String last) {       this(first, "", last);    }    ... 

The single statement in the second constructor of Name calls the first constructor. The goal in this example is to pass a default value of the empty string as the middle name.

Such a call to another constructor must appear as the first line of a constructor.

Constructor chaining can be a valuable tool for helping you eliminate duplication. Without chaining, you would need to write a separate method to do common initialization.


Since you bound the implementation of Comparable to the CourseSession, you must define the compareTo method so that it takes a CourseSession as parameter:

 public int compareTo(CourseSession that) {    return this.getDepartment().compareTo(that.getDepartment()); } 

For the compareTo method implementation, you are returning the result of comparing the current CourseSession's department (this.getDepartment()) to the parameter CourseSession object's department (that.getDepartment()).

At this point, both testComparable in CourseSessionTest and testReport in CouseReportTest should pass.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net