Java Basic Programming Help Please make sure this program ca
Java Basic Programming Help:
Please make sure this program can compile and run. Thanks!!
Implement a vending machine that holds cans of soda. To buy a can of soda, the customer needs toinsert a token into the machine. When the token is inserted, a can drops from the can reservoir intothe product delivery slot. The vending machine can be filled with more cans. The goal is to determinehow many cans and tokens are in the machine at any given time. What methods would you supplyfor a VendingMachine class? Give the names, parameter variables, and return types of the methods.For example I can think of insertToken, fillUp, getCanCount and getTokenCount methods.
What instance variables do the methods need to do their work? Hint: You need to track the numberof cans and tokens. Declare them with their type and access modifier.
Consider what happens when a user inserts a token into the vending machine. The number of tokensis increased, and the number of cans is decreased. Implement a method:
public void insertToken(){
// Instructions for updating the token and can counts
}
Now implement a method fillUp(int cans) to add more cans to the machine. Simply add thenumber of new cans to the can count.
Answer:
public void fillUp(int cans){
numCans = numCans + cans;
}
Next, implement two methods, getCanCount and getTokenCount, that return the current values ofthe can and token counts.
After You have implemented all methods of the VendingMachine class. Use exception handlingwherever applicablePut them together into a class, like this:
public class VendingMachine{
private your first instance variable
private your second instance variable
public your first method
public your second method
. . .
}
Solution
//VendingMachine.java
public class VendingMachine{
//instance variables
private int numCans;
private int tokens;
//constructor
public VendingMachine() {
numCans=0;
tokens=0;
}
//parameter constructor
public VendingMachine(int numCans) {
this.numCans=numCans;
tokens=0;
}
//method insertToken
public void insertToken() throws EmptyVendingMachine{
if(numCans<=0)
throw new EmptyVendingMachine(\"No cans\");
else
{
numCans=numCans-1;
tokens++;
}
}
//method that takes cans and adds to numCans
public void fillUp(int cans){
numCans = numCans + cans;
}
//Retunrs the tokens count
public int getTokenCount(){
return tokens;
}
//Returns the numCans
public int getCanCount(){
return numCans;
}
}
--------------------------------------------------------------------------------------------------------
//EmptyVendingMachine.java
public class EmptyVendingMachine extends Exception {
public EmptyVendingMachine(String msg) {
super(msg);
}
}
--------------------------------------------------------------------------------------------------------
//TestVendingMachine.java
public class TestVendingMachine {
public static void main(String[] args) throws EmptyVendingMachine {
VendingMachine vm=new VendingMachine(5);
System.out.println(\"Vending machine\");
System.out.println(\"Number of cans : \"+vm.getCanCount());
System.out.println(\"Number of tokens : \"+vm.getTokenCount());
System.out.println(\"Inserting token\");
vm.insertToken();
System.out.println(\"Number of tokens : \"+vm.getTokenCount());
System.out.println(\"Number of cans : \"+vm.getCanCount());
System.out.println(\"Filling cans 5\");
vm.fillUp(5);
System.out.println(\"Number of cans : \"+vm.getCanCount());
}
}
--------------------------------------------------------------------------------------------------------
Sample Output:
Vending machine
Number of cans : 5
Number of tokens : 0
Inserting token
Number of tokens : 1
Number of cans : 4
Filling cans 5
Number of cans : 9


