In: Computer Science
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.
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()
}
}