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, such as 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
Visit www . myprogramminglab . com to complete select exercises online and get instant
feedback .
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 implementa-
tion with arrays of different types to see if it works.
2. Listed next is the skeleton for a class named InventoryItem . Each inventory item
has a name and a unique ID number:
VideoNote
Solution to
Programming
Project 13.1
class InventoryItem
{
private String name;
private int uniqueItemID;
}
Flesh out the class with appropriate accessors, constructors, and mutatators.
The uniqueItemID 's are assigned by your store and can be set from outside
 
Search WWH ::




Custom Search