The self-test quiz for this week is here
Task 1: Catch Up
If you haven’t completed exercises from previous labs, you may use this week’s lab to catch up on unfinished work.
If you are up-to-date, you can carry on with the following exercises.
Task 2: Return to the Ticket Machine
Go back to the ticket machine project you worked on in last week’s tut.
- Create a new class Ticket which represents a ticket.
- Create appropriate fields. What information should a Ticket carry?
- Create a constructor to initialise these fields.
- Now modify purchaseTicket on the TicketMachine class so that it creates a new Ticket object and returns it, rather than printing it.
Let’s suppose now that a ticket has 10 charges. Each time we use it, one charge is lost.
- Change your Ticket class to track charges. What changes do you need to make to do this?
Now let’s add a recharge method to the TicketMachine. It should take a Ticket as a parameter and reset its charges to 10 but at the cost of 1/10th of the ticketPrice for every charge added (if the user has enough balance). Implement this in stages:
- Make the method just recharge the Ticket for free.
- Compute how much the recharge will cost. You will need to use an accessor method on the Ticket to find out how much charge it has.
- Add a test to see if the user has enough balance.
- If there is not enough balance print an error message and do not recharge.
- Subtract the charge from the balance.
Task 3: Clock (if time permits)
Take a look at the clock project from Chapter 3 of the textbook.
- Investigate the NumberDisplay class first. How does it work?
- Look at the setValue method. What does the && operator do? This is a boolean operator. Try experimenting with it in the Code Pad.
- Look at the increment method. What does the % operator do? Try experimenting with it in the BlueJ Code Pad.
- Now take a look at the ClockDisplay class. Try to understand how it works.
- ClockDisplay has two constructors. How do they differ?
- The second constructor does not call updateDisplay. Is this a bug? Why/why not?
- Change this class to have a seconds field as well as hours and minutes.
- Change the class to show a 12 hour clock with AM/PM.
No Comments so far ↓
There are no comments yet...Kick things off by filling out the form below.
You must log in to post a comment.