Equality


In an upcoming lesson, you will build a course catalog. One requirement for the course catalog will be to ensure that duplicate courses are not added to the catalog. Courses are semantically equal if they have the same department and course number.


Now that you have a functional Course class, you will want to override the equals method to supply a semantic definition for equality. If you have two Course objects and the department and number fields in both objects contain the same (corresponding) String, comparing the two should return true.

 // in CourseTest.java: public void testEquality() {    Course courseA = new Course("NURS", "201");    Course courseAPrime = new Course("NURS", "201");    assertEquals(courseA, courseAPrime); } 

This test fails. Remember from Lesson 1 that the object courseA is a separate object in memory, distinct from courseAPrime. The assertEquals method translates roughly[5] to the underlying lines of code:

[5] The actual code is slightly more involved.

 if (!courseA.equals(courseAPrime))    fail(); 

In other words, the comparison between two objects in assertEquals uses the equals method defined on the receiver. The receiver is the first parameter, which JUnit refers to as the expected value. The receiver in this example is courseA.

You have not yet defined an equals method in Course. Java therefore searches up the hierarchy chain to find one. The only superclass of Course is the implicit superclass Object. In Object you will find the default definition of equals:

 package java.lang; public class Object {    // ...    public boolean equals(Object obj) {       return (this == obj);    }    // ... } 

The equals implementation in Object compares the referencethe memory locationof the receiver to the reference of the object being passed in.

If you do not provide a definition for equals in your subclass, you get the default definition, memory equivalence.


Memory equivalence exists if two references point to the exact same object in memory.

You will want to override the equals method in the Course object. The Course definition of equals will override the Object definition of equals. The equals method should return true if the receiver object is semantically equal to the parameter object. For Course, equals should return TRue if the department and course number of the receiver is equal to that of the parameter Course.

Let's code this slowly, one step at a time. For now, code the equals method in Course to most simply pass the test:

 @Override public boolean equals(Object object) {    return true; } 

The equals method must match the signature that assertEquals expects. The assertEquals method expects to call an equals method that takes an Object as parameter. If you were to specify that equals took a Course object as parameter, the assertEquals method would not find it:

 @Override public boolean equals(Course course) {// THIS WILL NOT WORK    return true; } 

The assertEquals method would instead invoke the Object definition of equals.

Add a comparison to the test that should fail (since equals always returns true):

 public void testEquality() {    Course courseA = new Course("NURS", "201");    Course courseAPrime = new Course("NURS", "201");    assertEquals(courseA, courseAPrime);    Course courseB = new Course("ARTH", "330"); assertFalse(courseA.equals(courseB)); } 

Once you see that this test fails, update your equals method:

 @Override public boolean equals(Object object) {    Course that = (Course)object;    return this.department.equals(that.department) && this.number.equals(that.number); } 

You must first cast the object argument to a Course reference so that you can refer to its instance variables. The local variable name you give to the argument helps differentiate between the receiver (this) and the argument (that). The return statement compares this Course's department and number to the department and number of that Course. If both (&&) are equal, the equals method returns true.



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