Practical Exercise 2 using BlueJ

Note that this exercise is based on Barnes and Koelling, Objects First, Chapter 2.

Before starting these exercises copy (right click then save as) projects.zip to your home directory (e.g.Z:\My Documents) and unzip it.

StudentClass

  1. Create a student with login name "djb" and id "859012".
  2. What happens when you call getLoginName() on this student?
  3. Why does this happen?
  4. Modify the getLoginName() method so that it tests the length of strings and issues a warning message if they are too short.
  5. Modify it again so that instead of issuing a warning message, it uses the whole string

TicketMachine

  1. Open the project named basic-ticket-machine (see under chapter 2)
  2. Compile and create a new instance of the class TicketMachine
  3. Find out what the class does by testing the methods that can be accessed by right-clicking the TicketMachine object just created.
  4. The TicketMachine class has several shortcomings:

    Add appropriate conditional statements (if-then; if-then-else) to overcome these problems.


Heater

  1. Create a new project, heater-exercise, within BlueJ.
  2. Edit the details in the project description in the text note you see in the diagram.
  3. Create a class, Heater, that contains a single integer field, temperature.
  4. Define a constructor that takes no parameters. The temperature field should be set to the value 15 in the constructor.
  5. Define the mutators warmer and cooler, whose effect is to increase or decrease the value of temperature by 5 degrees respectively.
  6. Define an accessor method to return the value of temperature.