• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with compareTo()

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class Employee implements Comparable
{
int EmpID;
String Ename;
double Sal;
static int i;

public Employee(String ename, double sal) {
EmpID = i++;
Ename = ename;
Sal = sal;
}
public String toString() {
return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal" + Sal;
}

public int compareTo(Employee o1) {
if (this.Sal == o1.Sal)
return 0;
else if ((this.Sal) > o1.Sal)
return 1;
else
return -1;
}

}


public class ComparableDemo{

public static void main(String[] args) {

List<Employee> ts1 = new ArrayList<Employee>();
ts1.add(new Employee ("Tom",40000.00));
ts1.add(new Employee ("Harry",20000.00));
ts1.add(new Employee ("Maggie",50000.00));
ts1.add(new Employee ("Chris",70000.00));
Collections.sort(ts1);
Iterator itr = ts1.iterator();

while(itr.hasNext()){
Object element = itr.next();
System.out.println(element.toString() + "\n");

}

}
}



In the above program i am getting problem to understand the use of sort() and compareTo() method.How are they linked each other.
In this case i am getting one compilation error*******

C:\jdk1.5.0\bin>javac ComparableDemo.java
ComparableDemo.java:2: Employee is not abstract and does not override abstract m
ethod compareTo(java.lang.Object) in java.lang.Comparable
class Employee implements Comparable
^
Note: ComparableDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

But as i know i have done correct implementation of compareTo() according to it's signature.
One more thing i want to ask that how toString() method is invoked even it is not invoked explicitly.
Thanks
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

you did not implement the correct compareTo method, you used a different signature (the argument should be Object), so in fact you've only overloaded the method.

To compile, you still need to implement the method with the exact same signature as specified in Comparable.

Regards,
Alex
[ February 15, 2008: Message edited by: Alex Belisle Turcot ]
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alex one more thing i'll ask that
how toString() method is invoked even it is not invoked explicitly
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Bahubal:
Thanks Alex one more thing i'll ask that
how toString() method is invoked even it is not invoked explicitly



No magic.. if we take System.out.println for example
When you provide an object as argument, this method will call toString on your object.

All objects have the toString method, inherited from Object.

Regards,
Alex
[ February 15, 2008: Message edited by: Alex Belisle Turcot ]
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alex i got the point.
But regarding the prvious one (compareTo())...i want to add one more thing that i could be write if i's using latest jdk. As in K & B it given on page 552 -Override compareTo() taking argument of the type you are sorting.
I think it is possible with the help of generics.What do you think....
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Bahubal:
Thanks Alex i got the point.
But regarding the prvious one (compareTo())...i want to add one more thing that i could be write if i's using latest jdk. As in K & B it given on page 552 -Override compareTo() taking argument of the type you are sorting.
I think it is possible with the help of generics.What do you think....



Yes sorry I missed that.. (I did the programmer certification with 1.4...


You could instead Implement Comparable and specify your Employee Object:



Then, you original compareTo is OK by itself.

Regards,
Alex
[ February 16, 2008: Message edited by: Alex Belisle Turcot ]
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your previous replies.

public int compareTo(Employee o1) {
if (this.Sal == o1.Sal)
return 0;
else if ((this.Sal) > o1.Sal)
return 1;
else
return -1;
}
In this piece of code of the same program i want to know, how the this.sal and o1.sal are different.when an object is initialized ,gives value to this.sal.But what and from where o1.sal is getting value???
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Bahubal:
Thanks for your previous replies.

public int compareTo(Employee o1) {
if (this.Sal == o1.Sal)
return 0;
else if ((this.Sal) > o1.Sal)
return 1;
else
return -1;
}
In this piece of code of the same program i want to know, how the this.sal and o1.sal are different.when an object is initialized ,gives value to this.sal.But what and from where o1.sal is getting value???



After you filled your list with Employees, sort() will do the rest.

For example, sort() would call compareTo() on object #1, giving as argument object #2. The result of your compareTo() would tell sort() what order these 2 should be. After calling compareTo() on the entire list, your objects will be in order.

More specifically, this.sal refer to the current object and o1.sal to the Employee received as argument. The logic to sort and call your object's compareTo() is inside sort()

Regards,
Alex
[ February 16, 2008: Message edited by: Alex Belisle Turcot ]
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alex to sort out my doubts.
 
reply
    Bookmark Topic Watch Topic
  • New Topic