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...
Assume that you are a vending machine dealer. You plan to purchase a vending machine for...
Assume that you are a vending machine dealer. You plan to purchase a vending machine for $200,000. One year later, you are expected to sell it back and receive $224,000 as cash. What is the IRR (internal rate of return) on this investment? Be sure to apply the definition of IRR and show your work. 3 points> In order to buy the vending machine, you plan on borrowing $80,000 from Bank at 5% of interest rate and raise $120,000 from...
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...
Nowadays, movies can be rented from a vending machine located at the entrance to many stores.  Suppose...
Nowadays, movies can be rented from a vending machine located at the entrance to many stores.  Suppose that it is now Friday evening at 8pm and a certain machine within a certain store has six copiesof the movie “Twilight” available for rent.  The machine will not be visited by the owner until Sunday afternoon at noon (which is 40hrs later), at which time returned movies will be restocked. Suppose that customers wanting to rent “Twilight” arrive at this rental machine at a...
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...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the user (money), and returns the change to the user. The change means Quarter =   25 cent Dime = 10 cent Nickels = 5 cent Pinneies = 1 cent 2-) What is the output produced by the following code? int result = 11; result /= 2; System.out.println("resu lt is " + result);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT