user2864180
user2864180

Reputation: 1

Im trying to Write a Team class encapsulating the concept of a Team but I can't seem to compare the two teams

Heres what I have so far including the test class I'm just not sure how to compare the two teams this is the assignment................ The team has only one attribute, the team name (example Barcelona)

Include the following:

Class Team

public class Team {

private String name;

public Team(String name) { // Constructor

    this.name = name;
}

public void setName(String name) { // The Setter set's the name

    this.name = name;
}

public String getName() { // The Getter get's the the name

    return name;
}

@Override
public String toString() { // This returns a String object

    return "The team name is " + name;
}

@Override
public boolean equals(Object o) {

    if (o instanceof Team) {

        Team t = (Team) o;

        if (t.getName().equalsIgnoreCase(getName())) { //I'm not sure what i'm missing here

            return true;
        }

        else {
            return false;

        }

    }

    else {

        return false;
    }
}

}

Test class

public class TeamMain {
public static void main(String[] args) {

    Team team = new Team("ManUnited");

    Team team2 = new Team("ManCity");
    System.out.println(team.equals("ManUnited"));

    System.out.println(team2.equals("ManCity"));

    System.out.println(team.getName());
    team.setName("ManCity");
    System.out.println(team.toString());
}

}

Upvotes: 0

Views: 1279

Answers (3)

Richard Tingle
Richard Tingle

Reputation: 17226

You are asking if the team is equal to a String that happens to have the letters "ManUnited" in it, they are not the same because a string isn't a team.

System.out.println(team.equals("ManUnited"));

team is a Team, that contains a String, "ManUnited" actually is a String.

The correct test of your equals method would be

   Team team = new Team("ManUnited");
   Team team2 = new Team("ManCity");
   Team team3 = new Team("ManUnited");

   System.out.println(team.equals("team2")); //prints false
   System.out.println(team.equals("team3")); //prints true

Which correctly assesses equality (note the hashcode issue below however)

HashCode

Additionally (but not the direct cause of your problem) you are overriding the .equals() method without overriding the .hashCode() method, this may cause unexpected behaviour for classes/methods which use hashcode as a short cut to assessing equality (eg the HashSet). A good IDE will (prompt to) override the HashCode for you.

The hashcode is a function that returns an integer that is guaranteed equal for equal objects and relatively unlikely to be equal for non equal objects. In the non overridden state equals() and hashCode() both assess equality with respect to the objects being literally the same object. You have (quite legitimately) changed the equals method to consider objects which "look the same" to be equal, but the hashcode is still looking at if the objects are actually the same; hence unexpected behaviour

Upvotes: 1

user278064
user278064

Reputation: 10180

Item 9: Always override hashCode when you override equals

A common source of bugs is the failure to override the hashCode method. You must override hashCode in every class that overrides equals. Failure to do so will result in a violation of the general contract for Object.hashCode,

Effective Java, 2nd Ed.

And as pointed by @rgettman, you should compare Teams with Teams, and not Teams with strings. Otherwise you're code will return false.

System.out.println(team.equals("ManUnited")) // they are not alike

public boolean equals(Object o) {

    if (o instanceof Team) {

        Team t = (Team) o;

        if (t.getName().equalsIgnoreCase(getName())) { //I'm not sure what i'm missing here

            return true;
    }

        else {
            return false;

        }

   } else {

    return false; // team.equals("ManUnited") will return false, since string is not instance of Team.
}

Upvotes: 1

rgettman
rgettman

Reputation: 178283

In this line:

System.out.println(team.equals("ManUnited"));

You are comparing your Team to a String, so your code correctly returns false, because your o is not an instance of Team. Compare to a Team instead:

System.out.println(team.equals(new Team("ManUnited")));

Upvotes: 1

Related Questions