Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: finding the largest object help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default finding the largest object help

    hi, i'm having trouble with this java programming problem. any help will be greatly appreciated



    Write a method that returns the largest object in an array of objects. The method signature is:

    public static Object max(Comparable[] a)

    all the objects are instances of the Comparable interface. The order of the objects in the array is determined using the compareTo method.

    Write a test program that create an array of ten strings, an array of ten integers, and an array of ten dates, and finds the largest string, integer, and date in the array.




    *i think the compareto method is what is confusing me*


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: finding the largest object help

    Basically, the compareTo() method will return -1, 0 or 1. If the comparable object using this method is less than the comparable object in the parameters, it returns -1. If the comparable object using this method is equal to the comparable object in the parameters, it returns 0. If the comparable object using this method is greater than the comparable object in the parameters, it returns 1.

    So, lets say we wanted to compare two number (this is not actual code and will not compile):
    //Returns -1
    4.compareTo(5);
     
    //Returns 0
    4.compareTo(4);
     
    //Returns 1
    4.compareTo(3);

    Do you understand that much?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: finding the largest object help

    Quote Originally Posted by aussiemcgr View Post
    Basically, the compareTo() method will return -1, 0 or 1.
    According to the Comparable API:
    "Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. "

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: finding the largest object help

    It doesn't have to be -1, 0, or 1. It just has to fall into one of these three categories: less than zero, equal to zero, or greater than zero.

    So using the compareTo() method, you're largest object compared to every other object will return a value greater than 0 (can you see why?)

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: finding the largest object help

    Quote Originally Posted by helloworld922 View Post
    It doesn't have to be -1, 0, or 1. It just has to fall into one of these three categories: less than zero, equal to zero, or greater than zero.

    So using the compareTo() method, you're largest object compared to every other object will return a value greater than 0 (can you see why?)
    Ya, you got me there. I'm just used to working with tiny numbers and I overlooked that, but you are entirely correct.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. How to find location of ipaddress using Java
    By tej in forum Java Networking
    Replies: 8
    Last Post: September 8th, 2012, 06:05 AM
  2. Finding the Average
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2010, 07:58 PM
  3. [SOLVED] Finding acm package
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 12th, 2010, 11:50 AM
  4. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  5. Replies: 13
    Last Post: May 6th, 2009, 09:27 AM