0
public class Team {

    private String name, sport;
    private int numOfWins, numOfLosses;
    public Team(String name, String Sport){
        this.name= name;
        this.sport= Sport;
        numOfWins= 0;
        numOfLosses= 0;
    }

    public int addAnotherWin(){
        return numOfWins++;
    }

    public int addAnotherLoss(){
        return numOfLosses++;
    }

    public int getNumWins(){
        return numOfWins;
    }

    public int getNumLosses(){
        return numOfLosses;
    }

    public void resetWinsAndLosses(){
        numOfWins= numOfWins*0;
        numOfLosses= numOfLosses*0;
    }

    public double getPercentOfGamesWon(){
        double z= numOfWins/ numOfWins+numOfLosses;
        return z;
    }

    public boolean equals(){    

    }

    public String toString(){

    }
} 

So my equals method is suppose to compare two teams and return if they are equals if their PercentOfGamesWon is equal to each other, but I don't know how to write that, any help please? Another thing i need help with is the toString, i'm not sure what its for and what i'm suppose to write in it. I just started coding so sorry if this is a noob question.

Aadi Droid
  • 1,609
  • 3
  • 21
  • 46
  • Read the documentation. For one thing, `equals()` takes a parameter. – SLaks Oct 12 '14 at 20:31
  • Another step would be to make sure you format your code in your question so that the people you're asking for help can easily read it. – Mike 'Pomax' Kamermans Oct 12 '14 at 20:31
  • Add an Object argument to the **equals()** method and inside that method, check the variables held in this object and the argument to make sure they are equal. – SamTebbs33 Oct 12 '14 at 20:38
  • 1
    A more interesting question is how to implement a compareTo() for Team, which would incidentally solve your .equals() problem. It's likely that instances of team have to be ranked by performance at some point, so why not follow the natural progression and solve the inevitable issue. – MarsAtomic Oct 12 '14 at 20:39

4 Answers4

2

I highly suggest referring to the answers in What issues should be considered when overriding equals()

First see if the object being compared is actually a team (using instanceof), then compare the percentages:

public boolean equal(Object obj) {
     if(!obj instanceof Team) 
          return false;

     return getPercentOfGamesWon() == ((Team) obj).getPercentOfGamesWon();
} 

Now you can call the equals method of one Team and pass in the other team to compare them:

Team team1 = new Team();
Team team2 = new Team();

boolean equal = team1.equals(team2);

As mentioned by @MarsAtomic in the comment section of your question, you should really be implementing a compareTo(Team) method. This method should return a number based on whether the percentage is higher, lower or the same:

class Team implements Comparable<Team> {

     public int compareTo(Team team) {
          int ourPercent = getPercentOfGamesWon();
          int theirPercent = team.getPercentOfGamesWon();

          if(ourPercent > theirPercent)
               return 1;
          else if(ourPercent < theirPercent)
               return -1;
          else
               return 0; //the same
     }
}
Community
  • 1
  • 1
Dioxin
  • 13,042
  • 5
  • 35
  • 70
  • 1
    Percents should be doubles, not ints for better accuracy. And `Integer.compare()` saves you some work. – user949300 Oct 12 '14 at 21:00
  • @user949300 That doesn't make sense, as 1.00 can be represented as 100, and vice versa (double gives more range being 64 bit, but I doubt that kind of accuracy is needed). As for `Integer.compare`, it lacks scalability and is not easily testable. With `Comparable`, you can now document what is being compared, and you could also change the implementation to compare in a different mannar (such as lexical comparing of names, comparing by some non-integer value like the team's Color) – Dioxin Oct 12 '14 at 21:26
  • @user949300 Ah, I now see what you mean by using `Integer.compare` (I guess it could shorten the compareTo method), but seeing how I'm strict when it comes to testability, I try to avoid static methods unless I *REALLY* need to use one – Dioxin Oct 12 '14 at 21:41
  • If one team has 199 wins and 1 loss, and another has 198 wins and 2 losses, they will both be at 99% using ints and incorrectly be "equal". There are surely smaller and more reasonable numbers causing this bug, but these were the easiest to come up with. – user949300 Oct 12 '14 at 23:59
1

As @MarsAtomic commented, this is more suited for a Comparator, not equals. Even if the Giants and Dodgers finish with the same record, I don't think you want to consider them "equals". And you are likely to want to rank the teams by performance.

public static Comparator<Team> BY_PCT = new Comparator<Team>() {
   public int compare(Team t1, Team t2) {
      double winPct1 = t1.getPercentOfGamesWon();
      double winPct2 = t2.getPercentOfGamesWon();
      return Double.compare( winPct1, winPct2);
   }
};

You could also implement Comparable as in Vince's answer.

user949300
  • 14,622
  • 6
  • 30
  • 61
0

First step is to determine how to calculate a percentage. Percentage is often written as "Value/Combined Values". If youre just returning a percentage then that is your amswer. Though if accuracy is important I reccomended using the BigDecimal class.

As for the toString method, you can basically put anything in here. ToString() is supposed to give meaningful text to an object. This relates to the equals method. In many objects .equals() compares hashcode, the location in memory. Therefore, toString often returns hashcode for many objects. It is essential to make hashcode equal to values you are comparing and many times toString is just a string representation of these values. In your case I would return the percentage.

DanSchneiderNA
  • 346
  • 4
  • 15
  • Also, your getPercentOfGamesWon method will return a wrong value due to order of operations. Put the addition in parentheses. But following the code of Vince Emigh is completely okay. It takes the general idea of what I say and puts it to actual use. – DanSchneiderNA Oct 12 '14 at 20:47
0

You can do it like this:

public class Team {

    private String name, sport;
    private int numOfWins, numOfLosses;

    public Team(String name, String Sport) {
        this.name = name;
        this.sport = Sport;
        numOfWins = 0;
        numOfLosses = 0;
    }

    public int addAnotherWin() {
        return numOfWins++;
    }

    public int addAnotherLoss() {
        return numOfLosses++;
    }

    public int getNumWins() {
        return numOfWins;
    }

    public int getNumLosses() {
        return numOfLosses;
    }

    public void resetWinsAndLosses() {
        numOfWins = numOfWins * 0;
        numOfLosses = numOfLosses * 0;
    }

    public Double getPercentOfGamesWon() {
        double z = numOfWins / (numOfWins + numOfLosses);
        return new Double(z);
    }

    @Override
    public boolean equals(Object o) {
        if ( o instanceof Team)
        {
            Team other = (Team)o;
            return this.getPercentOfGamesWon().equals(other.getPercentOfGamesWon());
        }
        return false;
    }

    public static void main(String[] args) {
        Team teamOne = new Team("Falcons", "Football");
        teamOne.addAnotherWin();
        teamOne.addAnotherWin();

        Team teamTwo = new Team("Broncos", "Football");
        teamTwo.addAnotherWin();
        teamTwo.addAnotherWin();

        if( teamOne.equals(teamTwo) )
        {
            System.out.println("Two wins, they are equal");
        }

        teamOne.addAnotherLoss();

        if ( ! teamOne.equals(teamTwo) )
        {
            System.out.println("Two wins, but one loss, they are not equal");
        }


    }

}

I added a main so you you could see it in action. Another, possibly more useful solution might be to implement Comparable, which would let you see if a team is less than, equal to, or greater than another team.

David S.
  • 6,043
  • 1
  • 23
  • 44
  • This is not really a good equals due to floating point errors. It should check equality of the doubles within a certain threshold of each other. – nmore Oct 12 '14 at 21:10
  • Looks like it is hard to induce the errors with whole numbers though. – nmore Oct 12 '14 at 21:15