I want to create a method equals(Object obj) to test if two objects are equal. But I don't even know how to start. Here is my program

class LinkedList {
    
  ListItem front = null;
  ListItem rear = null;
  int numItems = 0;      // Current number of items.
   
  // Could this class use a constructor?
    
  // Instance method to add a data item.
  public void addData (Object obj)
  {
    if (front == null) {
      front = new ListItem (obj);
      rear = front;
    }
    else {
      rear.next = new ListItem (obj);
      rear = rear.next;
    }
    numItems++;
  }
    
  public void printList ()   
  {
    ListItem listPtr = front;
    System.out.println ("List: (" + numItems + " items)");
    int i = 1;
    while (listPtr != null) {
      System.out.println ("Item# " + i + ": "
                          + listPtr.getData());
                          // Must implement toString()
      i++;
      listPtr = listPtr.next;
    }
  }

  // Membership method.
  public boolean memberOf (Object obj)
  {

    ListItem listPtr = front;
    while (listPtr != null) {
      if (listPtr.data.equals(obj))
        return true;
      listPtr = listPtr.next;
    }
    return false;
  }
                          
} // End of class "LinkedList"
      
      
// An object to use in the list:
   
class Person {
  
  String name;
  String ssn;
    
  // Constructor.
  public Person (String name_in, String ssn_in)
  {
    name = name_in;  ssn = ssn_in;
  }  
    
  // Override toString()
  public String toString ()
  {
    return "Person: name=" + name + ", ssn=" + ssn;
  }   

   
} // End of class "Person"
  
public boolean equals(Object obj)
  {
   
  
}
   
    
     
// Test class.
  
class TestListMember2 {
   
  public static void main (String[] argv)
  {   
    // Create a new list object.
    LinkedList L = new LinkedList ();

  
    L.addData (new Person ("Terminator", "444-43-4343"));
    L.addData (new Person ("Rambo", "555-54-5454"));
    L.addData (new Person ("James Bond", "666-65-6565"));
    L.addData (new Person ("Bruce Lee", "777-76-7676"));
 
    // Print contents.
    L.printList();
     
    // Test membership.
    Person p = new Person ("Rambo", "555-54-5454");
    if (L.memberOf (p))
      System.out.println ("Rambo is in the list");
    else
      System.out.println ("Rambo is not in the list");
    
    Person p2 = new Person ("PeeWee", "555-54-5454");
    if (L.memberOf (p2))
      System.out.println ("PeeWee is in the list");
    else
      System.out.println ("PeeWee is not in the list");
  }
    
}

Recommended Answers

All 15 Replies

you have to overide the method equal in tha appropriate class inside the class definition and not outside.

But it is inside the right class, in this case Person

Quick Answer: Yes, you have to write your own equals method in the Person class so that you override the inherited (java.lang.Object) default equals method.
Reason: the inherited method will only compare the references (memory addresses) of the 2 objects which is almost meaningless within the context of your program. You as the designer gets to decide what actually makes 2 objects equal to each other and then the fun begins: you have to code it up algorithmically :-) yeeeha!
In the case of your Person class you might decide that the Name and SSN class fields being equal make the 2 instances of Person equal to each other....
You will end up with an equals method in Person class that is called like this
Person.equals(inPerson);
maybe this looks confusing to you but you have to think that the instance executing the equal method will be comparing ITSELF (it's own state variable values) with the instance you are passing in for the comparison (inPerson).

Hope that helps somewhat; I'm a newbie to so someone else may want to check what I've said :-)

so you will compare
Person.name with inPerson.name [ be careful here as Strings are objects so you will need to do that kinda like this:
name.equals( inPerson.getName() ); which will return a boolean...
if you just do this (I've assumed you have or will write a getName() method to access your name class variable as it should be private anyway)... and, if you just do this
name == inPerson.name; you will just be comparing references (memory addresses again!) and that is meaningless here because you want to compare the values: Strings are objects, not primitives!
ssn is ok though as it's a primitive (if I remember correctly) so you can just do something like this:
ssn == inPerson.ssn; which will return a boolean

does that help you out?

here is a version of an equals method (sort of) in the context of your Person class... assuming a few things:
1. that you make your class variables private (and you should!)
2. that you add the following accessor methods ('getters'):

public String getName() {
        return name;
    }

    public String getSsn() {
        return ssn;
    }

AND here is a possible equals method; but check it thoroughly as I'm a newbie too!

@Override
    public boolean equals(Person inPerson) {
        boolean isEqual = Boolean.FALSE;    // assume they are not equal
        if( name.equals( inPerson.getName() ) && 
            ssn.equals( inPerson.getSsn() )  ) {
                isEqual = Boolean.TRUE;     // only now are the contents found to be equal
        }
        return isEqual;
    } // end of equals method

Does that help?

FYI:
"Boolean.TRUE" is valid Java, but "true" is also valid, and what most people would write.
This code also makes the very common mistake of redundantly using an if test to set a boolean. So it could be written

@Override
 public boolean equals(Person inPerson) {
   return  name.equals( inPerson.getName() ) &&  ssn.equals( inPerson.getSsn() );
 } // end of equals method

hey, of course! Like that simplicity!

James, got any other idea's on my class reference prob? Still getting nowhere...

what is the difference between equals(Object obj) and equals(Person inPerson)?

Object is the superclass of all objects in JAVA which includes your Person class. Person therefore inherits attributes and methods from Object.

you can override the method equals(Object obj) of the Object class in your Person class by having equals(Person inPerson) because Person is also type of Object.

confused much? :)

welcome to the object-oriented programming! yay!

hahah I think I understood. Let me try to finish this, i will get back to you'll

This is the error I am getting now:

TestListMember2.java:119: 'class' or 'interface' expected
public boolean equals(Person inPerson)
^
1 error

can you post your new code?

// A node that contains any "Object"

class ListItem {

  Object data = null;    // The universal Object.
  ListItem next = null;

  // Constructor.
  public ListItem (Object obj)
  {
    data = obj;  next = null;
  }

  // Accessor.
  public Object getData () 
  {
    return data;
  }
}


// Linked list class - also a dynamic class.

class LinkedList {

  ListItem front = null;
  ListItem rear = null;
  int numItems = 0;      // Current number of items.

  // Could this class use a constructor?

  // Instance method to add a data item.
  public void addData (Object obj)
  {
    if (front == null) {
      front = new ListItem (obj);
      rear = front;
    }
    else {
      rear.next = new ListItem (obj);
      rear = rear.next;
    }
    numItems++;
  }

  public void printList ()
  {
    ListItem listPtr = front;
    System.out.println ("List: (" + numItems + " items)");
    int i = 1;
    while (listPtr != null) {
      System.out.println ("Item# " + i + ": " 
                          + listPtr.getData());
                          // Must implement toString()
      i++;
      listPtr = listPtr.next;
    }
  }

  // Membership method.
  public boolean memberOf (Object obj)
  {
    ListItem listPtr = front;
    while (listPtr != null) {
      if (listPtr.data.equals(obj))
        return true;
      listPtr = listPtr.next;
    }
    return false;
  }

} // End of class "LinkedList"


// An object to use in the list:

class Person {

  String name;
  String ssn;

  // Constructor.
  public Person (String name_in, String ssn_in)
  {
    name = name_in;  ssn = ssn_in;
  }
 public Person (String name_in, String ssn_in)
  { 
    name = name_in;  ssn = ssn_in; 
  }     

   public String getName() {
      return name;
      }

   public String getSsn() {
      return ssn;
     }

  // Override toString()
  public String toString ()
  {     
    return "Person: name=" + name + ", ssn=" + ssn;
  }

} // End of class "Person"


// Test class.

class TestListMember2 {

  public static void main (String[] argv)
  {
    // Create a new list object.
    LinkedList L = new LinkedList ();


    L.addData (new Person ("Terminator", "444-43-4343"));
    L.addData (new Person ("Rambo", "555-54-5454"));
    L.addData (new Person ("James Bond", "666-65-6565"));
    L.addData (new Person ("Bruce Lee", "777-76-7676"));

    // Print contents.
    L.printList();

    // Test membership.
    Person p = new Person ("Rambo", "555-54-5454");
    if (L.memberOf (p))
      System.out.println ("Rambo is in the list");
    else
      System.out.println ("Rambo is not in the list");

    Person p2 = new Person ("PeeWee", "555-54-5454");
    if (L.memberOf (p2))
      System.out.println ("PeeWee is in the list");
    else
      System.out.println ("PeeWee is not in the list");
  }

}
// A node that contains any "Object"

class ListItem {

Object data = null; // The universal Object.
ListItem next = null;

// Constructor.
public ListItem (Object obj)
{
data = obj; next = null;
}

// Accessor.
public Object getData ()
{
return data;
}
}


// Linked list class - also a dynamic class.

class LinkedList {

ListItem front = null;
ListItem rear = null;
int numItems = 0; // Current number of items.

// Could this class use a constructor?

// Instance method to add a data item.
public void addData (Object obj)
{
if (front == null) {
front = new ListItem (obj);
rear = front;
}
else {
rear.next = new ListItem (obj);
rear = rear.next;
}
numItems++;
}

public void printList ()
{
ListItem listPtr = front;
System.out.println ("List: (" + numItems + " items)");
int i = 1;
while (listPtr != null) {
System.out.println ("Item# " + i + ": "
+ listPtr.getData());
// Must implement toString()
i++;
listPtr = listPtr.next;
}
}

// Membership method.
public boolean memberOf (Object obj)
{
ListItem listPtr = front;
while (listPtr != null) {
if (listPtr.data.equals(obj))
return true;
listPtr = listPtr.next;
}
return false;
}

} // End of class "LinkedList"


// An object to use in the list:

class Person {

String name;
String ssn;

// Constructor.
public Person (String name_in, String ssn_in)
{
name = name_in; ssn = ssn_in;
}
public Person (String name_in, String ssn_in)
{
name = name_in; ssn = ssn_in;
}

public String getName() {
return name;
}

public String getSsn() {
return ssn;
}

// Override toString()
public String toString ()
{
return "Person: name=" + name + ", ssn=" + ssn;
}
public boolean equals(Person inPerson)
  {
    
  return name.equals(inPerson.getName()) && ssn.equals(inPerson.getSsn());


} // End of class "Person"


// Test class.

class TestListMember2 {

public static void main (String[] argv)
{
// Create a new list object.
LinkedList L = new LinkedList ();


L.addData (new Person ("Terminator", "444-43-4343"));
L.addData (new Person ("Rambo", "555-54-5454"));
L.addData (new Person ("James Bond", "666-65-6565"));
L.addData (new Person ("Bruce Lee", "777-76-7676"));

// Print contents.
L.printList();

// Test membership.
Person p = new Person ("Rambo", "555-54-5454");
if (L.memberOf (p))
System.out.println ("Rambo is in the list");
else
System.out.println ("Rambo is not in the list");

Person p2 = new Person ("PeeWee", "555-54-5454");
if (L.memberOf (p2))
System.out.println ("PeeWee is in the list");
else
System.out.println ("PeeWee is not in the list");
}

}

firstly you have two same constructors at line 83 to 90. And your error was caused by a lack of a semicolon on the Person class. line 111 is not the end of Person class rather its the end of equals() method. you need to add one more semicolon after that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.