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 compareTo() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compareTo() method" Watch "compareTo() method" New topic
Author

compareTo() method

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I have a compareTo() method now (with the help of someone from this forum). It's inside a Threat class but needs to be overidden in one of it's subclasses. It needs to be overidden in the Malware class. If both objects are malware it should compare severity (integer), then if that matches it should compare name. Here's the entire program, can anyone help?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by John Lockheart:
I have a compareTo() method now (with the help of someone from this forum). It's inside a Threat class but needs to be overidden in one of it's subclasses. It needs to be overidden in the Malware class. If both objects are malware it should compare severity (integer), then if that matches it should compare name...

//Severity is an int and can't be deferenced...
//not sure how to compare...

It sounds like you already have a good idea of what this should do. Your comments indicate that the problem is in getting and/or working with the variables. What exactly have you tried?


"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
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I need a solid if statement that says : If severity != then return comparison, if severity does equal, compare by name and return comparison

Since i'm comparing numerical values (with severity) i ran into a brickwall when trying to deference it. I tried "if this.severity == other.severity return name.compareTo(((Threat) other).name);" But I didn't know what kind of return statement to write for severity. I also had to get help with that compareTo statment I already have. Not sure how to return a comparison with the severity. Need help desperately!!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by John Lockheart:
...Not sure how to return a comparison with the severity...

According to the API for Comparable, the compareTo method "Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object."

So when this comparison is based on numeric values, it's common to simply return a difference, like...

return this.severity - other.severity;
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
Compiler can't find severity variable...need help with the code, any suggestions? How can I reference the severity variable? I thought comparing two int's would be easy.... Rest of program's uptop.

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I tried revising the code from my previous post, now I get a Class Cast exception. Just thought i'd update it, since i'm still working on it, praying for help...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

It's me, the other guy

This line:

return this.severity - ((Malware) other).severity; //Class cast exception

will, not surprisingly, throw a ClassCastException if "other" isn't an instance of Malware. This situation should be common, so this is obviously not a good idea!

Now this line

if(this == other){

ensures that the following block of code will execute if, and only if, this object is being compared to itself -- a rather boring situation, and one in which the answer (0) doesn't even need to be computed, right? You're therefore clearly asking the wrong question here.

OK, so there are two possibilities here: first, both objects are Malware or a subclass of Malware; or only "this" is, and "other" isn't. So you know the method has to look like this:


}


[Jess in Action][AskingGoodQuestions]
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
Ok but that's what i'm having problems with. Not sure how to write the code to compare severity...An int cannot be deferenced so I can't write it like I did for the name string. Now that I have the if statement and the name comparison, i'm just missing the severity comparison...tried a ton of diffrent things...

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by John Lockheart:
... Not sure how to write the code to compare severity...An int cannot be deferenced so I can't write it like I did for the name string. ...tried a ton of diffrent things...

...

//Don't know how to cast other to malware, not even sure of the code to compare severity...

I think these questions are answered in the posts above -- even with specific examples. Take another look, and show us how you're putting this together.
[ January 26, 2007: Message edited by: marc weber ]
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
If I return the difference of the severity comparisons I get a casting call error. And I cannot write it in the way I thought I could because an int cannot be deferenced. After each post in the forum i've tried to re-write the code many times and keep winding up with errors. How can I cast severity? More so, how can I even compare the severity of both objects?

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by John Lockheart:
...

If you could call compareTo on ints, what would this method do? Most likely, it would simply return the difference. So instead of trying to call this method, all you need is...
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
Here's to solutions I've tried. One executes fine, one gives a Class Cast Exception. I tried the first because I thought it was a proper if case, gives the first answer -1, then gives a class cast exception when doing the second comparison. The second solution produces all output BUT, gives one wrong answer, and a bizarre answer of 11 (which is positive so its technically right, but why 11).



John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
Ok here's the new code, I added an if exception which seems to make everything work. But i'm confused on the output. It outputs, -1, 11, -13, 0, 1. Which is all right, but why 11 and -13? Why not 1 and -1???

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Because nothing about compareTo() says that it has to return -1, 0, or 1, so implementations are free to return any positive or negative values that are convenient. String.compareTo() returns values which are often (if not always; depends on the characters) related to the difference between characters' Unicode values. For example, "a".compareTo("z") returns -25 in JDK 1.5 .
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I see...well then everything works out nicely. I also had another question. I know how to call variables in a superclass from a subclass. But what about if I wanted to call subclass variables from the superclass. Reach down the Hierarchy from the top (as apposed to reaching up from the bottom) If I created a method inside the Threat Class which were to modify, reset variables to zero, or just use them in if cases, that were inside the phishing class (extends threat), or inside the Virus (extends Malware) subclass which is a sub subclass because it's a subclass of Malware (which extends threat). How do I reach those variables? I wouldn't want to have to write the method in each class and continually override it like I would the toString() method. Any suggestions?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compareTo() method