Question

JAVA In this lab, you will implement a vending machinethat holds cans of soda. To buy...

JAVA

In this lab, you will implement a vending machinethat 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.

Instances of a class with no constructor are always constructed with all instance variables set to zero (or nullif they are object references). It is always a good idea to provide an explicit constructor.

Provide two constructors for the VendingMachineclass:

a) A constructor with no arguments that initializes the vending machine with 10 soda cans

b) A constructor, VendingMachine(int cans), that initializes the vending machine with the given number of cans

Both constructors should initialize the token count to 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

VendingMachine.java

public class VendingMachine {
private int cans, tokens;
  
// default constructor
public VendingMachine()
{
this.cans = 10;
this.tokens = 0;
}

// explicit constructor
public VendingMachine(int cans)
{
this.cans = cans;
this.tokens = 0;
}
  
private void refill()
{
this.cans = 10;
System.out.println("The Vending Machine is refilled with 10 cans.");
}
  
public void buyCan()
{
this.tokens++;
this.cans--;
if(this.cans == 0)
refill();
}

public int getCans() {
return cans;
}

public int getTokens() {
return tokens;
}
  
@Override
public String toString()
{
return("Number of tokens: " + this.tokens + ", Number of cans: " + this.cans);
}
}

VendingMachineTest.java (Driver class)

public class VendingMachineTest {
  
public static void main(String[] args)
{
VendingMachine machine = new VendingMachine(20);
  
System.out.println("INITIAL STATE:\n" + machine.toString());
  
// initiate to buy 15 cans
System.out.println("\nAttempting to buy 15 cans...");
for(int i = 0; i < 15; i++)
{
machine.buyCan();
}
// display the number of cans and tokens inside the machine
System.out.println("\nAFTER BUYING 15 CANS:\n" + machine.toString());
  
// again buy 10 cans
System.out.println("\nAttempting to buy 10 cans...");
for(int i = 0; i < 10; i++)
{
machine.buyCan();
}
// display the number of cans and tokens inside the machine
System.out.println("\nAFTER BUYING 10 CANS:\n" + machine.toString());
}
}

*************************************************************** SCREENSHOT ************************************************************

Add a comment
Know the answer?
Add Answer to:
JAVA In this lab, you will implement a vending machinethat holds cans of soda. To buy...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code in JAVA You are asked to implement “Connect 4” which is a two player game....

    Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The...

    C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT