wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparable interface. Doubt in ComparableTest class. Please find the question as comment below. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparable interface. Doubt in ComparableTest class. Please find the question as comment below." Watch "Comparable interface. Doubt in ComparableTest class. Please find the question as comment below." New topic
Author

Comparable interface. Doubt in ComparableTest class. Please find the question as comment below.

Ranjitha Serginaro
Greenhorn

Joined: Jan 10, 2013
Posts: 2
/* Pgm to display the student details after sorting them with respect to their GPA using the compareTo() method . . .*/





/*Output
Name = Dan Costello ID = 104 GPA = 2.3
Name = William Costigan ID = 102 GPA = 2.8
Name = Jamie Sullivan ID = 103 GPA = 3.6
Name = Mike Larry ID = 101 GPA = 4.0
BUILD SUCCESSFUL (total time: 0 seconds)
*/
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch Ranjitha !

Ranjitha Serginaro wrote:
Object[] studentArray=studentSet.toArray(); /* Why is 'Object' class used here instead of 'Student' class to create the 'studentArray' object.
Also what does toArray() method do?*/

becasue java.util.AbstractCollection.toArray() returns Object[] . If you want to return Student[] then you need to consider generic version - T[] toArray(T[] a)
for more detail : go through JavaDoc.

and couple of suggestion :
1.dont use double for calculation, it may loose precision in result . So, consider java.lang.Double.compare(double, double)

2.use generic collection instead of error prone raw collection .

in this way you can do clean array conversion as below code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparable interface. Doubt in ComparableTest class. Please find the question as comment below.
 
Similar Threads
So exhausted...anyone there to help?
Explain Working of compareTo() method with respect to this example
Still not getting working of compareTo() method
call second constructor?
Still trying to understand compareTo() method