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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!