CS/SWE 332 In Class Exercise Number 1


Instructions: Work with your group. Your group assignment is on Piazza. (You might decide to change groups later, but for today, stick with this group.) When we invoke the BB break-out function, nagivate to your assigned group. (Yes, this is nuts from a usability perspective, but it's BB.)
  1. Your group should spend a few minutes getting acquainted. Explain a bit about yourself: full-time student?, working in software development?, why are you taking this class?, favorite/least favorite thing about writing software?, etc.
  2. Decide on a mechanism for joint communication. Google docs? IDE with screen share? Something else?
Now address a technical topic. This exercise touches on some of the thorny issues in data abstraction and inheritance. There is a lot going on in this example. Hence don't worry if it seems confusing today. We'll revisit this example several times over the course of the semester.

Consider the following (textbook) code:

public class User {
    private String name;
    public User (String name) { this.name = name; }
    @Override public boolean equals (Object obj) {
       if (!(obj instanceof User)) return false;
       return ((User) obj).name.equals(this.name);
    }
}
public class SpecialUser extends User {
    private int id;
    public SpecialUser (String name, int id) { super(name); this.id = id; }
    @Override public boolean equals (Object obj) {
       if (!(obj instanceof SpecialUser)) return false;
       return super.equals(obj) && ((SpecialUser) obj).id == this.id;
    }
}
  1. What does it mean for the equals() method to be correct? How do you know? Be as concrete as you can. Can you check correctness with test cases?




  2. How does the given implementation of equals() in in class User stack up? Again, be concrete. Can you find a test case that demonstrates the problem?




  3. How does inheritance complicate the correctness discussion for equals() in class SpecialUser?




  4. What do you think of the equals() method in the SpecialUser class?