Create a new Java class inside your project folder The name

Create a new Java class inside your project folder.
The name of the class should be: Change
Note that this means you should have the following line at the top of your program:

public class Change

Write a program that will calculate the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and one dollar, in 5 cent increments (25, 30, 35, 40...), and the machine only accepts a single dollar bill as payment.

If a price that is less than 25 cents or more than 1 dollar is entered, an error message should be displayed. An error message should also be displayed if a cost is entered which is not a multiple of 5.

The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user\'s values are shown below in italics:

Enter the price of the item.
(from 25 cents to a dollar, in 5-cent increments): 45

You bought an item for 45 cents and gave me a dollar,
so your change is:
2 quarter(s),
0 dime(s), and
1 nickel(s).

Here is another example run of the program:

Enter the price of the item.
(from 25 cents to a dollar, in 5-cent increments): 10

Error: Minimum price is 25 cents

Here is another example run of the program:

Enter the price of the item.
(from 25 cents to a dollar, in 5-cent increments): 105

Error: Maximum price is 100 cents

Here is another example run of the program:

Enter the price of the item.
(from 25 cents to a dollar, in 5-cent increments): 56

Error: Price must be in 5-cent increments

Solution

Change.java

import java.util.Scanner;

public class Change {

   public static void main(String[] args) {
       //Declaring constants
       final int QUARTERS=25,DIME=10,NICKEL=5;
      
       //Declaring variables
       int cost_of_item,dimes,nickels,quarters,remAmount,rem;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //This loop continues to executes until the user enters a valid amount as input
       while(true)
       {
           //Getting the input entered by the user
           System.out.println(\"Enter the price of the item.\");
           System.out.print(\"from 25 cents to a dollar, in 5-cent increments): \");
           cost_of_item=sc.nextInt();
          
           /* checking if the user entered input is greater than or equal 25 cents or not
           * if yes,display error message
           */
           if(cost_of_item>=25)
           {
               /* checking if the user entered input is less than or equal to 100 cents or not
               * if yes,display error message
               */
               if(cost_of_item<=100)
               {
                   /* checking if the user entered input is multiple of 5 or not
                   * if yes,display error message
                   */
                   if(cost_of_item%5==0)
                   {
                       //Displaying the change
                       System.out.println(\"You bought an item for \"+cost_of_item+\" cents and gave me a dollar,\");
                       System.out.println(\"so your change is:\");
                       remAmount=100-cost_of_item;
                       //Finding no of quarters
                       quarters=remAmount/QUARTERS;
                       rem=remAmount-(quarters*QUARTERS);
                       //Finding no of dimes
                       dimes=rem/DIME;
                       rem=rem-(dimes*DIME);
                       //Finding no of nickels
                       nickels=rem/NICKEL;
                       //Displaying the change                      
                       System.out.println(quarters+\" quarter(s)\");
                       System.out.println(dimes+\" dime(s), and\");
                       System.out.println(nickels+\" nickel(s)\");
                       break;
                   }
                   else
                   {
                       System.out.println(\"Error: Price must be in 5-cent increments\ \");
                       continue;
                   }
      
               }
               else
               {
                   System.out.println(\"Error: Maximum price is 100 cents\ \");
                   continue;
               }
                  
           }
           else
           {
               System.out.println(\"Error: Minimum price is 25 cents\ \");
               continue;
           }  
       }
      
      
   }

}

________________________

Output:

Enter the price of the item.
from 25 cents to a dollar, in 5-cent increments): 10
Error: Minimum price is 25 cents

Enter the price of the item.
from 25 cents to a dollar, in 5-cent increments): 105
Error: Maximum price is 100 cents

Enter the price of the item.
from 25 cents to a dollar, in 5-cent increments): 56
Error: Price must be in 5-cent increments

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

_____________Thank You

Create a new Java class inside your project folder. The name of the class should be: Change Note that this means you should have the following line at the top o
Create a new Java class inside your project folder. The name of the class should be: Change Note that this means you should have the following line at the top o
Create a new Java class inside your project folder. The name of the class should be: Change Note that this means you should have the following line at the top o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site