Java Reference
In-Depth Information
21.
Yes, it is legal to add the method
doubleBalance
to the inner class
Money
because
an inner class has access to the instance variables, like
balance
of the outer class.
To test this out, add the following as a method of the outer class:
public void
test()
{
balance.doubleBalance();
}
22. It would be legal. The method
makeDeposit
is assumed to be the method
makeDeposit
of the outer class. The calling object
balance
is assumed to be the
instance variable of the outer class. These sorts of tricks can lead to confusing
code. So, use them sparingly. This is just an exercise.
23. No, a nonstatic inner class cannot have any static methods.
24. Yes, you can have a nonstatic method in a static inner class.
25.
OuterClass.InnerClass.someMethod();
26.
Outs outerObject =
new
Outs();
Outs.Ins innerObject =
outerObject.
new
Ins();
innerObject.specialMethod();
27.
You would get your first error on the following statement and it would be a
complier error:
anObject.setNumber(42);
With the change described in the exercise,
anObject
is of type
Object
and
Object
has no method named
setNumber
.
Programming Projects
Many of these Programming Projects can be solved using AW's CodeMate.
To access these please go to:
www.aw-bc.com/codemate
.
1.
Modify the recursive implementation of binary search from Chapter 11 so that
the search method works on any array of type
Comparable[]
. Test the imple-
mentation with arrays of different types to see if it works.
2.
Listed below is the skeleton for a class named
InventoryItem
. Each inventory
item has a name and a unique ID number:
class
InventoryItem
{
private
String name;
private int
uniqueItemID;
}