ITEC 120
L010a: Comparing Treasures

Objectives

To successfully complete this lab you will develop the equals and compareTo methods and use these methods to compare treasure objects. You will also understand the difference between a shallow comparison and a deep comparison.

Assignment

Download Comp.java and compile the program. Execute the program and walk through the code while examining the output.  Explain to your partner how the program works. 

equals

equals is a special method that defines what it means for two objects to be equal to each other. We use == to compare primitive data types, but for objects, we need to define the equals method.

What happens when you use == to compare two objects?

equals is a boolean method; it returns true if this object (the one used to call the method, which is the one before the dot) is the same as the specified object (the one passed to the method as a parameter), and false if the two objects are not the same as each other.

You will write an equals method for your Treasure class which will define what it means for two treasures to be the same as each other. Extend your Treasure class by declaring a method named equals that takes a treasure as an input and returns true if all four instance variables have the same value. Strings are objects! Be sure to use the String method equals when comparing strings.

compareTo

compareTo is a special method that provides a way to put objects in order. We define a compareTo method for objects we create when we want that type of object to be Comparable.

compareTo returns an integer: a positive integer if this object (the one used to call the method, which is the one before the dot) is greater than the specified object (the one passed to the method as a parameter). It returns a negative integer if this object is less than the specified object, and it returns zero if this object is equal to the specified object. The two objects being compared are the same type. Extend your Treasure class by declaring a method named compareTo that takes a treasure as a parameter and compares the value property of the two treasures. 

Java sorts and searches use the compareTo method.

TreasureTest

Develop a test driver named TreasureTest. Implement the test cases defined below. Create a treasure named t1 and create four additional treasures t11 – t14. Each of these treasures has one property different from t1.

    1. t1 == t1
    2. t1 == t11
    3. t1.equals(t1)
    4. t1.equals(t11)
    5. t1.equals(t12)
    6. t1.equals(t13)
    7. t1.equals(t14)
    8. t1.compareTo(t1)
    9. Test compareTo with objects that return a positive number
    10. Test compareTo with objects that return a negative number

Create an explorer in the test driver and put a treasure in each pocket such that:

    1. Left pocket == right pocket
    2. Left pocket != right pocket
    3. Left pocket .equals right pocket
    4. Left pocket !equals right pocket
    5. Left pocket compareTo right pocket returns 0
    6. Left pocket compareTo right pocket returns positive number
    7. Left pocket compareTo right pocket returns negative number

Submit Your Assignment

Submit Treasure.java and TreasureTest.java to the L10a dropbox on D2L.