RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab10a
== vs .equals
lab10a

For each of the following classes:
  1. discuss: when should two different instances be considered .equals (if ever)? (Is looking-alike good enough?)
  2. Write test cases for a .equals method (with a stub version which always returns, say, false). When stubbing out test cases for any boolean function, you should always create at least two test cases (one which returns true, one which returns false). For testing .equals tests, there are three natural tests:
    1. instances which are ==,
    2. instances which are different yet have all the same fields,
    3. and
    4. instances which differ in at least one field.
  3. Expand your .equals stub with actual code, so that you pass the test cases.
Remember: For Strings, use .equals (and not ==) to check whether two Strings both have the same sequence of characters.
  1. PhoneNumber (Compare each field -- not just their printed (String) representations.)
    Here is a skeleton for class PhoneNumber you can use if you didn't complete lab09.
  2. Address
  3. Name
  4. ContactInfo
  5. Sometimes data-mining companies obtain two different records which both talk about the same person, even though some information is incomplete or not a complete match. (For instance, credit card companies want to know if the person

    Holden Card, living at 123 House St., with phone number 540-321-9876
    is the same as
    Holden A. Card, living at 123 House Street, with phone number 540-321-9876 and work number 540-123-4567.
    While it's dangerous to write an .equals which would consider the above to the same, certainly we could have a method likelyEquals that would identify such matches. (The data-mining company might have a human review such likely matches, to make a final decision.)

    Your task: For ContactInfo, write a method probablyEqual, which returns true if the two instances have the same first & last name and the same address (but other fields may be different).

  6. Dog (from hw03-soln). Are two dogs to be considered equal, if they are the same age and make the same sound? What is a strong enough condition to know two Dog objects should be considered equal? Write .equals; the method-body need only be three words and two punctuations long!

public vs. private

We mentioned that every field and method should be declared either public or private (as appropriate -- in today's lab, every field is private and every method is public). Add the appropriate adjective in front of every field and method declaration. (How does this change the auto-generated documentation?)

Similar to lect09b, include the following comment inside your .equals method:

    // WARNING: We'll learn in week14:
    // the parameter-type for `equals' should actually be `Object'.

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme