COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Lab Week 5 – Designing Classes

Posted by on August 24th, 2014 · Lab

The self-test quiz for this week is here.

Assignment 1

  1. If you haven’t started working on assignment 1, follow the assignment instructions to download the skeleton project.
  2. Make sure you understand what is required. If not ask your tutor.
  3. Assignment submission will be enabled later in the week.

Investigating the TicketMachine:

  1. Download theĀ TicketMachine project we’ve started looking at 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?

    Is this behaviour correct?

  4. Experiment with the purchaseTicket() method. Does it work correctly? Try a number of different tests to make sure.
  5. 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.
  6. Call the method again and use the Debugger to step through the code.
  7. Try this with different balances to see how the execution varies.
  8. 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.

Design a Class to represent an E-Book.
It should record the following information:

  • An author
  • An title
  • The number of pages
  • The current page being read.

Implement your class in the following steps:

  1. Create a class definition with an appropriate name.
  2. Add appropriate fields. What types will they have? What names will you give them?
  3. Create a constructor to initialise these fields. What starting values will you use?
  4. Create accessor methods to access these fields.
  5. Create mutator methods to allow the use to:
    • turn pages forwards and backwards
    • jump to a specific page
  6. What should happen if they try to access a page outside of the sensible range? How can you implement this?
  7. Create several different books to test your code. What happens if:
    • Your book has only one page?
    • Your book has no pages?
    • Your book has 1000000000000000000000 pages?
  8. Comment your class to describe what it is for and what each of your methods does.

 

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.