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 code 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 "code " Watch "code " New topic
Author

code

hemamalini s.
Greenhorn

Joined: Oct 17, 2005
Posts: 13
import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}
****************************why above code is giving ClassCastException?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
TreeSet sorts the elements according to the natural order of the elements.
But since the class 'Person' does not implement the 'Comparator' interface which is used to compare the elements, you are getting the class cast exception. I mean the person class is casted to the type 'Comparator'

Please correct me if im wrong.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi,

i have compiled the program ...both options 1 and 4 are wrong ...i suppose there is something wrong with the answer.....

thanks
sri


Thanks & Regards<br /> <br />-Srikanth
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
You should get errors in both the following lines

c = b; // 1
s = c * b; //4

In first line a byte variable cannot be assigned to a char because
char cannot have negative values.

In the 4th line the result of any arithmetic operation is an int , but it's assigned to a shot and hence the error.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by Srinivasa Raghavan:
You should get errors in both the following lines

c = b; // 1
s = c * b; //4

In first line a byte variable cannot be assigned to a char because
char cannot have negative values.

In the 4th line the result of any arithmetic operation is an int , but it's assigned to a shot and hence the error.


Srinivasa, Are you sure you have answered the above mentioned Question only?

I dont find anything like
c = b; // 1
s = c * b; //4
in this code.

import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}
****************************why above code is giving ClassCastException?



Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Where is the question posted by Hema ?
She has posted another question in the same thread .. I replied to it .. Also Srikanth reddy replied to it .. Now what happened to the source ?? May be she has deleted..
Sara Olsen
Ranch Hand

Joined: Oct 12, 2005
Posts: 59
Originally posted by Srinivasa Raghavan:
Where is the question posted by Hema ?
She has posted another question in the same thread .. I replied to it .. Also Srikanth reddy replied to it .. Now what happened to the source ?? May be she has deleted..


No, you answered this question first, and then it seems like you answered another question as well in this thread
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
I don't understand how s=c*b answer the question about the TreeSet...

So, concerning the TreeSet :

The ClassCastException is due to an attempt of casting a Person object to a Comparable object (not a Comparator).

A TreeSet maintains element sorted.
If the TreeSet is instanciated using a Comparator object (Using constructors : TreeSet(Comparator c) or TreeSet(SortedSet s)), this Comparator is used for comparing elements.

Otherwise, (instanciating the TreeSet with TreeSet() or TreeSet(Collection c)), elements must implements Comparable;

Look at Javadoc for the constructor TreeSet() :
Constructs a new, empty set, sorted according to the elements' natural order. All elements inserted into the set must implement the Comparable interface.


Regards,<br />Seb<br /> <br />SCJP 1.4
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Originally posted by Sara Olsen:


No, you answered this question first, and then it seems like you answered another question as well in this thread


No .. There were two questions in same thread..
Here is the question .

Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Originally posted by srikanth reddy:
hi,

i have compiled the program ...both options 1 and 4 are wrong ...i suppose there is something wrong with the answer.....

thanks
sri


The above was another reply to the question, even this is not related to TreeSet
[ October 17, 2005: Message edited by: Srinivasa Raghavan ]
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
This seems to be a mysterious thread....

Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Thats why bartenders insist not to post different questions in the same Thread. This wastes time ...
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
yes..both were supposed to be in the same thread ....

might be hema has edited i suppose......

thanks
sri
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Srinivasa Raghavan:
Thats why bartenders insist not to post different questions in the same Thread. This wastes time ...


We do, we do, many many times - but who listens?



Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
 
subject: code