Question

In: Computer Science

Java This is background information First, code this project. Write a program that determines the change...

Java

This is background information First, code this project. 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 a dollar, in 5-cent increments (25, 30, 35, ..., 90, 95, or 100) and the machine only accepts a single dollar bill to pay for the item. For example, a possible dialogue with the user might beEnter price of item(from 25 cents to a dollar, in 5-cent increments): 45You bought an item for 45 cents and gave me a dollar,so your change is 2 quarters,0 dimes, and 1 nickel.

You can write this program based on the program or with if statements. After getting it to work, include input checking. Display the change only if a valid price is entered (no less than 25 cents, no more than 100 cents, and an integer multiple of 5 cents). Otherwise, display separate error messages for any of the following invalid inputs: a cost under 25 cents, a cost that is not an integer multiple of 5, and a cost that is more than a dollar.

Write comments

Solutions

Expert Solution

CODE

import java.util.Scanner;

public class Main
{
        public static void main(String args[])
        {
                //variables
                int price;
                int quarters, dimes, nickel;
                
                Scanner sc = new Scanner(System.in);
                
                //prompt for price
                System.out.print("Enter price of item(from 25 cents to a dollar, in 5-cent increments): ");
                price = sc.nextInt();
                
                //if price less than 25 cents
                if(price < 25)
                {
                        System.out.println("Invalid price, Price can't be less than 25 cents.");
                }
                //if price greater than 100 cents
                else if(price > 100)
                {
                        System.out.println("Invalid price, Price can't be greater than 100 cents.");
                }
                //if price is not multiple of 5
                else if(price % 5 != 0)
                {
                        System.out.println("Invalid price, Price must be multiple of 5.");
                }
                //if a valid price
                else
                {
                        //calculating total change
                        int cents = 100 - price;
                        
                        //calculating number of quarters
                        quarters = cents / 25;
                        
                        //calculating remaining change
                        cents = cents % 25;
                        
                        //calculating number of dimes
                        dimes = cents / 10;
                        
                        //calculating remaining change
                        cents = cents % 10;
                        
                        //calculating number of nickel
                        nickel = cents / 5;
                        
                        //printing output
                        System.out.println("You bought an item for " + price + " cents and gave me a dollar,so your change is " + quarters + " quarters," + dimes + " dimes, and " + nickel + " nickel.");
                }
        }
}

OUTPUT

CODE SCREEN SHOT


Related Solutions

Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
Write a program in JAVA that takes in two words, and then it recursively determines if...
Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second word. If the size of the first word is larger than the second then it should automatically return false. Also if both strings are empty then return true. YOU MAY NOT(!!!!!!!!!!): Use any iterative loops. In other words, no for-loops, no while-loops, no do-while-loops, no for-each loops. Use the string...
Write a java program using the following instructions: Write a program that determines election results. Create...
Write a java program using the following instructions: Write a program that determines election results. Create two parallel arrays to store the last names of five candidates in a local election and the votes received by each candidate. Prompt the user to input these values. The program should output each candidates name, the votes received by that candidate, the percentage of the total votes received by the candidate, and the total votes cast. Your program should also output the winner...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
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!)
write a java program. 10-20 lines of code You are a landscaper, one of your first...
write a java program. 10-20 lines of code You are a landscaper, one of your first tasks is to determine the cost of landscaping a yard. You charge a flat fee of $40 to landscape a yard, and an extra $10 a bag for raking leaves. Not all yards you work on have leaves that need to be raked. Houses without any leaves will be discounted $5. Using your program, use the JOption Pane to ask the homeowner to enter...
Write a program that prompts the user enter a word, and determines the first half and...
Write a program that prompts the user enter a word, and determines the first half and second half of the word. Print the first and second half of the word on different lines. Sample runs of the program are shown below. Note: If the word is an odd number of letters, the first half should contain fewer letters (as shown in sample run #2) SAMPLE PROGRAM RUN #1                     Enter a word: carrot First half: car Second half: rot SAMPLE PROGRAM...
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Program 5A: Determine which student has the highest grade Write a Java program that determines which...
Program 5A: Determine which student has the highest grade Write a Java program that determines which student has the highest grade. You will ask the user to enter the number of students. Then you will ask for each student and their grade. You will output the name and grade of the student with the highest grade. You will NOT use an array for this assignment. Call your class Program5A, so your filename will be Program5A.java. It is essential for grading...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should be only 3) in it and prints out the number of photos in the gallery.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT