Skip to main content
Code Review

Return to Question

deleted 20 characters in body
Source Link

Consider a simple vending machine class. The machine accepts tokens and dispenses cans of refreshing beverages. Write a complete class (on the next page) as described below:

  • The class has two instance data fields; one to keep track of the number of cans in the machine and one to keep track of the number of tokens (think coins) collected.
  • There should be two constructors. One takes no arguments and starts with 50 cans and zero tokens. The other takes one argument, the initial number of cans in the machine.
  • There should be a method to purchase a drink which adds one token to the machine and subtracts one can – assuming there are still cans in the machine to be purchased.
  • There should be one method to add cans to the machine.
  • There should be separate methods to return the number of cans remaining and the number of tokens collected.
  • Bonus (+2 marks): Write Write a toString( ) method to allow for easy printing of a vending machine object

Consider a simple vending machine class. The machine accepts tokens and dispenses cans of refreshing beverages. Write a complete class (on the next page) as described below:

  • The class has two instance data fields; one to keep track of the number of cans in the machine and one to keep track of the number of tokens (think coins) collected.
  • There should be two constructors. One takes no arguments and starts with 50 cans and zero tokens. The other takes one argument, the initial number of cans in the machine.
  • There should be a method to purchase a drink which adds one token to the machine and subtracts one can – assuming there are still cans in the machine to be purchased.
  • There should be one method to add cans to the machine.
  • There should be separate methods to return the number of cans remaining and the number of tokens collected.
  • Bonus (+2 marks): Write a toString( ) method to allow for easy printing of a vending machine object

Consider a simple vending machine class. The machine accepts tokens and dispenses cans of refreshing beverages. Write a complete class (on the next page) as described below:

  • The class has two instance data fields; one to keep track of the number of cans in the machine and one to keep track of the number of tokens (think coins) collected.
  • There should be two constructors. One takes no arguments and starts with 50 cans and zero tokens. The other takes one argument, the initial number of cans in the machine.
  • There should be a method to purchase a drink which adds one token to the machine and subtracts one can – assuming there are still cans in the machine to be purchased.
  • There should be one method to add cans to the machine.
  • There should be separate methods to return the number of cans remaining and the number of tokens collected. Write a toString( ) method to allow for easy printing of a vending machine object
added 18 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Vending Machine class ...did I do this right?

This is the question: I am looking for ways to improve the code that I wrote for this.

Consider a simple vending machine class. The machine accepts tokens and dispenses cans of refreshing beverages. Write a complete class (on the next page) as described below:  The class has two instance data fields; one to keep track of the number of cans in the machine and one to keep track of the number of tokens (think coins) collected.  There should be two constructors. One takes no arguments and starts with 50 cans and zero tokens. The other takes one argument, the initial number of cans in the machine.  There should be a method to purchase a drink which adds one token to the machine and subtracts one can – assuming there are still cans in the machine to be purchased.  There should be one method to add cans to the machine.  There should be separate methods to return the number of cans remaining and the number of tokens collected.  Bonus (+2 marks): Write a toString( ) method to allow for easy printing of a vending machine object

Consider a simple vending machine class. The machine accepts tokens and dispenses cans of refreshing beverages. Write a complete class (on the next page) as described below:

  • The class has two instance data fields; one to keep track of the number of cans in the machine and one to keep track of the number of tokens (think coins) collected.
  • There should be two constructors. One takes no arguments and starts with 50 cans and zero tokens. The other takes one argument, the initial number of cans in the machine.
  • There should be a method to purchase a drink which adds one token to the machine and subtracts one can – assuming there are still cans in the machine to be purchased.
  • There should be one method to add cans to the machine.
  • There should be separate methods to return the number of cans remaining and the number of tokens collected.
  • Bonus (+2 marks): Write a toString( ) method to allow for easy printing of a vending machine object

Vending Machine class ...did I do this right?

This is the question: I am looking for ways to improve the code that I wrote for this.

Consider a simple vending machine class. The machine accepts tokens and dispenses cans of refreshing beverages. Write a complete class (on the next page) as described below:  The class has two instance data fields; one to keep track of the number of cans in the machine and one to keep track of the number of tokens (think coins) collected.  There should be two constructors. One takes no arguments and starts with 50 cans and zero tokens. The other takes one argument, the initial number of cans in the machine.  There should be a method to purchase a drink which adds one token to the machine and subtracts one can – assuming there are still cans in the machine to be purchased.  There should be one method to add cans to the machine.  There should be separate methods to return the number of cans remaining and the number of tokens collected.  Bonus (+2 marks): Write a toString( ) method to allow for easy printing of a vending machine object

Vending Machine class

I am looking for ways to improve the code that I wrote for this.

Consider a simple vending machine class. The machine accepts tokens and dispenses cans of refreshing beverages. Write a complete class (on the next page) as described below:

  • The class has two instance data fields; one to keep track of the number of cans in the machine and one to keep track of the number of tokens (think coins) collected.
  • There should be two constructors. One takes no arguments and starts with 50 cans and zero tokens. The other takes one argument, the initial number of cans in the machine.
  • There should be a method to purchase a drink which adds one token to the machine and subtracts one can – assuming there are still cans in the machine to be purchased.
  • There should be one method to add cans to the machine.
  • There should be separate methods to return the number of cans remaining and the number of tokens collected.
  • Bonus (+2 marks): Write a toString( ) method to allow for easy printing of a vending machine object
Source Link

Vending Machine class ...did I do this right?

This is the question: I am looking for ways to improve the code that I wrote for this.

Consider a simple vending machine class. The machine accepts tokens and dispenses cans of refreshing beverages. Write a complete class (on the next page) as described below:  The class has two instance data fields; one to keep track of the number of cans in the machine and one to keep track of the number of tokens (think coins) collected.  There should be two constructors. One takes no arguments and starts with 50 cans and zero tokens. The other takes one argument, the initial number of cans in the machine.  There should be a method to purchase a drink which adds one token to the machine and subtracts one can – assuming there are still cans in the machine to be purchased.  There should be one method to add cans to the machine.  There should be separate methods to return the number of cans remaining and the number of tokens collected.  Bonus (+2 marks): Write a toString( ) method to allow for easy printing of a vending machine object

public class Vendingmachine
{
 private int tokens;
 private int cans;
 
 public Vendingmachine()
 {
 tokens=0;
 
 cans=50;
 }
 
 public Vendingmachine( int ca)
 {
 tokens=0;
 
 cans=ca;
 
 System.out.printf(" The constructor for this %s\n", this);
 }
 public void setTokens( int coins)
 {
 tokens = coins;
 }
 
 public void setCans ( int ca)
 {
 cans = ca;
 }
 
 public void purchase( int coins)
 {
 tokens = coins;
 if (tokens >=1)
 cans = cans-tokens;
 if (cans == 0)
 addCans();
 }
 
 public int getTokens()
 {
 return tokens;
 }
 
 public int getCans()
 {
 cans = cans- tokens;
 return cans;
 }
 
 
 public void addCans()
 {
 
 cans =50;
 }
 public String toString()
 {
 String str = "You have purchased "+ cans + " cans and have entered the following amount of coins: " + tokens;
 return str;
 }
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /