Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use Java Write a program that will simulate a change machine found at cash regis

ID: 3761332 • Letter: U

Question

Use Java

Write a program that will simulate a change machine found at cash registers. Input the amount due and amount paid from the keyboard.

Tell the user how much change is owed and number of quarters, dimes, nickels, and pennies in change a customer would receive.

Pay special attention to roundoff error. Your program should use division and modular division. No if’s or loops may be used. You may assume all change will be a positive number with no more than two decimal places. If more than $1.00 is owed, the full value should be included in the number of quarters returned. For example, $2.50 should be returned as 10 quarters.

Hint: Modular division is a great way to find the remainder of a division. Think about how you can use this to calculate the change that is left over after some coins are given.

Sample Run:

Please Enter the Cost of the Item:
4.57
Please Enter the Amount Paid:
5.00

Change Owed: 0.43
Quarters: 1
Dimes: 1
Nickels: 1
Pennies: 3

Explanation / Answer


/**The java program CoinMachine that prompts
* the user to enter cost of item in dollar
* and amount paid.
* Then prints the change in terms
* of quarters, dimes , nickels
* and pennis.
* */
//CoinMachine.java
import java.util.Scanner;
public class CoinMachine
{
   public static void main(String[] args)
   {

       //set constant values for change of dollar
       final int QUARTERS=25;
       final int DIMES=10;
       final int NICKELS=5;
       final int PENNIS=1;


       //declare and intialize integer variables
       int quarters=0;
       int dimes=0;
       int nickels=0;
       int pennis=0;


       //Create an scanner class object
       Scanner scanner=new Scanner(System.in);      
       System.out.println("Please Enter the Cost of the Item:");
       double cost=scanner.nextDouble();
       System.out.println( "Please Enter the Amount Paid:");
       double amount=scanner.nextDouble();

       //Round off the value
       double change=Math.round((amount-cost)*100.0)/100.0;
       //print change
       System.out.println("Change Owed: "+change);

       //change the decimal to integer value of change
       change=change*100;

       //check if change is >=25
       if(change>=25)
       {
           //calculate the number of quarters
           quarters=(int) (change/QUARTERS);
           int remcents=(int) (change%QUARTERS);          

           //check if remaining cents is >=10
           if(remcents>=10)
           {
               //calculate the number of dimes
               dimes=(int)(remcents/DIMES);
               remcents=(int) (remcents%DIMES);
           }

           //check if remaining cents is >=5
           if(remcents>=5)
           {
               //calculate the number of nickels
               nickels=(int)(remcents/NICKELS);
               remcents=(int) (remcents%NICKELS);
               //set remaining cents to pennis
               pennis=remcents;
           }          
       }

       //print number of quarters
       System.out.println("Quarters : "+quarters);
       //print number of dimes
       System.out.println("Dimes : "+dimes);
       //print number of nickels
       System.out.println("Nickels : "+nickels);
       //print number of pennis
       System.out.println("Pennis : "+pennis);
   }
}

------------------------

Sample Output:


sample run1:

Please Enter the Cost of the Item:
4.57
Please Enter the Amount Paid:
5.0
Change Owed: 0.43
Quarters : 1
Dimes : 1
Nickels : 1
Pennis : 3

sample run2:
Please Enter the Cost of the Item:
4.2
Please Enter the Amount Paid:
5.0
Change Owed: 0.8
Quarters : 3
Dimes : 0
Nickels : 1
Pennis : 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote