Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the following as a Java application: 1) Implement a vending machine that holds cans of soda. To buy a can of soda, the customer

Complete the following as a Java application:

1) Implement a vending machine that holds cans of soda. To buy a can of soda, the customer needs to insert a token into the machine. When the token is inserted, a can drops from the can reservoir into the product delivery slot. The vending machine can be filled with more cans. The goal is to determine how many cans and tokens are in the machine at any given time.

What methods would you supply for a VendingMachine class? Describe them informally.

2) Now translate those informal descriptions into Java method headers, such as

public void fillUp(int cans)

Give the names, parameter variables, and return types of the methods. Do not implement them yet.

3) What instance variables do the methods need to do their work? Hint: You need to track the number of cans and tokens. Declare them with their type and access modifier.

4) Consider what happens when a user inserts a token into the vending machine. The number of tokens is increased, and the number of cans is decreased. Implement a method:

public void insertToken() { // Instructions for updating the token and can counts }

You need to use the instance variables that you defined in the previous step. Be sure to check that the number of cans is greater than zero before decreasing the can count and increasing the token count.

5) Now implement a method fillUp(int cans) to add more cans to the machine. Simply add the number of new cans to the can count.

6) Next, implement two methods, getCanCount and getTokenCount, such that each returns the current values of the can and token counts, respectively.

7) You have implemented all methods of the VendingMachine class. Put 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 . . . }

8) Now complete the following tester program so that it exercises all of the methods of your class.

public class VendingMachineTester { public static void main(String[] args) { VendingMachine machine = new VendingMachine(); machine.fillUp(10); // Fill up with ten cans machine.insertToken(); machine.insertToken(); System.out.print("Token count: "); System.out.println(machine.getTokenCount()); System.out.println("Expected: ___"); // <--- COMPLETE THE OUTPUT STRING System.out.print("Can count: "); System.out.println(machine.getCanCount()); System.out.println("Expected: ___"); // <--- COMPLETE THE OUTPUT STRING } }

Build and run your completed application. Make sure the actual token and can counts match their expected counts.

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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

4. Why should we avoid use of personalised language in conflict?

Answered: 1 week ago