Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #22
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    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. #23
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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. #24
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  5. #25
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    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. #26
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by gcalvin View Post
    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 FirstFirst 12

Similar Threads

  1. assignment help
    By esallender in forum New To Java
    Replies: 4
    Last Post: 10-25-2010, 12:10 PM
  2. help with an assignment!
    By Tek in forum New To Java
    Replies: 6
    Last Post: 10-24-2010, 11:32 PM
  3. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 09:58 AM
  4. GUI First Assignment-DUE 8/1/08
    By ljk8950 in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2008, 04:23 AM
  5. First GUI Assignment
    By ljk8950 in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 07:29 AM

Posting Permissions

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