Question

In: Computer Science

1-You want to dispense an item from the vending machine. An item in the machine can...

1-You want to dispense an item from the vending machine. An item in the machine can cost between 25 cents and a dollar, in 5 cent increments (25,30,35,,40,45......,90,95,100) and the machine accepts only a single dollar bill to pay for the item.
Write a java class called ChangeMaker that will determine the change to be dispensed from a vending machine. Use the following UML diagram.

ChangeMaker
       Attributes
   private double money
   private int numberOfQuarters
   private int numberOfDimes
   private in numberofNickels
      
       Operations
   public ChangeMaker()
   public double getMoney()
public void setMoney(double newMoney)
   public void calculateChange()
public void getInput(Scanner scanner)
   public String toString()


The names of the variable and methods indicate their functionality.
Use the calculateChange method to calculate the number of quarters,dimes and nickels.
Use the getInput method to get the input from user.
In a tester class that holds a main method, perform the following tasks.
-Create a ChangeMaker object
-Create a Scanner object
-Use the appropriate method to take user input
Please note that I've included a toString() method which is a standard method that returns a string to describe the class.
-Your output should be similar to this.

Enter price of item (from 10 cents to a dollar, in 5-cent increments): 65
You bought an item for 65.0 cents and gave me a dollar, so your change is 1 quarter(s), 1 dime(s), and 0 nickel(s).

Hint: Calculate the leftover and cast this number from type double to int, then use the integer division and remainder operator to determine the number of coins of each type.

Solutions

Expert Solution

Answer: Hey!! Kindly finds your solution below. Let me know if any issue.Thanks.

Copy to Code: This program has two classes changemaker and Main class. changeMaker class has all methods implementation. In main, call all methods first it will take input from user and calculate change ,disply it on the screen.

import java.io.*;
import java.util.Scanner;
class changeMaker//class changeMaker
{
   double money;//data members of a class
int numberOfQuarters;
   int numberOfDimes;
int numberofNickels;
  
   public changeMaker()//constructor
   {
   }
   public void getInput(Scanner sc)//to take input
   {
       System.out.println("Enter price of an item: ");
       money = sc.nextDouble();
   }
   public void setMoney(double newMoney)//setter
   {
       money = newMoney;
   }
   public double getMoney()//getter
   {
       return money;
   }
   public void calculateChange()//calculate change
   {
   int cents = 100;//value of 1 dollar
   money = cents-money; //left over cents
       do{
if ((int)money >= 25) //calculate quarters
{
numberOfQuarters =(int)money / 25; // How many quarters we can get?
money =(int) money % 25;
}
else if ((int)money >= 10)
{
numberOfDimes =(int) money /10; // How many dimes we can get?
money = (int)money % 10;
}
else if ((int)money >= 5)
{
numberofNickels = (int)money /5; // How many nickels we can get?
money = (int)money % 5;
}
else
{
money = (int)money; // How many cents we'd have left?
}
}while ((int)money != 0);
}
public String toString()//override toString()
{
   return String.format("Quarters: "+numberOfQuarters + "\nDimes: "+numberOfDimes+"\nNickels: "+numberofNickels);
}  
}      
class Main//Main class
{
   public static void main(String []args)
   {
      
       Scanner sc = new Scanner(System.in);//Scanner object
       changeMaker ch = new changeMaker();//create object of class
       ch.getInput(sc);//call input method
       ch.calculateChange();//call calculateChange()
       System.out.println(ch.toString());//toString()
   }
}


Related Solutions

Write a program that determines the change to be dispensed from a vending machine. An item...
Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and 1 dollar, in 5-cents increments (25, 30, 35, . . . 90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. (Explain with algorithms if you can please!)
Using a vending machine requires instructions from the person buying an item and the machine dispensing...
Using a vending machine requires instructions from the person buying an item and the machine dispensing the item. Using terms and concepts learned this week, discuss the following activities. What actions by either the person buying an item or the vending machine would you describe as a privilege or non-privileged instructions? Why? Relate memory management concepts (virtual, real) to the operation of the vending machine. Relate processes and threads to the operation of the vending machine. Directly address each question...
A soda vending machine is supposed to dispense 7 fluid ounces of soda into a cup....
A soda vending machine is supposed to dispense 7 fluid ounces of soda into a cup. The canteen manager tested the machine by taking a sample of 20 cups of soda. He measured the 20 cups of soda individually and found that the measurements had a mean of 8.08 fl. oz. and a standard deviation of 2.85 fl. oz. Determine if the machine dispense is different from the required ounces set by the company. (a) State the null hypothesis and...
Coca-Cola years ago was experimenting with a vending machine that would dispense their products according to...
Coca-Cola years ago was experimenting with a vending machine that would dispense their products according to the weather, specifically the ambient air temperature. Also considered was whether the product was demanded during off season when there is less traffic. In one study, assuming 200,000 “smart” vending machines were in place, the incremental (marginal) profit associated with the smart vending machine was estimated at $328.5 million per year. In fact, the CEO of Coca-Cola was quoted as stating “In a final...
When the price of a soda from the campus vending machine was $0.50 per can, 100...
When the price of a soda from the campus vending machine was $0.50 per can, 100 cans were sold each day. After the price increased to $0.60 per can, sales dropped to 80 cans per day. Over this range, the absolute price elasticity of demand for soft drinks was approximately equal to A. 1.00 B. 2.00 C. 1.47 D. 1.22 1 points    QUESTION 9 Luna is a manufacturer of fashion jewelry. The CEO of Luna makes sure that the...
1.You find a quarter in the vending machine at work. Now you have to check the...
1.You find a quarter in the vending machine at work. Now you have to check the vending machine every day now. This is an example of ___. a. operant conditioning with positive reenforcement b. classical conditioning with positive reenforcemenet c. operant conditioning with negative reenforcement d. classical conditioning with negative reenforcement 2. Every time you answer a question correctly in class, I praise you. This is an example of which learning theory? a. classical conditioning b. observational learning c. operant...
You are in the train station waiting for your train to arrive. You see a vending machine from where you can buy snacks and chips for $3.50.
  You are in the train station waiting for your train to arrive. You see a vending machine from where you can buy snacks and chips for $3.50. Posted on the machine is a sign saying: “Property of XYZ Vendsolutions Pty Ltd”. You insert the coins required into the machine and out comes a packet of chips. However, when you open the packet of chips, you find bits of plastic inside and you don’t want to eat the chips. You...
Sam is considering purchase of a vending machine to sell sodas. The cost of the vending...
Sam is considering purchase of a vending machine to sell sodas. The cost of the vending machine is $3,400. Sam estimates that the vending machine will last for five years and will provide net income of $800 each year for its lifetime. a. If Sam pays $3,400 for the vending machine today, what is its net present value at 7%? Should Sam purchase the vending machine? b. If Sam pays $3,400 for the vending machine today, what is its net...
Coding Java Vending machine Lab
Given two integers as user inputs that represent the number of drinks to buy and the number of bottles to restock, create a VendingMachine object that performs the following operations:Purchases input number of drinksRestocks input number of bottlesReports inventoryThe VendingMachine is found in VendingMachine.java. A VendingMachine's initial inventory is 20 drinks.Ex: If the input is:5 2the output is:Inventory: 17 bottles import java.util.Scanner; public class LabProgram {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);              /* Type your code here. */            } }
You are running a grocery store thinking about installing the Sushi 1000 vending machine. The machine...
You are running a grocery store thinking about installing the Sushi 1000 vending machine. The machine costs $100,000 today (year 0) and will last six years (years 1 through 6). Assume annual sales in years 1 through 6 will be $75,000, costs will be $50,000, and depreciation will be $20,000 (straight-line). Assume a discount rate of 10%. Calculate the cash flows and NPV of this project first assuming a corporate tax rate of 0% and then assuming a corporate tax...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT