Informatics 1 SoSe 2021

Website of Prof. Dr. Barne Kleinen, Professor for Media Informatics (Bachelor/Master) at HTW Berlin

Exercise 02: TicketMachine

     <prev next>

Book Printed Ticket. Sludge G

Pre-lab

0. 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;
}

1. How can you tell the difference between a method and a constructor just by looking at its header?

2. 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.

3. What about the following version? System.out.println("# price cents.");

Assignment

Download the exercise source code from GitHub (you can download the whole exercise02 repository as a zip here).

  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 pre-lab exercise 0 and try it out. Were you right? What about the change given in pre lab exercises 2 and 3? 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)
{
...
}
  1. 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.
  2. 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.
  3. Add a printPrice 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 printPrice methods show the same output, or different? How do you explain this effect?
  1. (For the bored) Can you make the better-ticket-machine give proper change with a minimal amount of Euro coins?

What to hand in

Hand in your lab report until April 22, 10pm (22.00 Uhr). See the Labs and Exercises page.


Adapted from https://people.f4.htw-berlin.de/~weberwu/info1/Labs/Lab1.shtml , which was itself adapted from Objects First with Java, A Practical Introduction Using BlueJ. David Barnes & Michael Kölling, 2009