HTW Berlin Medieninformatik 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

What to Bring to 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.");

Post-Lab, AKA  What To Turn In

Your completed assignment, submitted in Moodle as a pdf, should include:

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

  1. Download the Ticket-Machine project and open it in BlueJ. Experiment with it like we did in the lecture and record your observations.
  2. Replace the constructor with the constructor from the pre-lab P1 and try it out. Were you right? What about the change given in P3 and P4? What happens? Record your results in your report.
  3. Is it always necessary to have a semicolon at the end of a field declaration? Experiment via the editor and record your results.
  4. If the name of 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?
  5. Write an accessor method getTotal in the TicketMachine class. The new method should return the value of the total field.
  6. Try removing the return statement from the body of getPrice. What error message do you see now when you try compiling the classes?
  7. Complete the following method, whose purpose is to subtract the value of its parameter from a field named price.
    /**
     * Reduce price by the given amount.
     */
    public void discount (int amount)
    {
    ...
    }
  8. Add a method called prompt to the 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.
  9. Add the possibility to count the number of tickets sold. Include a method for outputting how many tickets have been sold, like we did in class.
  10. Add a 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.
    where xyz should be replaced by the value held in the 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?
  11. Implement a method, 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?
  12. (For the bored) Can you make the 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.


Copyright Prof. Dr. Debora Weber-Wulff
Questions or comments: <weberwu@htw-berlin.de>
Some rights reserved. CC-BY-NC-SA - Copyright and Warranty

The exercises are adapted from Objects First with Java, A Practical Introduction Using BlueJ. David Barnes & Michael Kölling, 2016