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

Thread: Exercise 165

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Exercise 165

    Hi, so I have a question about this code. It says that if myID is equal to "t.getID" then return 0. Otherwise, it uses the "compareTo" method, within the definition of a compareTo method. How can a method be in the definition of itself?

    public class TestClass implements Comparable<TestClass> 
    { 
      private String myName; 
      private int myID; 
     
      public TestClass( String name, int id ) 
      { 
        myName = name; 
        myID = id; 
      } 
     
      public String getName() 
      { 
        return myName; 
      } 
     
      public int getID() 
      { 
        return myID; 
      } 
     
    //This section is where the editable code is. But I didn't write it. It was there to start me off I guess.:
     
    public int compareTo( TestClass t )
    {
      if ( myID == t.getID() )
        return 0;
     
      return myName.compareTo( t.getName() );
    } 
     
    //That was the first part of editable code
    //That was the part that was mysterious to me.
     
    } 
     
    public class MainClass 
    { 
      public static void main( String[] args ) 
      { 
        TestClass c1 = new TestClass( "zulima", 1 ); 
        TestClass c2 = new TestClass( "fred", 2 ); 
        TestClass c3 = new TestClass( "eric", 3 ); 
        TestClass c4 = new TestClass( "jane", 2 ); 
        TestClass c5 = new TestClass( "hermione", 1 ); 
     
    //I can also edit this line right here:
     
    fSystem.out.println( c1.compareTo( c2 ) );
     
      } 
    }

    I'm also looking for a good explanation of what the "compareTo" method is trying to do, other than return 0. I mean, I know its supposed to compare but, how does it compare conceptually.

    Thanks for the help. Sincerely, ghostheadx


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,760
    My Mood
    Mellow
    Thanks
    233
    Thanked 712 Times in 698 Posts

    Default Re: Exercise 165

    How can a method be in the definition of itself?
    The TestClass.compareTo() and String.compareTo() methods are not the same.

    You can define what the compareTo() method does based on what you decide <, == (equals()), and > mean for two TestClass objects. You can count dirtySocks, tiresOnAxels, compare bodyTemps whatever you'd like.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 165

    So one compareTo() method can be used in the definition of the other. How is the inner method defined then? I mean originally.

    --- Update ---

    I don't completely understand the exercise. What am I supposed to do? I don't get the instructions.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,760
    My Mood
    Mellow
    Thanks
    233
    Thanked 712 Times in 698 Posts

    Default Re: Exercise 165

    How is the inner method defined then?
    This is a lazy question. Refer to the String API.

    I don't get the instructions.
    What don't you understand? You might try searching for 'java compareto' to find other examples or tutorials to help you understand. If you've made the effort but can't find total consciousness, come back with specific questions with more clarity than, "I don't get it."

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 09:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 07:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 10:31 PM
  4. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 08:40 PM
  5. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM

Tags for this Thread