943,363 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5977
  • Java RSS
May 6th, 2003
0

Help with casting

Expand Post »
I'm trying to cast an Object to a type Procedure (a class I wrote) in order to access a method in the Procedure class. Problem is, Procedure is a subclass of Object, and it requires a run-time check to verify that the object that I'm trying to cast as a Procedure object is actually a Procedure object in the first place. How do I go about implementing this run-time check?

The code is an implementation of the method compareTo() in the interface Comparable. It requires that an Object be passed, so passing an actual Procedure instance is out of the question. I need to cast it back into a Procedure object from an Object.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EricR1983 is offline Offline
6 posts
since Mar 2003
May 7th, 2003
0
Re: Help with casting
Ok, I did a little research and found that I can use the instanceof operator to check if the variable that is passed is an instance of the Procedures class. Where do I go from there? Here's the block of code that I'm working with:
Java Syntax (Toggle Plain Text)
  1. /**
  2.   * Compare this procedure to the one passed in as an argument.
  3.   * Procedures are compared based on their names only - case is
  4.   * ignored!
  5.   *
  6.   * @param other The Procedure object to compare this to.
  7.   *
  8.   * @return 0 if the names of the two procedures are equal.
  9.   * a value < 0 if the name of this procedure is less than the name
  10.   * of the procedure passed in.
  11.   * a value > 0 if the name of this procedure is greater than the
  12.   * name of the procedure passed in.
  13.   */
  14.  
  15. public int compareTo( Object other ) {
  16. int retint;
  17. if( other instanceof Procedure ) {
  18. String newName = new String( (Procedure)other.getName() );
  19. retint = name.compareToIgnoreCase(other.getName());
  20. }
  21. return retint;
  22. }

The Procedure class represents a medical procedure. I'm writing it as part of a doctor's office simulation for my CS class project. Each procedure , when created, is given a name, a cost, a duration, and two boolean values that tell it whether or not it is covered by HMO Insurance and/or Private Insurance. The compareTo method in Procedure is supposed to compare the Procedure that it is called on with another Procedure that is passed in as an Object. The two Procedures are compared by name only, and case is ignored. It is supposed to return an int value that is either negative (if the first String is less than the second), positive (if the second String is less than the first), or zero (if both Strings are equal).

As far as I can tell, the String method compareToIgnoreCase will do exactly what I need done (ignoring case, return a negative, positive, or zero depending on the two Strings' relationship), once I can get the name of the 'other' Procedure object.

name is the String within this instance of Procedure that I am comparing with the String name of 'other,' which is another Procedure object.


getName() is the method that I am trying to call from within the Procedure class to get it to return the name of the Procedure 'other'. The class implements the interface Comparable, and this is as far as I've gotten. Its a bit of a hodge-podge as it is now, but if anyone could steer me in the right direction I'd appreciate it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EricR1983 is offline Offline
6 posts
since Mar 2003
May 7th, 2003
0
Re: Help with casting
*bump* 'cause i suck at java
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
May 8th, 2003
0
Re: Help with casting
Well, I got it... I was missing a damn set of parentheses...

The functional return statement looks like this:
Java Syntax (Toggle Plain Text)
  1. int retint = 0;
  2. if( other instanceof Procedure ) {
  3. retint = name.compareToIgnoreCase( ((Procedure)other).getName() );
  4. }
  5. return retint


All thanks to that extra set of parentheses around ((Procedure)other), gosh darnit. Well, I'm glad I got that figured out.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EricR1983 is offline Offline
6 posts
since Mar 2003
May 8th, 2003
0
Re: Help with casting
Stuff like that always happens to me! I've learned the best way to get past a place you are stuck is to just close it, walk away and come back the next day with a fresh mind.
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help me... it's urgent!!
Next Thread in Java Forum Timeline: Using Java To Write MMORPG's





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC