• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Comparable..  RSS feed

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*Hint* Only object references can be dereferenced. Primitives cannot be dereferenced.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you give me your explanation? and what is dereference?
please explain that too...

thanks
Preetha
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i tried the above code it's not giving the sorted output.
could you please check the code you gave?

Thanks
Preetha
 
Sheriff
Posts: 5961
905
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post code.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Devaka,
Thanks for your great explanation , i am clear with it.

Preetha
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!