• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparable..

 
Preethi Dev
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 ]
 
Alain Dickson
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
 
Harvinder Thakur
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
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4594
302
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
 
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic