Results 1 to 7 of 7
  1. #1
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default How to override Equals method with a Java File

    I have a file which I named myDriver.java which looks like this:
    Java Code:
    package cs235.java;
    
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.io.IOException;
    
    public class myDriver{
    
    public static void main(String [] args){
    	String one;
    	String two;
    	Factory fac =new Factory();
    	Person [] PersonList = new Person[3];
    	PersonList[0]=fac.createEmployee("104323425", "Trevor", "Home");
    	PersonList[1]=fac.createStudent("120420432", "Jordan", "IT", 4.0);
    	PersonList[2]=fac.createEmployee("104323425", "Jordan", "Campus Life");
    	Person [] PersonList2 = new Person[1];
    	PersonList2[0]=fac.createEmployee("104323425", "Trevor 2", "School");
    	//if(((PersonImpl)PersonList[1]).equals(((PersonImpl)PersonList[1]))){
    	System.out.println("ID of One: " + PersonList[0].getID());
    	System.out.println("ID of Two: " + PersonList2[0].getID());
    	if(!PersonList[0].equals(PersonList[2])){
    		System.out.println("They Should be Equal");
    	}
    	else{
    		System.out.println("They Are Equal");
    	}
    	
    	System.out.println(((PersonImpl)PersonList[1]).compareTo(((PersonImpl)PersonList[2])));
    	System.out.println(((PersonImpl)PersonList[1]).compareTo(((PersonImpl)PersonList[0])));
    	PersonIO listwriter=new PersonIO();
    	listwriter.save(PersonList, "output1.txt");
    	
    	PersonIO listreader=new PersonIO();
    	listreader.load("output1.txt");
    }
    
    }
    The thing that isn't working is the Equals method, For some reason it's not using the equals method I defined in the PersonImpl.java I wrote below:

    package cs235.java;

    Java Code:
    public class PersonImpl implements Person {
    	
    	public String getID(){
    		return theID;
    	}
    	public void setID(String id){
    		theID=id;
    	}
    	public String getName(){
    		return theName;
    	}
    	public void setName(String name){
    		theName=name;
    	}
    	@Override 
    	public boolean equals(PersonImpl per){
    		System.out.println("Equals Sees for main: " + this.getID());
    		System.out.println("Equals Sees for Second: " + per.getID());
    		return this.getID().equals(per.getID());
    		
    	}
    	
    
    	public int compareTo(PersonImpl per){
    		return this.getID().compareTo(per.getID());
    	}
    	public String theID;
    	public String theName;
    
    }
    Instead it uses the default equals method.
    It works if I cast the Person to a PersonImpl like this ((PersonImpl)Person[1]).equals(((PersonImpl)Person2[1]));

    Like you can see in My comments but I can't do it that way.

    Any ideas or help would be greatly appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your compiler surely is complaining about the @Override annotation, correct? Your equals method signature must match exactly that of Object's. That includes the Object parameter. So use Object as your parameter not PersonImpl. This means that you will first need to check the parameter is an instanceOf PersonImpl before using it (return false if it's null or if it fails the instanceOf check), THEN you need to cast the object to PersonImpl before calling the getID method.

  3. #3
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default

    Sorry I forgot to remove the @Override before I posted, I added that in to see if it would work but unfortunately it doesn't. Basically I need my main function to be able to call the equals method of the PersonImpl which implements from Person Class

    Java Code:
    package cs235.java;
    
    /**
     * The Person interface represents a person who is affiliated with a university
     */
    public interface Person {
        
        /**
         * Returns the person's ID
         * 
         * @return the person's ID.
         */
        String getID();
        
        /**
         * Sets the person's ID
         * 
         * @param id the new value for the person's ID.
         *      This may be any non-null String
         * @throws IllegalArgumentException if id is null
         */
        void setID(String id);
        
        /**
         * Returns the person's name
         * 
         * @return the person's name.
         */
        String getName();
        
        /**
         * Sets the person's name
         * 
         * @param name the new value for the person's name.
         *      This may be any non-null String
         * @throws IllegalArgumentException if name is null
         */
        void setName(String name);
    
    }
    So I need it to work like this:

    Person [] personlist=new Person[4];

    if(personlist[2].equals(personlist[3]){
    do if stuff
    }

    Which doesn't work it only makes the equal return true if the two objects are exactly the same object.

    I have to do it that way as a requirement for my assignment

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Did you do the thing that @Fubarable is telling you to do, though?

    If you're overriding a method, it's good practice to put @Override before the method. If your class doesn't compile, you know you've screwed something up.

  5. #5
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default

    Thanks for the help I figured out what was wrong,

    I need to pass in Object general type to both the compareTo and Equals so

    public boolean equals(Object per)
    instead of
    public boolean equals(PersonImpl per)

    when I do that though I have to cast per to PersonImpl which fulfills the requirements of my assignment.

    ((PersonImpl)per).getID()

    Thanks for the help.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I need to pass in Object general type to both the compareTo and Equals so
    As I already mentioned in my post (did you fully read it?). And actually you don't need to pass an Object type for compareTo if your class implements a generic Comparable interface:
    Java Code:
    public class PersonImpl implements Person, Comparable<PersonImpl>

  7. #7
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default

    Ok, that would be a good way to do it, (sorry I wasn't very clear in my post, I did do what you said) Thanks for the help everything is working now.

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  2. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 06:32 PM
  3. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-20-2008, 12:58 AM
  4. Why Equals method should be over ridden in Hashcode?
    By skyineyes in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 05:13 PM
  5. Replies: 0
    Last Post: 01-26-2008, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •