0

I'm writing a program and I'm supposed to check and see if a certain object is in the list before I call it. I set up the contains() method which is supposed to use the equals() method of the Comparable interface I implemented on my Golfer class but it doesn't seem to call it (I put print statements in to check). I can't seem to figure out whats wrong with the code, the ArrayUnsortedList class I'm using to go through the list even uses the correct toString() method I defined in my Golfer class but for some reason it won't use the equals() method I implemented.

//From "GolfApp.java"    
public class GolfApp{
ListInterface <Golfer>golfers = new ArraySortedList<Golfer> (20);
Golfer golfer;
//..*snip*..
if(this.golfers.contains(new Golfer(name,score)))
    System.out.println("The list already contains this golfer");
else{
    this.golfers.add(this.golfer = new Golfer(name,score));
    System.out.println("This golfer is already on the list");
}

//From "ArrayUnsortedList.java"
protected void find(T target){
    location = 0;
    found = false;

    while (location < numElements){
        if (list[location].equals(target))  //Where I think the problem is                       
        {
            found = true;
            return;
        }
        else 
            location++;
    }
 }

 public boolean contains(T element){
    find(element);
    return found;
 }


//From "Golfer.java"    
public class Golfer implements Comparable<Golfer>{
//..irrelavant code sniped..//
public boolean equals(Golfer golfer)
{
    String thisString = score + ":" +  name;  
    String otherString = golfer.getScore() + ":" + golfer.getName() ;
    System.out.println("Golfer.equals() has bee called");

    return thisString.equalsIgnoreCase(otherString);
}

public String toString()
{
    return (score + ":" + name);
}

My main problem seems to be getting the find function of the ArrayUnsortedList to call my equals function in the find() part of the List but I'm not exactly sure why, like I said when I have it printed out it works with the toString() method I implemented perfectly.

I'm almost positive the problem has to do with the find() function in the ArraySortedList not calling my equals() method. I tried using some other functions that relied on the find() method and got the same results.

4
  • Does your Golfer class actually implement Comparable? For example: "public class Golfer implements Comparable" Commented Sep 23, 2012 at 0:26
  • Yeah I should have included that like I did with the variables at the top. It acutally uses Generics for it too which I don't totaly understand yet, I'm not sure if that's the problem. Commented Sep 23, 2012 at 0:28
  • Is something preventing you from stepping through your code to see what's going on? Commented Sep 23, 2012 at 1:31
  • It turns out I the equals() method has to be in the form equals(Object objName) since it inherits from Object. I actually did run through it quite a few times though before I asked here :P Commented Sep 23, 2012 at 1:53

1 Answer 1

1

Your equals method should take an Object argument, not a Golfer. The equals(Golfer) method is overloading the Comparable's equals(Object) method but does not implement it. It's simply an overloaded method no other code knows about, so it doesn't get called.

public boolean equals(Object obj)
{
    if(!(obj instanceof Golfer)) return false;

    Golfer golfer = (Golfer)obj;
    String thisString = score + ":" +  name;  
    String otherString = golfer.getScore() + ":" + golfer.getName() ;
    System.out.println("Golfer.equals() has bee called");

    return thisString.equalsIgnoreCase(otherString);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.