Results 21 to 26 of 26
- 03-01-2011, 04:40 PM #21
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The reason I don't just give you the code is because this is extremely trivial for me. For you, doing it will be a good learning experience, and good practice. You were fairly close earlier, you had the class constructed properly, and simply needed to implement comparable. There is actually another thread in the forums titled "I need help understanding comparable"(or something similar to that, the exact title eludes me). This topic explains everything pretty well. Create the class, then add the comparable, then fill an arraylist in main and sort it. If you can do that you are nearly done. Do you know how to read a file?
- 03-01-2011, 05:17 PM #22
Member
- Join Date
- Feb 2011
- Posts
- 44
- Rep Power
- 0
ok, nobody wants to write code i understand it.
so now is time for theory as u like it much.
public int compareTo(CITY o) {
// TODO Auto-generated method stub
if(postalnumber < o.postalnumber){
return 0;
}
else if(postalnumber> o.postalnumber)
{
return 1;
}
return -1;
}
what does this 0 and -1 means.
I think i will have many answers bc this belongs to theory :d
thank u guys anyway;)
- 03-01-2011, 05:36 PM #23
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
From Comparable (Java Platform SE 6)
Note that since postalnumber is an int, you can simplify the whole method by just returning postalnumber - o.postalnumber. That will automatically give you a positive int if the first is greater, a negative int if it's less, and a zero if they're equal.Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
-Gary-
- 03-01-2011, 05:45 PM #24
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You could also use the Integers built in compareTo which I was trying to suggest to you earlier. I actually did just write the code, it took me about 6 minutes. You doing it yourself will definitely be better though. You are nearly done as well, you have set up the class, and you just got 2 possible solutions for implementing compareTo.
While doing city1.zip - city2.zip will work on this example be weary of using this method.
if you have 2 ints, i and j, and you implement compareTo with
You will get incorrect results if i is a large positive int and j is a large negative int. Due to overflow it will return a negative value(pulled this xplanation from page 824 of Thinking in Java by Bruce Eckel)Java Code:public int compareTo(j){ return i - j; }
Since the max possible zip will probably be 99999, and there will be no negatives, it is safe to do zip1 - zip2.Last edited by sunde887; 03-01-2011 at 05:50 PM.
- 03-01-2011, 08:00 PM #25
Member
- Join Date
- Feb 2011
- Posts
- 44
- Rep Power
- 0
to be honest i have never seen such a pretty guys, thank u for all explanations u have given me.
I really appreciate it.
Everything is clear.
- 03-01-2011, 11:02 PM #26
While that may work in this situation, in general it is not good advice. Consider the following which produces incorrect output.
Java Code:int val1 = Integer.MIN_VALUE; int val2 = 10; int comp = val1 - val2; if(comp < 0) { System.out.println(val1 + " is less than " + val2); } else if (comp > 0) { System.out.println(val1 + " is greater than " + val2); } else { System.out.println("Values are equal"); }
Similar Threads
-
assignment help
By esallender in forum New To JavaReplies: 4Last Post: 10-25-2010, 12:10 PM -
help with an assignment!
By Tek in forum New To JavaReplies: 6Last Post: 10-24-2010, 11:32 PM -
Need help with assignment! please
By runawaykinms in forum Java AppletsReplies: 2Last Post: 10-06-2010, 09:58 AM -
GUI First Assignment-DUE 8/1/08
By ljk8950 in forum AWT / SwingReplies: 2Last Post: 08-01-2008, 04:23 AM -
First GUI Assignment
By ljk8950 in forum New To JavaReplies: 1Last Post: 07-31-2008, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks