This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checking equality

 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey yall, I am trying to get a method that casts in an object (x) and then uses it to compare if a marble is too big. Basically I have a diameter setup as an int and the x is an object. How whould I compare these two in this method: ( I know this method will not work, and I don't just want to compare them, I want to see if the x is > the int diameter that I have set to 5.
here is my code:
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose this should work...remarks?
if( ((Comparable)x).compareTo(x) > mySpoutDiameter)
 
Nils Hofmann
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i would solve it in this way:



nils
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Nils, but this really doesn't apply because Object x really is the parameter being passed in. I am not just putting that there because I need an object to be defined. But I did add your getDiameter method
I'm just wondering if my comparable code will work.
Thanks again
Steve
 
Nils Hofmann
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your comparable code doesn't work. you compare x with x, this will always result 0.
have a look at
Java-API
to see details about the compareTo-Method.
in the class from which x is an object of have to override the compareTo-Method and then you have to do a typcast to the class from which x is an object of, and not to the Comparable Interface.
...and why have the parameter in your willMarbleFit-Method to be from typ object?
nils
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Nils, thats just the way it is. It is a project that I am working on for class, and this method passes in an Object x, if it were up to me it would pass in an int value.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just getting an Object x with no knowledge of what type it is, it's going to be awfully tough to compare anything about it. It might be a String representation of a number, a Long or a Swing button or the System object. Check your problem description carefully to see if you know more about the Object you're being given. For example, if you know it's a Marble (even though the method signature says Object) you could cast it to Marble and ask for the diameter:

Similarly, if you know it's a String you could use Integer.parseInt((String)x) or whatever works for the real type you're getting.
In real life this would not be a real smart design. I'm guessing the assignment specified Object for the parameter just to give you a bit of a hard time and make you do the cast. Waddya think?
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throwing an IndexOutOfBoundsException in this method seems a very bizarre thing to do. Your method declares it returns a boolean, yet it never returns "false" but throws an exception instead.
It would be better to let the code that uses your willMarbleFit() method decide what to do if the marble does not fit.
David
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
steve.Wysocki,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. We'd like you to simply change that . into a space.
Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic