Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

Decision Making in Groups Leadership in Meetings

Answered: 1 week ago