HTW Berlin Fachbereich 4 Internationaler Studiengang Internationale Medieninformatik (Bachelor) Info 1: Informatik I Winter Term 2022/23 |
Laboratory
1: TicketMachine
|
This week's lab work is intended to acquaint you with the BlueJ environment.
Pre-lab
Please bring these exercises printed out or written out with you to lab. Please have your name on your page.
P1. What could be wrong with the following constructor? Don't execute it, think about it in your head.
public TicketMachine (int ticketCost) { int price = ticketCost; balance = 0; total = 0; }
P2. How can you tell the difference between a method and a constructor just by looking at its header?
P3. What do you think would be printed if you altered
the fourth statement of printTicket so that price also has quotes around
it, as follows:
System.out.println("# " + "price" + " cents.");
Don't execute this, just write down your expectations.
P4. What about the following version?
System.out.println("# price cents.");
Lab assignments are due the night before your next lab at 22.00, I want you to get some sleep the night before lab. They may, of course, be turned in earlier. You hand them in by preparing the report in PDF and submitting it to Moodle.
Assignment
Ticket Machine
getBalance
is changed to getAmount
, does
the return statement in the body of the method also need to be changed
for the code to compile? Try it out within BlueJ. What does this tell
you about the name of an accessor method and the name of the field
associated with it?getTotal
in the TicketMachine
class. The
new method should return the value of the total
field. getPrice
. What error message
do you see now when you try compiling the classes?price
./**
* Reduce price by the given amount.
*/
public void discount (int amount)
{
...
}
TicketMachine
class.
This should have a void
return
type and take no parameters. The body of the method should print the
following single line of output:Please insert the correct amount of money.
showPrice
method to the TicketMachine
class. This should have a void return type and take no parameters. The
body of the method should print:The price of a ticket is xyz cents.
price
field when
the method is called. Now create two ticket machines with differently
priced tickets. Do calls to their showPrice
methods show the same output, or different? How do you explain this
effect?empty
,
that simulates the effect of removing all money from the machine. This
method should have a void
return type, and its body should simply set the total
field to zero. Does this method need to take any parameters? Test your
method by creating a machine, inserting some money, printing some
tickets, checking the total, and then emptying the machine. Is the empty
method a mutator or an accessor?better-ticket-machine
give proper change with a minimal amount of Euro coins?Writeup
You will be doing your writeup at home. Use the notes that you took in your logbook. Submit your writeup as your post-lab to the Moodle area.