[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Comparable confusion

Posted on 2007-10-19
9
Medium Priority
?
163 Views
Last Modified: 2013-11-23
Hi,

I find Comparable confusing.

In one of the notes, I read this. (The code is really lenght, so i just write the skeleton)

public class gsSort
{

public static void sort(Comparable[] a,int size)
{
int i=0;.....

for(i=0;i<size-1;i++)
int indexofsmallest=findSmallest(index,a,size);
change(i,indexofsmallest,a);
}

}
public static int findSmallest(int index, Comparable a,int size)
{

Comparable min=a[index];
int indexofmin=index;

for(int i=index+1;i<size;i++)
if(a[i].compareTo(min)<0)
{
min=a[i];
indexofmin=i;
}
return indexofmin;
}

public static void change(int i,int j,Comparable[]a)
{
Comparable temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

public class demo{

public static void main(String []args)
{

Double []d=new Double[10];

..
....
gsSort.sort(d,d.length);
...
}
}


Does the above class implements the Comparable interface ? , if yes, i thought it should have the word implement

something like public class demo implements Comparable.....

thanks

0
Comment
Question by:zizi21
  • 3
  • 3
  • 3
9 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20113631
that's exactly what it should say - implements Comparable - otherwise it's not implementing the Comparable interface
0
 

Author Comment

by:zizi21
ID: 20113799
but this code is from my lecture notes which is from a textbook and it says this" Both the Double and String classes implement the Comparable interfaces"..
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 1000 total points
ID: 20113903
Indeed String and Double implement Comparable and do have a method 'compareTo()'

The rule is: if you implement Comparable, then you MUST have a method 'compareTo()'.

;JOOP!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:basicinstinct
ID: 20113905
yes, and have you looked at the source code for the String and Double classes?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20113908
It is legal to have a method 'compareTo()' and NOT to implement Comarable.
In effect the name 'compareTo()' is a free choice.
Until you "promise" to have one.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20113912
From the official source of String.java (it is 3003 lines of code!):


....................................
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];
..........................................
    public int compareTo(String anotherString) {
      int len1 = count;
      int len2 = anotherString.count;
      int n = Math.min(len1, len2);
      char v1[] = value;
      char v2[] = anotherString.value;
      int i = offset;
      int j = anotherString.offset;

...................................

;JOOP!
0
 

Author Comment

by:zizi21
ID: 20116455
Sorry..if this is obvious.. but do you meant to say that :

Because in my main, I am using Double and String objects, the Double and String classes already has
(the word implements Comparable), I have already implemented Comparable and do not need to enforce it in my class

public class gsSort. thanks
0
 
LVL 23

Assisted Solution

by:basicinstinct
basicinstinct earned 1000 total points
ID: 20116715
Hi zizi

Short answer, 'no', your class does not implement Comparable.

The Double and String classes implement comparable.  Your class does not implement comparable just because it uses classes that do so.

You only need to implement comparable in your class if it represents some kind of object that can be compared to another.  Strings can be compared to each other in terms of alphabetical ordering.  I can't really see why your class needs to implement comparable.


0
 

Author Comment

by:zizi21
ID: 20151920
sorry..just finished two other exams..going back to java..forgive me for the late reply...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question