Results 1 to 7 of 7
- 01-21-2009, 06:34 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 37
- Rep Power
- 0
How to override Equals method with a Java File
I have a file which I named myDriver.java which looks like this:
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: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"); } }
package cs235.java;
Instead it uses the default equals method.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; }
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.
-
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.
- 01-21-2009, 08:07 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 37
- Rep Power
- 0
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
So I need it to work like this: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); }
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
- 01-21-2009, 09:12 PM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.Neil Coffey
Javamex - Java tutorials and performance info
- 01-21-2009, 11:41 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 37
- Rep Power
- 0
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.
-
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:I need to pass in Object general type to both the compareTo and Equals so
Java Code:public class PersonImpl implements Person, Comparable<PersonImpl>
- 01-22-2009, 03:25 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
Creating a new equals() method help
By Dave0703 in forum New To JavaReplies: 2Last Post: 09-21-2008, 05:32 PM -
Object class's equals() method behavior????
By skyineyes in forum New To JavaReplies: 4Last Post: 07-19-2008, 11:58 PM -
Why Equals method should be over ridden in Hashcode?
By skyineyes in forum New To JavaReplies: 1Last Post: 05-26-2008, 04:13 PM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-25-2008, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks