• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparable..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Devaka,
Thanks for your great explanation , i am clear with it.

Preetha
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic