File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Generic methods/classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic methods/classes" Watch "Generic methods/classes" New topic
Author

Generic methods/classes

Kewl Daemon
Greenhorn

Joined: Mar 24, 2005
Posts: 3
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.

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Kewl Daemon
Greenhorn

Joined: Mar 24, 2005
Posts: 3
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.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
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...)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
Consider Paul's rocket mass heater.
 
subject: Generic methods/classes