Informatics 1 WS 2014/15

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

Exercise 02: Shapes and TicketMachine

     <prev next>

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. In the lecture we have talked about data types called int and String. Java has more predefined primitive data types. What are they? Record where you found this information.

P2. What are the types of the following values (constants)?

  • 0
  • “hello”
  • 101
  • -1
  • true
  • “true”
  • “33”
  • ‘3’
  • 3.1415

P3. Pick up a book - you should have at least one at home. Is it an object or a class? If it is a class, name some objects. If it is an object, name the class. Give your reasoning.

P4. What class does the following constructor belong to? How many formal parameters does it have? What are their types?

public Book (String title, double price){ }

If you will be using your own laptop in the lab, please install BlueJ before you come to lab. BlueJ is available for all platforms - it’s Java!

Post-Lab, AKA What To Turn In

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

  • the names and roles of any collaborators in any parts of the exercise.
  • a description of what you did during the lab, including a record of how you got BlueJ started and how you produced the pdf
  • a screenshot of your scene. How did you get a screenshot produced?
  • a description of what you tried with the TicketMachine and what you think is wrong with the code given.

Lab assignments are due the night before your next lab at 23.00, I want you to get some sleep the night before the next class. They may, of course, be turned in earlier. You hand them in by preparing the report in PDF and submitting it to Moodle.

Assignment

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

  1. Open up the Figures project. Create a circle, a triangle, and two squares. Oops, where is the second square? How can you make both visible on the screen?

  2. Make all four figures have the same color. What did you have to do? Did you make any mistakes doing this?

  3. Inspect your objects. What do they have in common, where do circles, triangles and squares differ? How do the two squares differ?

  4. What is wrong with the following constructor?

     public TicketMachine (int ticketCost) {
       int price = ticketCost;
       balance = 0;
       total = 0;
     }
    
  5. Open the TicketMachine project in BlueJ. Replace the constructor with the constructor above and try it out. Were you right? Record your results in your report.

  6. Kara: Go back to the first Scenario of last Exercise. Look at the source code from Kara01 and KaraWorld01. Do these classes have constructors? If so, can you figure out what they do? (“super” calls the super class constructor).

  7. (For the bored) Can you make the TicketMachine (you’ll need one that already gives a refund - e.g. the better-ticket-machine) give proper change using Euro coins?

Writeup - what to put in the Lab Report

You will be doing your writeup at home or in the lab if time permits. Use the notes that you took in your logbook. Submit your writeup as your post-lab to the Moodle area.


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