Java Reference
In-Depth Information
Answers to Self-Test Exercises
1.
mustard 1
hot dogs 12
apple pie 1
2.
This method has been added to the class
LinkedList1
on the accompanying
CD.
public
boolean
isEmpty( )
{
return
(head ==
null
);
}
3.
This method has been added to the class
LinkedList1
on the accompanying
CD.
public
void
clear( )
{
head
=
null
;
}
If you defined your method to remove all nodes using the
deleteHeadNode
method, your method is doing wasted work.
4.
Yes. If we make the inner class
Node
a public inner class, it could be used outside
the definition of
LinkedList2
, whereas leaving it as private means it cannot be
used outside the definition of
LinkedList2
.
5.
It would make no difference. Within the definition of an outer class there is full
access to the members of an inner class whatever the inner class member's access
modifier is. To put it another way, inside the private inner class
Node
, the modi-
fiers
private
and
package access
are equivalent to
public
.
6.
Because the outer class has direct access to the instance variables of the inner class
Node
, no access or mutator methods are needed for
Node
.
7.
It would be legal, but it would be pretty much a useless method, because you
cannot use the type
Node
outside of the class
LinkedList2
. For example, out-
side of the class
LinkedList2
, the following is illegal (
listObject
is of type
LinkedList2
):
Node v = listObject.startNode( );
//Illegal
whereas the following would be legal outside of the class
LinkedList2
(although
it's hard to think of anyplace you might use it):
Object v = listObject.startNode( );