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-cent increments
(25, 30, 35, . . . , 90, 95, or 100), and the machine accepts only a single
dollar bill to pay for the item.

- int variable itemPrice

- int variables quarters, dimes, nickels

- Scanner object

Program accepts itemPrice, which is a price for the item you would like to purchase.

Positive behaviour:

Please follow the example:

-If the Item price was entered 95: need to display message: Your change is 0 quarters, 0 dimes, and 1 nickels.
-If the Item price was entered 60: need to display message: Your change is 1 quarters, 1 dimes, and 1 nickels.

Note: Implement a program that calculates the minimum number of coins required to give a user change.

Negative behaviour:

If itemPrice is less than 25 or more than 100 cents, then display error message:

Invalid price!

If itemPrice is not divisible by 5, then also display error message:

Invalid price!

use (itemPrice % 5 == 0) expression to find it is divisible by 5.

Respuesta :

The program that determines the change to be dispensed from a vending machine is illustrated below

How to depict the program?

import java.util.Scanner;  //to accept input from user

public class Main {   //class name

 public static void main(String[] args) {  //start of main function

Scanner input =new Scanner(System.in);  // creates Scanner class object

int amount, quarters, dimes, nickels, pennies,change;   //declare variables

System.out.println("Enter the price of the(from 25 cents to a dollar, in 5-cent increments): ");  //prompts user to enter the price

amount=input.nextInt();   //reads amount from user

change= 100-amount;   //computes change

System.out.println("You bought an item for " + amount+" cents and gave me a dollar, so your change is :");  

quarters=change/25;  //computes quarters

change=change%25;  //computes quarter remaining

if(quarters == 1)   // if quarter is equal to 1

System.out.println(quarters+ " quarter");   //prints quarter value in singular

else if (quarters>1)   // if value of quarters is greater than 1

System.out.println(quarters+" quarters");     //prints plural quarters value

dimes=change/10;  //computes dime

if(dimes == 1)   // if value of dime is equal to 1

System.out.println(dimes+ " dime");   //prints single value of dimes

else if (dimes>1)   //if value of dimes is greater than 1

System.out.println(dimes+" dimes");  //prints plural value of dimes

change=change%10;   //computes dimes remaining

nickels=change/5;  //computes nickels

if(nickels == 1)   //if value of nickels is equal to 1

System.out.println(nickels+ " nickel");   //prints single value of nickels

else if (nickels>1)   //if value of nickels is greater than 1

System.out.println(nickels+" nickels");   //prints plural value of nickels

change=change%5;    //computes nickels remaining

pennies=change;  //computes pennies

if(pennies == 1)   //if value of pennies is equal to 1

System.out.println(pennies+ " penny");   //prints single value of pennies

else if (pennies>1)    //if value of pennies is greater than 1

System.out.println(pennies+" pennies");     } }  //prints plural value of pennies

Learn more about program on:

https://brainly.com/question/1538272

#SPJ1

Otras preguntas