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:
Joined: Mar 02, 2003
I suppose this should work...remarks? if( ((Comparable)x).compareTo(x) > mySpoutDiameter)
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
Joined: Jan 10, 2004
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
Joined: Mar 02, 2003
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.
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?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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
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!