B1 Informatics 1 WS 2023/24

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

Exercise 02: TicketMachine

     <prev next>

Old Ticket Printed Ticket. Sludge G

Source code for this exercise: https://github.com/htw-imi-info1/chapter02

Pre-lab

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

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

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

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

Assignment

Part 1: TicketMachine

Download the exercise source code from GitHub (you can download the whole chapter02 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 1 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 / as described in the textbook.
  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?

Part 2: KaraClock Preparation

update 23.10.2023: there is a new special repo for all KaraClock exercises: https://github.com/htw-imi-info1/kara-clock-lab if you just start, use this one, otherwise just continue with the first one: Use the project chapter02/kara/kara-clock-lab02 in chapter02 for these exercises.

You will need while-loops to solve these exercises, which we will discuss in class, but are not part of chapter02 in the book.

update 20.10.2023: as we did not do the while-loops today, the exercises 1. Multi-Move and 2. Count Leaves are OPTIONAL.

0. Count Leaves with act() loop and a field

(optional if you skip to 2. Count Leaves but also a good preparation for it)

  1. Open TestWorld4WithMyKara. Make Kara move up to the tree and count the leaves found on it’s way. It should move up and count one step with each call to the act() method. It should stop (by calling Greenfoot.stop();) either at the tree above or on the first free field.

    Add an accessor(getter) (public int getCount() or similar) to return the number of leaves counted so far.

1. Multi-Move

  1. copy the multiMove method we discussed in class from chapter02/kara/kara-loop-examples to DigitDisplayKara (the method definition is already there, you just need to fill it). Test it in TestWorld0 and TestWorld1. TestWorld1 contains a setup you don’t need to understand just now - just click “act” once and all Karas should be aligned in the middle like this:
Initial TestWorld1TestWorld1 after act()
TestWorld1 beforeTestWorld1 after
  1. Extend Multi-Move to move backwards: Extend the multiMove(int steps)-method to allow Kara to move backwards if a negative number is passed as parameter. It should turnAround (a method with the same name might be handy), move the appropriate number of steps and turnAround again. Hint: You can get the absolute value of a number steps using Math.abs(steps). Use TestWorld0 during the development; after you’re done test it in TestWorld2:
Initial TestWorld2TestWorld2 after act()
TestWorld1 beforeTestWorld1 after

2. Move Up And Count

  1. Open TestWorld4. complete the method public int moveUpAndCount() in DigitDisplayKara. It should move up and count the leaves. It should stop either at the tree above or on the first free field. It returns the number of leaves counted. Test your method by calling it from Kara’s context menu.
  2. When you’re done, test your method in TestWorld5, which contains some edge cases like a full row and one with zero leaves.
TestWorld5 beforeTestWorld5 outputTestWorld5 after act()
TestWorld5 beforeTestWorld5 outputTestWorld5 after

What to hand in

Hand in your lab report (pdf) and your code (zip) the night before the next lab, 10pm (22.00 Uhr). See the Labs and Exercises page.


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