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 3 of 3

Thread: Largest object in an array of objects

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Largest object in an array of objects

    Hello, I'm sorta new to Java and I am having issues -understanding- the assignment.
    I'm not asking for any codes, just hoping I could get some clarification on some of the requirements.

    The assignment is...

    Finding the largest object:
    Write a method that returns the largest objects in an array of objects. The method
    Signature is:
    public static Object max( Comparable[] a)
    All objects are instance of the Comparable interface. The order of objects in array is determined using compareTo method.
    Write a test program( Deriver) that creates array of ten Strings, MRects and Dates, and find the largest String, MRect and Date in the array.

    So, my question is, would it be suitable to make a class named "Object" and create the method "public static Object max( Comparable[] a)" in the class?
    Or is there already an Object class that I have yet to hear about?

    For the method, I am confuse as to why I am suppose to pass a Comparable[] as one of the arguments. Would someone be able to clarify that?

    PS: Not sure if I posted this in the right section. My apologies in advance.


  2. #2
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Largest object in an array of objects

    In my interpretation, Object is just a generic name for a class. I think you create a class with a field variable String/Date/Rectangle, that implement Comparable<CLASSNAME>. Then you need to override the method compareTo() in order to get the biggest String/Date/Rectangle. Then in that class create a method public static ClassName max(AnArrayofThisClass[] a) that returns the largest instance of the Class. I think Comparable[] a is just saying that it is an array of objects that have implemented Comparable.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Largest object in an array of objects

    Quote Originally Posted by Noct View Post
    would it be suitable to make a class named "Object"
    Or is there already an Object class that I have yet to hear about?
    java.lang.Object




    Quote Originally Posted by Noct View Post
    For the method, I am confuse as to why I am suppose to pass a Comparable[] as one of the arguments. Would someone be able to clarify that?
    Say you have object A. Object A implements the Comparable interface. This means object A knows how to compare itself to another object. When you call the method for object A, you have to give it another Comparable object to compare to. What you pass in is the other object, say B. Then object A decides if it is larger than, smaller than, or equal to B, and returns the result to where the method call was made.

    --- Update ---

    Quote Originally Posted by Noct View Post
    Not sure if I posted this in the right section. My apologies in advance.
    The "Whats wrong with my code" section is reserved for questions with code.
    Thread moved to theory and questions.

Similar Threads

  1. Finding the largest object in an array
    By ashl7 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2013, 06:57 PM
  2. 3x3 array find largest number
    By joecan2010 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 27th, 2012, 11:56 PM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. Populate an Object with Other Objects
    By EmSaint in forum Object Oriented Programming
    Replies: 2
    Last Post: October 25th, 2010, 12:45 AM
  5. finding the largest object help
    By nickypass in forum Object Oriented Programming
    Replies: 4
    Last Post: October 16th, 2010, 05:48 PM