Java Reference
In-Depth Information
Self-Test Exercises
(continued)
20. Redefi ne the
equals
method of the class
SalariedEmployee
( Display 7.5 ) so
that it has a parameter of type
Object
and follows the other guidelines we gave
for an
equals
method. Assume the defi nition of the method
equals
for the
class
Employee
has been changed to be as in Display 7.10. (Remember, you
should use
getClass()
, not
instanceof
.)
21. Redefi ne the
equals
method of the class
Date
( Display 4.13 ) so that it has
a parameter of type
Object
and follows the other guidelines we gave for an
equals
method. (Remember, you should use
getClass()
, not
instanceof
. )
22. What is the output produced by the following program? (The classes
Employee
and
HourlyEmployee
were defi ned in this chapter.)
public class
Test
{
public static void
main(String[] args)
{
Employee object1 =
new
Employee();
Employee object2 =
new
HourlyEmployee();
if
(object1.getClass( ) == object2.getClass( ))
System.out.println("Same class.");
else
System.out.println("Not the same class.");
}
}
23. (This exercise requires that you have covered the starred subsection “An
Enhanced
StringTokenizer
Class *,” earlier in this chapter.)
Although we did not discuss it when we covered the class
StringTokenizer
,
the class
StringTokenizer
has a method with the following heading:
public
Object nextElement()
The method
nextElement()
returns the same string as the method
nextToken()
,
but
nextElement()
returns it as something of type
Object
, as opposed to type
String
. Give a suitable defi nition of
nextElement
to add to the defi nition of
EnhancedStringTokenizer
. This defi nition will override the defi nition of
nextElement
in the class
StringTokenizer
. (
Hint
: the defi nition is just like the
defi nition of
nextToken
except for fi xing the type of the string returned.)