File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explain Working of compareTo() method with  respect  to  this example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explain Working of compareTo() method with  respect  to  this example " Watch "Explain Working of compareTo() method with  respect  to  this example " New topic
Author

Explain Working of compareTo() method with respect to this example

saurabhthard aggarwal
Greenhorn

Joined: Nov 20, 2008
Posts: 22
/* WHEN IS COMPARETO METHOD CALLED, WHY DO WE NEED TO PUT ELEMENTS OF TREESET IN TO AN OBJECT ARRAY AND THEN TYPE CAST IT.
*/


import java.util.*;

class Comparable1
{
public static void main(String[] args)
{
TreeSet ts1=new TreeSet();

ts1.add(new Student("Saurabh","Thard",100,0.1));

ts1.add(new Student("Sachin","Tendulkar",200,0.01));

ts1.add(new Student("Prannoy","Roy",300,03.03));

ts1.add(new Student("Barkha","Dutt",400,04.4));

ts1.add(new Student("Vikram","Chandra",500,5.05));

Object [] studArray=ts1.toArray();

Student s;

for(Object obj:studArray)

{
s=(Student) obj;

System.out.printf("NAME = %s %s STUDENT ID =%d GPA=%.1f \n",s.fname(),s.lname(),
s.StudentID(),s.gpa());

}
}
}
class Student implements Comparable
{

String fname;
String lname;
int studentID;
double gpa;

public Student(String fname,String lname,int studentID,double gpa)
{

if(fname==null || lname==null || studentID==0 || gpa==0.0)

{
throw new IllegalArgumentException();
}

this.fname=fname;
this.lname=lname;
this.studentID=studentID;
this.gpa=gpa;
}

public String fname()
{
return fname;
}

public String lname()
{
return lname;
}

public int StudentID()
{
return studentID;
}

public double gpa()
{
return gpa;
}

public int compareTo(Object o) //OVER-RIDING COMPARETO SINCE IT IMPLEMENTS COMPARABLE.
{
double f= gpa- ((Student)o).gpa;

if(f==0.0)
return 0;

else if(f<0.0)
return -1;

else
return 1;
}
}



Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
please always used code tags


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
WHEN IS COMPARETO METHOD CALLED,


its is called every time you add an element to TreeSet. Since treeset by default does the sorting on the basis of comparator implemented for the class. In this case Student is object and the Student class is implementing comparator interface.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Well the compareTo method will actually be called only when add method is called on a TreeSet which has any elements. If there are no elements in the TreeSet, then the method will not be called on adding an element .

And as far as the type casting goes, you have to do that only because you didn't use generics. If you use this syntax for your declaration



Then you don't need any type casts and you can use the enhanced for loop like this



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain Working of compareTo() method with respect to this example