Question
how to add these two constructors 1) A default constructor, VendingMachine(), that initializes the vending machine with 10 soda cans 2) A constructor, VendingMachine(int cans),
how to add these two constructors
1) A default constructor, VendingMachine(), that initializes the vending machine with 10 soda cans
2) A constructor, VendingMachine(int cans), that initializes the vending machine with the given number of cans
my codes:
public class VendingMachine {
public int canCount; public int tokenCount;
public void add(int cans) { canCount = canCount + cans; }
public boolean insertToken() { if (canCount <= 0) { return false; } else { canCount = canCount - 1; tokenCount = tokenCount + 1; } return true; }
public int getCanCount() { return canCount; }
public int getTokenCount() { return tokenCount; } }
tester code:
public class VendingMachineTester {
/** * @param args the command line arguments */ public static void main(String[] args) { VendingMachine machine = new VendingMachine(); machine.add(10); boolean status; machine.insertToken(); status = machine.insertToken(); System.out.println("Status " + status); System.out.println("Expected: true"); 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"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started