Assignment # 1 –
Problem 22
described on page 247 of “Computing with Java”
Write a coffee vending machine class. Include fields giving the number of cups of coffee available, the cost of one cup of coffee, and the total amount of money inserted by the user. This machine requires exact change. Include one constructor which stocks the machine with a quantity and price of coffee specified as parameters, and another with no parameters which stocks the machine with 10 cups of coffee at fifty cents each. Include the following methods:
menu( ) |
// displays the quantity and
price of coffee |
insert(int quarters, int dimes, int nickels) |
// inserts the given amount |
select( ) |
// dispenses a cup of coffee
if user has inserted enough // money and coffee is
available, otherwise displays a // message // use return code of 1 if a
cup of coffee is dispensed // use return code of 0 if a
cup of coffee is not dispensed |
refund( ) |
// returns the money inserted |
Write a main method to create some vending machines and test their operations.
A sample test driver for the vending machine class is below:
// the following program should be in source file Main.java
public class Main {
public static void main (String [] args) {
int cup_of_coffee;
VendingMachine vm1 = new
VendingMachine(3,65);
System.out.println(vm1);
vm1.menu();
vm1.insert(2,1,1);
cup_of_coffee = vm1.select();
if (cup_of_coffee == 0)
vm1.refund();
vm1.insert(1,1,6);
cup_of_coffee = vm1.select();
if (cup_of_coffee == 0)
vm1.refund();
vm1.insert(3,1,1);
cup_of_coffee = vm1.select();
if (cup_of_coffee == 0)
vm1.refund();
vm1.insert(1,4,0);
cup_of_coffee = vm1.select();
if (cup_of_coffee == 0)
vm1.refund();
vm1.insert(2,1,1);
cup_of_coffee = vm1.select();
if (cup_of_coffee == 0)
vm1.refund();
System.out.println(vm1);
}
}
Notes
about the toString( ):
System.out.println(vm1);
Activates toString( ) to print the contents of the vending machine.
An
example of program output.
Vending
machine contains $0.00
0
quarters
0
dimes
0 nickels
3 cups of coffee
3 cups are available. A cup costs 65
cents.
Coffee is not dispensed unless exact
change is deposited.
Coffee is not dispensed because no coffee is
available.
Vending machine contains $1.95
4
quarters
6
dimes
7 nickels
0 cups of coffee
////////////////////////////////////////////////////////////////////////
Test
your program for correctness and accuracy, prior to submitting it for grading.
Only
send the *.java file for grading
All assignments MUST be received as email prior to the class session due date.
· Email sent to jpetlick@condor.depaul.edu
NOTE: When sending this e-mail assignment be sure to use the following header format or else the email assignment will NOT be graded: To: jpetlick@condor.depaul.edu |