The goals of this lab are
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.
Read section How To 3.1: Implementing a class of the text.
Your task is to design, implement, and test a VendingMachine
class.
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.
|
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. |
Exercise 3
|
Write and compile the definition of class |
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.
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? |
Exercise 6
|
What instance fields would you supply? Hint: You need to track the number of cans and tokens. |
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:
VendingMachine(int cans)
that initializes the vending machine with the given number of cans
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:
Exercise 8
|
Enter your final source code for class VendingMachine .
|
Last Modified: 2010-02-11.