Java Reference
In-Depth Information
2.2
Examining a class definition
The exercises at the end of the previous section reveal that TicketMachine objects only
really behave in the way we expect them to if we insert exactly the correct amount of money
to match the price of a ticket. As we explore the internal details of the class in this section,
we shall see why this is so.
Take a look at the source code of the TicketMachine class by double-clicking its icon in the
class diagram within BlueJ. It should look something like Figure 2.1.
Figure 2.1
The BlueJ editor
window
The complete text of the class is shown in Code 2.1. By looking at the text of the class defini-
tion piece by piece, we can flesh out some of the object-oriented concepts that we talked about
in Chapter 1. This class definition contains many of the features of Java that we will see over
and over again, so it will pay greatly to study it carefully.
Code 2.1
The TicketMachine
class
/**
* TicketMachine models a naive ticket machine that issues
* flat-fare tickets.
* The price of a ticket is specified via the constructor.
* It is a naive machine in the sense that it trusts its users
* to insert enough money before trying to print a ticket.
* It also assumes that users enter sensible amounts.
*
 
 
Search WWH ::




Custom Search