COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Lab Week 5 – Arrays and Classes

Posted by on August 22nd, 2013 · Uncategorized

If you didn’t finish last week’s exercises, continue on with those. In particular, make sure you understand how to use loops and arrays (i.e. the last two exercises).

Classes

Investigating the TicketMachine:

  1. Download theĀ TicketMachine project we have been building in lectures. Unzip it on your desktop and open the project with BlueJ.
  2. Create an instance of the TicketMachine class.
  3. Experiment with the addMoney() method. What happens if you:
    • Add 0 cents?
    • Add an negative amount?
  4. Is this behaviour correct?
  5. Experiment with the purchaseTicket() method. Does it work correctly? Try a number of different tests to make sure.
  6. Open up the source and place a breakpoint at the beginning of the purchaseTicket() method by clicking in the grey bar on the left. A little stop sign should appear.
  7. Call the method again and use the Debugger to step through the code.
  8. Try this with different balances to see how the execution varies.
  9. There is a bug in this method. Can you find it?

Extending the TicketMachine

Let us extend the ticket machine to keep track of the total amount of cash collected and to allow us to change ticket prices.

  1. Add a new field to the class to store the total cash collected.
  2. When does this value change? What methods will you need to modify to track these changes?
  3. Add a new accessor method getTotal() to return the value.
  4. Test your code with a variety of method calls to make sure it does the right thing.
  5. Add a new mutator method setPrice() to allow you to change the ticket price.
  6. What happens if you set the ticket price to zero? a negative number?
  7. Add a conditional statement to set-price to stop the price from being set to a negative value.

 

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.