| | |
Help with casting
![]() |
•
•
Join Date: Mar 2003
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Mar 2003
Posts: 6
Reputation:
Solved Threads: 0
0
#2 May 7th, 2003
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:
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.
Java Syntax (Toggle Plain Text)
/** * Compare this procedure to the one passed in as an argument. * Procedures are compared based on their names only - case is * ignored! * * @param other The Procedure object to compare this to. * * @return 0 if the names of the two procedures are equal. * a value < 0 if the name of this procedure is less than the name * of the procedure passed in. * a value > 0 if the name of this procedure is greater than the * name of the procedure passed in. */ public int compareTo( Object other ) { int retint; if( other instanceof Procedure ) { String newName = new String( (Procedure)other.getName() ); retint = name.compareToIgnoreCase(other.getName()); } return retint; }
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.
0
#3 May 7th, 2003
*bump* 'cause i suck at java
-Ryan Hoffman
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
•
•
Join Date: Mar 2003
Posts: 6
Reputation:
Solved Threads: 0
0
#4 May 8th, 2003
Well, I got it... I was missing a damn set of parentheses... 
The functional return statement looks like this:
All thanks to that extra set of parentheses around ((Procedure)other), gosh darnit. Well, I'm glad I got that figured out.

The functional return statement looks like this:
Java Syntax (Toggle Plain Text)
int retint = 0; if( other instanceof Procedure ) { retint = name.compareToIgnoreCase( ((Procedure)other).getName() ); } return retint
All thanks to that extra set of parentheses around ((Procedure)other), gosh darnit. Well, I'm glad I got that figured out.
0
#5 May 8th, 2003
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.
-Ryan Hoffman
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
![]() |
Similar Threads
- Type casting and type conversions (C)
- Type casting (C++)
- Problems casting a const char* to char* (C++)
- type casting (C++)
- casting error (Java)
Other Threads in the Java Forum
- Previous Thread: Help me... it's urgent!!
- Next Thread: Using Java To Write MMORPG's
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





