I have an assignment where I I have to write a generic version of method isEqualTo that compares its two arguments with the equals method and returns true if they are equal and false otherwise. The generic method is to be run in a program that calls isEqualTo with a variety of types, such as Integer, Double, String and Object. The program allows user input for Integer, Double, and String but Object is hard coded. This is all done in one class. I actually have 2 questions. #1 did I do it right and #2 I'm kinda confused about comparing objects since you can't have 2 objects with the same name. Is there a way to compare them other than the way I show below. Thanks in advance for any pointers or advice.
Once you add an argument list to main, this should work fine.
The equals method is the proper way to compare instances. Since different classes have different criteria for being "equal," this method should be overridden.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Mar 24, 2005
Thanks for the reply. I'm not sure I follow you though on adding an argument list to main. We use BlueJ to write and compile our programs. Once you run the program, it then uses the Scanner to prompt the user for input, not the String args Hopefully I'm on the right track with what you were trying to convey. As far as the isEqualTo method, we could only use one method so I'm not sure what you meant by "Since different classes have different criteria for being "equal," this method should be overridden." Any chance I can get you to clarify. I'm a total newbie to Java.
The JVM looks for a main method with a String array as its single argument as the entry point for launching a program. By omitting the String array, you change the main method signature and defeat the ability of the JVM to treat the file as executable.
Perhaps your environment has a different way of launching an app. I've never seen one do that, but that doesn't necessarily mean anything...
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
I'm not familiar with BlueJ, but I would be surprised if it invokes main without the signature, "pubic static void main(String args)" -- regardless of whether args is used to pass any values. But if it works...
I'm sorry I wasn't very clear with respect to the equals method. I thought you were asking a more general question, so when I said "this method should be overridden," I meant in the general context of defining classes.
The classes you're using here (String, Integer, etc.) are already defined with an overridden equals method, so this is not something you need to do.