Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic methods/classes

 
Kewl Daemon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kewl Daemon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic