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

Comparable..

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi , i got it from Devaka Cooray's practice exam 2.



Can anyone make this code work because i am getting errors as " i cannot be dereferenced"
this is a drag and drop question. I couldnt able to see the answers for drag and drop questios.

Thanks in advance
Preetha

( Jesper Young: Added code tags and code indentation )
[ December 15, 2008: Message edited by: Jesper Young ]
Alain Dickson
Ranch Hand

Joined: Dec 08, 2008
Posts: 53
Hi Preetha,

I have changed the to make it compile, but have not explained what I changed, just to stimulate your thinking. Please let me know if you need and explaination.



Alain
SCJP 6
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
*Hint* Only object references can be dereferenced. Primitives cannot be dereferenced.


thanks
Harvinder
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
could you give me your explanation? and what is dereference?
please explain that too...

thanks
Preetha
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi i tried the above code it's not giving the sorted output.
could you please check the code you gave?

Thanks
Preetha
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3093
    
  40

Hi Preetha,

First of all, you should know that you can't use the dot(.) operator, after a primitive variable. So, if the variable 'i' is an 'int', you can't use anything like i.xxxx This is the problem of direference.
Because of this, you have to use 'Integer', instead of 'int'.

You should be aware about the sorting mechanism. In this program, I used compareTo method to implement the business logic of Integer comparing. Please note that the comparation should be done only with the value of the variable 'i'. (Because the output should be 1,2,3,...). In your code, (and Alain's code) you are attempting to recursively compare the object 'v' with the same object 'v'. It is wrong. The actual thing we have to do is, comparing the value of the variable 'i' on current object, with the new 'v' object. Therefore the correct statement should be:

return i.compareTo(v.i);

Following is the complete program for this:



Sorry for the inconvenience you had faced, when trying to copy this drag-and-drop program from the simulator


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

Please use code tags when you post code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Devaka,
Thanks for your great explanation , i am clear with it.

Preetha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparable..