Java Reference
In-Depth Information
Our ticket machines work by customers “inserting” money into them and then requesting a
ticket to be printed. Each machine keeps a running total of the amount of money it has collected
throughout its operation. In real life, it is often the case that a ticket machine offers a selection
of different types of ticket from which customers choose the one they want. Our simplified
machines print tickets of only a single price. It turns out to be significantly more complicated to
program a class to be able to issue tickets of different values than it does to have a single price.
On the other hand, with object-oriented programming it is very easy to create multiple instances
of the class, each with its own price setting, to fulfill a need for different types of tickets.
2.1.1
Exploring the behavior of a naïve ticket machine
Open the naive-ticket-machine project in BlueJ. This project contains only one class -
TicketMachine - which you will be able to explore in a similar way to the examples we
discussed in Chapter 1. When you create a TicketMachine instance, you will be asked to
supply a number that corresponds to the price of tickets that will be issued by that particular
machine. The price is taken to be a number of cents, so a positive whole number such as 500
would be appropriate as a value to work with.
Concept:
Object creation:
Some objects
cannot be con-
structed unless
extra information
is provided.
Exercise 2.1 Create a TicketMachine object on the object bench and take a look at
its methods. You should see the following: getBalance , getPrice , insertMoney , and
printTicket . Try out the getPrice method. You should see a return value containing
the price of the tickets that was set when this object was created. Use the insertMoney
method to simulate inserting an amount of money into the machine. The machine stores as
a balance the amount of money inserted. Use getBalance to check that the machine has
kept an accurate record of the amount just inserted. You can insert several separate amounts
of money into the machine, just like you might insert multiple coins or bills into a real machine.
Try inserting the exact amount required for a ticket, and use getBalance to ensure that the
balance is increased correctly. As this is a simple machine, a ticket will not be issued automati-
cally, so once you have inserted enough money, call the printTicket method. A facsimile
ticket should be printed in the BlueJ terminal window.
Exercise 2.2 What value is returned if you get the machine's balance after it has printed a
ticket?
Exercise 2.3 Experiment with inserting different amounts of money before printing tickets.
Do you notice anything strange about the machine's behavior? What happens if you insert too
much money into the machine - do you receive any refund? What happens if you do not insert
enough and then try to print a ticket?
Exercise 2.4 Try to obtain a good understanding of a ticket machine's behavior by
interacting with it on the object bench before we start looking, in the next section, at how the
TicketMachine class is implemented.
Exercise 2.5 Create another ticket machine for tickets of a different price; remember that you
have to supply this value when you create the machine object. Buy a ticket from that machine.
Does the printed ticket look any different from those printed by the first machine?
 
Search WWH ::




Custom Search