Java Reference
In-Depth Information
Self-Test Exercises
(continued)
20. Redefine 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 definition 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. Redefine 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 defined 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 *.”)
Although we did not cover 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 definition of
nextElement
to add to the definition of
EnhancedStringTokenizer
. This definition will override the definition of
nextElement
in the class
StringTokenizer
. (
Hint
: the definition is just like the
definition of
nextToken
except for fixing the type of the string returned.)