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
- Create a student with login name "djb" and id "859012".
- What happens when you call getLoginName() on this student?
- Why does this happen?
- Modify the getLoginName() method so that it tests the length of strings and issues a warning message
if they are too short.
- Modify it again so that instead of issuing a warning message, it uses the whole string
TicketMachine
- Open the project named basic-ticket-machine (see under chapter 2)
- Compile and create a new instance of the class TicketMachine
- Find out what the class does by testing the methods that can be accessed by right-clicking the TicketMachine
object just created.
- The TicketMachine class has several shortcomings:
- It contains no check that the customer has entered enough money to pay for a ticket.
- It does not refund any money if the customer pays too much.
- It does not check to ensure that the customer enters sensible amounts of money (e.g. 0 or negative quantities).
- It does not check that the ticket price passed to the constructor is sensible
Add appropriate conditional statements (if-then; if-then-else) to overcome these problems.
Heater
- Create a new project, heater-exercise, within
BlueJ.
- Edit the details in the project description in the text note you see in the diagram.
- Create a class, Heater, that contains a single integer field, temperature.
- Define a constructor that takes no parameters. The temperature field should be set
to the value 15 in the constructor.
- Define the mutators warmer and cooler, whose
effect is to increase or decrease the value of temperature by 5 degrees respectively.
- Define
an accessor method to return the value of temperature.