# Thread: Can somebody help me in this assignment?

1. 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?

2. 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;)

3. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
9
From Comparable (Java Platform SE 6)
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
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.

-Gary-

4. 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
Java Code:
```public int compareTo(j){
return i - j;
}```
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)

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 06:50 PM.

5. 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.

6. Originally Posted by gcalvin
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.
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");
}```

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•