CECS 277
LAB ASSIGNMENT 2
Assigned date: Wednesday 9/5
Due date: Monday 9/10
20 points
Objectives
- Be Familar with classes and objects
- Be able to design a class for a given problem
- Design a functional and efficient methods
Prelab
- Read chapter 8 - Classes and objects
- Read the javadoc note posted on the class website
Write a program that simulates a vending machine. Users select a product and and provide payment. IR the payment is sufficient to cover the purchase price or the product, the product is dispensed and change is given. Otherwise, the payment is returned to the user.
Requirements:
- Create a class called CashRegister that has methods to totals up sales and compute change due.
- Create a class called MonetaryUnit that has methods to returns the name or the monetary unit and the monetary value or the unit.
- Test your designing classes by running the main methods below.
/**
This program tests the CashRegister class.
*/
public class CashRegisterTester
{
public static void main(String[] args)
{
final double NICKEL_VALUE = 0.05;
final double DIME_VALUE = 0.1;
final double QUARTER_VALUE = 0.25;
final double DOLLAR_VALUE = 1.0;
CashRegister myRegister = new CashRegister();
myRegister.recordPurchase(1.82);
myRegister.enterPayment(1, new MonetaryUnit(DOLLAR_VALUE, "dollar bill"));
myRegister.enterPayment(3, new MonetaryUnit(QUARTER_VALUE, "quarter"));
myRegister.enterPayment(2, new MonetaryUnit(NICKEL_VALUE, "nickel"));
double myChange = myRegister.giveChange();
System.out.println("Change: " + myChange);
System.out.println("Expected: 0.03");
}
}
Output
Change: 0.030000000000000027
Expected: 0.03
Grading requirements
- A print copy or your source code, and runtime output.
- Document your program with Javadoc and other comments
- Demonstrate the result to the instructor in the lab
- Submit the lab assignment to the Beachboard.