Java Reference
In-Depth Information
Note: As soon as we implement a class of bags in the next chapters, you can actually run
the programs shown in the previous listings. You just need to reconcile the class name Bag
that these examples use with the names of the classes in the next chapters.
Question 8 Consider the program in Listing 1-4. After creating the instance myBank of the
class PiggyBank , suppose that we add several unknown coins to myBank . Write some code that
will remove coins from the bank until either you remove a penny or the bank becomes empty.
Using an ADT Is Like Using a Vending Machine
1.19
Imagine that you are in front of a vending machine, as Figure 1-3 depicts; or better yet, take a break
and go buy something from one!
FIGURE 1-3
A vending machine
I'm really thirsty —
what looks good?
When you look at the front of a vending machine, you see its interface. By inserting coins
and pressing buttons, you are able to make a purchase. Here are some observations that we can
make about the vending machine:
You can perform only the specific tasks that the machine's interface presents to you.
You must understand these tasks—that is, you must know what to do to buy a soda.
You cannot access the inside of the machine, because a locked shell encapsulates it.
You can use the machine even though you do not know what happens inside.
If someone replaced the machine's inner mechanism with an improved version, leaving the
interface unchanged, you could still use the machine in the same way.
 
 
 
Search WWH ::




Custom Search