15-100 Lecture 9 (Friday, February 4, 2008)

Quiz

Given the following section excerpted from a class specification, please add two methods: the canonical method that returns a String representation of an instance and the canonical method that determins if another instance is equivalent to this one.

  class Person {
    private String name;
    private int age;

    ...
  }
  

The two requested methods are toStirng() and equals(). Since they override methods inherited from the Object class, we must use exactly the correct signatures. Please find a solution below:

  class Person {
    private String name;
    private int age;

    ...

    public String toString() {
      return name + ", " + age; // The exact formatting was up to you
    }

    public boolean equals (Object o) {
      Person p = (Person) o;

      if (!this.name.equals(p.name))
        return false;

     if (this.age != p.age)
        return false;
    
      return true;
    } 
  }
  

Upcoming Event

Next class, we've got an exam. What do you think we'll ask you to do? Unsurprisingly, the exam will mostly be asking you to do things very similar to the stuff we've been doing in class and also on the labs.

You'll be asked to implement a class, including the contructor(s), toString(), and equals() methods. You'll also be asked to write a test driver within main(). Please pay careful attention to the signatures of these methods -- they have to be correct.

You'll need to know how to correctly use the various primitives (int, long, float, double, char, boolean), and the String class. You'll need to know when to use == and when to use .equals(). The methods within th class can make use of all of the operators we've learned as well as if and if-else statements.

In order to make good use of time, you may be asked to implement only certain methods and constructors, rather than all of them. Similarly, you may be asked to test only certain features, rather than all of them, in the test driver.

There will also be a short answer section. In this section, you'll mostly be asked about "vocabulary". What is an object? A reference? A class? An instance variable? A constructor? But, there might also be some other types of questions, such as, "What is the difference, in code, between a constructor and a method?" Or, "There is a particular signature associated with the equals(), why can't we change this signature?"

Building Up the Person Example


class Person {
  private String name;
  private int age;


  public Person (String name, int age) {
    this.name = name;
    this.age = age;
  }


  // For fun, let's do a second version that init's age to a sentinel value
  public Person (String name) {
    this.name = name;
    this.age = -1;
  }


  public String getName() {
    return name;
  }


  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }


  public boolean isOlder (Person p) {
    return (age > p.age);
  }


  public String toString() {
    return name + ", " + age; // The exact formatting was up to you
  }


  public boolean equals (Object o) {
    Person p = (Person) o;

    if (!this.name.equals(p.name))
      return false;

   if (this.age != p.age)
      return false;

    return true;
  }


  public static void main (String[] args) {
    Person fullInit = new Person ("Greg", 33);
    System.out.println (fullInit);

    Person nameOnlyInit = new Person ("Greg");
    System.out.println (nameOnlyInit);
    System.out.println (nameOnlyInit.getName() + ", " + nameOnlyInit.getAge());

    if (fullInit.equals(nameOnlyInit)) 
      System.out.println ("1st equals test: Equivalent -- Incorrect");
    else
      System.out.println ("1st equals test: Not Equivalent -- Correct");

    nameOnlyInit.setAge(fullInit.getAge());
    System.out.println(nameOnlyInit);

    if (fullInit.equals(nameOnlyInit)) 
      System.out.println ("2nd equals test: Equivalent -- Correct");
    else
      System.out.println ("2nd equals test: Not Equivalent -- Incorrect");

    Person matchesOnlyAge = new Person ("Jimbo", 33);
    System.out.println (matchesOnlyAge);

    if (fullInit.equals(matchesOnlyAge)) 
      System.out.println ("3rd equals test: Equivalent -- Incorrect");
    else
      System.out.println ("3rd equals test: Not Equivalent -- Correct");

    Person younger = new Person ("Angie", 32);

    if (fullInit.isOlder(younger))
      System.out.println ("1st isOlder test: Older -- Correct");
    else
      System.out.println ("1st isOlder test: Not Older -- Inorrect");

    if (younger.isOlder(fullInit))
      System.out.println ("2nd isOlder test: Older -- Incorrect");
    else
      System.out.println ("2nd isOlder test: Not Older -- Correct");

    if (matchesOnlyAge.isOlder(fullInit))
      System.out.println ("3rd isOlder test: Older -- Incorrect");
    else
      System.out.println ("3rd isOlder test: Not Older -- Correct");

  
    
  }
}