COMP 1113 Lab 5

Goals

The goals of this lab are

  1. Implement a class, following the process in How To 3.1: Implementing a class.

Instructions

Work on this lab in your registered lab section. Answer the exercises in the Acorn COMP 1113A1 Lab 05. Note that the Acorn quiz closes Friday, 19 February 2010, 5:00 PM.

Part 1: Preparation

Read section How To 3.1: Implementing a class of the text.

Part 2: Design, Implement, Test

Your task is to design, implement, and test a VendingMachine class.

Step 1: Find out which methods you are asked to supply.

In this lab, you will implement a vending machine. The vending machine holds cans of soda. To buy a can of soda, the customer needs to insert a token into the machine. When the token is inserted, a can drops from the can reservoir into the product delivery slot. The vending machine can be filled with more cans. The goal is to determine how many cans and tokens are in the machine at any given time.

Exercise 1
What methods would you supply for a VendingMachine class? Describe them informally.

Step 2: Specify the public interface.

Now translate those informal descriptions into Java method signatures, such as

public void fillUp(int cans)

Exercise 2
Give the access specifiers, names, parameters, and return types of the methods. Do not implement them yet.

Step 3: Document the public interface.

Exercise 3

Write and compile the definition of class VendingMachine, with an appropriate Javadoc comment for the class, and an empty class body. Enter your source code for class VendingMachine here.

Documentation Test: Use the Javadoc tool to generate the API documentation for your class, and browse the documentation. To do this in BlueJ, select Project window, menu item Tools -> Project Documentation. You can also do this at the command line (man javadoc). Make sure that the documentation agrees with what you intended to write as comments.

Style Test: It is very important to follow a set of coding conventions when you write your code. Use the checkstyle tool to confirm that you are following the coding conventions for this course. The checkstyle tool can also reveal some potential bugs such as hiding a field. It is a good idea to check the style of your source code frequently.

Exercise 4
For each method of class VendingMachine, add a "method stub" and an appropriate Javadoc comment for the method. The stub for a method that returns a number should return 0. Compile the class. Enter your source code for class VendingMachine here.

Repeat the Documentation and Style Tests.

Step 6: Test your class.

Yes, this step is out of order. Test-Driven Development (TDD) is a software development process in which we write the tests before writing the code that must pass the tests. At the moment, we are testing that class VendingMachine has the required API. The test program also reports actual and expected effects of invoking constructors and methods of the API. This directs what the programmer(s) of the class must achieve - the actual and expected effects must agree.

Here is a simple test program for class VendingMachine:

  public class VendingMachineTester
  {
     public static void main(String[] args)
     {
        VendingMachine machine = new VendingMachine();
        machine.fillUp(10); // fill up with ten cans
        machine.insertToken();
        machine.insertToken();
        System.out.print("Token count: ");
        System.out.println(machine.getTokenCount());
        System.out.println("Expected: 2");
        System.out.print("Can count: ");
        System.out.println(machine.getCanCount());
        System.out.println("Expected: 8");
     }
  }
Exercise 5
Run the test program. What is the output?

Step 4: Determine instance fields.

Exercise 6
What instance fields would you supply? Hint: You need to track the number of cans and tokens.

Step 5: Implement constructors and methods.

As noted in How To 3.1: Implementing a class, implement the methods one at a time, starting with the easiest first.

Exercise 7
Implement and test the methods of class VendingMachine. Enter your source code for class VendingMachine here.

The VendingMachine class does not have any constructors. Instances of a class with no constructor are always constructed with all instance variables set to zero (or null if they are object references). It is always a good idea to provide an explicit constructor.

You will provide two constructors for the VendingMachine class:

Both constructors should initialize the token count to 0.

Starting with the (simplest) default constructor, develop the code for each constructor separately, following these steps:

  1. Write a stub for the constructor. The stub will have an empty body.
  2. Write the Javadoc comment for the constructor. Generate and browse the documentation for the class.
  3. Update the test program appropriately. To test the default constructor, modify the expected results as necessary. To test the one parameter constructor, write another test program that is similar to the given one, except it invokes the new constructor, with required modifications to the expected results.
  4. Run the test program.
  5. Write the statement(s) of the constructor's body.
  6. Repeat the two previous steps until the code passes the test.
Exercise 8
Enter your final source code for class VendingMachine.

Last Modified: 2010-02-11.