Ask your own question, for FREE!
Computer Science 81 Online
OpenStudy (anonymous):

Can someone help me with my project? I'm learning polymorphism and I think I have the code correctly but the server that checks my code says there is a bug which I can't figure out what it is. http://img138.imageshack.us/img138/3769/344c6b3a0093434690f1b02.png This is the code I have and the server is giving me link to this webpage http://goo.gl/R5Ba1

OpenStudy (asnaseer):

when defining an equals method, you cannot assume that the object passed in will be of the same type. so instead of defining the parameter of equals as type Entree, it should be of type Object. The equals method would need to check that the passed in value is of the same type as the object you are comparing it to. Also be careful as null can also be passed in.

OpenStudy (anonymous):

oh so I should be using object.equals(object)? But the type in the parameter (Entree) was given my instructor, I don't think I should change the type passed in. Is there an alternative method to fix this?

OpenStudy (anonymous):

given by*

OpenStudy (asnaseer):

I assume the class you are writing the equals method for is the Entree class?

OpenStudy (anonymous):

yeah

OpenStudy (asnaseer):

ok, so lets say you had this code: SomeOtherClass x = new SomeOtherClass(); Entree y = new Entree(); if (y.equals(x)) { ... }

OpenStudy (asnaseer):

you can see from that example that the equals method on Entree could be passed-in ANY other type of object (including a null value). do you agree?

OpenStudy (anonymous):

yeah but wouldn't this work? if(y.getName().equals(x.getName()){ ... } cause even they are different object, it's comparing the Strings of that object?

OpenStudy (asnaseer):

'x' in my example above is NOT of type Entree - so it does not have the method getName()

OpenStudy (anonymous):

then do I have to change the current object to Entree?

OpenStudy (asnaseer):

this article might help you understand: http://www.artima.com/lejava/articles/equality.html

OpenStudy (anonymous):

I still don't understand.. I cannot change the type passed in and I am not allowed to use the operator instanceof..

OpenStudy (asnaseer):

I think it might be better if you put down exactly what your teacher has asked you to do including all the restrictions.

OpenStudy (anonymous):

I finally figured it out, thanks

OpenStudy (asnaseer):

yw

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!