The following is the program I wrote, but the result is not what is required exatly.
Somebody please point me out to what I am doing wrong.

The question:

Write a Java application that reads an integer value between 0 and 100 (inclusive), representing the amount of a purchase in cents. Produce an error message if the input value is not in that range. If the input is valid, determine the amount of change that would be received from one dollar, and print the number of quarters, dimes, nickels, and pennies that should be returned. Maximize the coins with the highest value. Follow the format below. The user input is shown in boldface:

Enter the purchase amount [0-100]: 36
Your change of 64 cents is given as:

2 Quarters
1 Dimes
0 Nickels
4 Pennies

Hint: Use integer division and the % operator, also known as the mod operater. For example, 64/25 equals 2, and 64 % 25 equals 14. You must store your intermediate calculations in variables, not just redo every step over in the print statement!

The program:

// make change in dimes etc.


class MakeChange


{
public static void main (String[] args)
{
int price, change, quarters, dimes, pennies;


System.out.println("type price(0:100):");


price = 36;


change = 100 - price; // how much change
quarters = change / 2; // number of quarters
dimes = change / 1; // number of dimes
pennies = change / 4; // number of pennies


System.out.print("The change is:");
System.out.print(quarters);
System.out.print("quarters");
System.out.print(dimes);
System.out.print("dimes");
System.out.print(pennies);
System.out.print("pennies.\n");
}
}

Dani AI

Generated

Quick diagnosis: the program posted by never reads user input, uses the wrong divisors (2, 1, 4) instead of coin values, and doesn’t keep intermediate remainders. was right to point to the hint about integer division and modulus, and ’s suggestion to use Scanner is exactly what makes this interactive (as expected).

Algorithm to follow (keep each step in a variable as required): read an int from the user and validate it’s between 0 and 100; compute change = 100 - price; then sequentially extract coins from largest to smallest using integer division and % to get the remainder. Store the remainder after each coin so you don’t recalculate everything in the print statements.

A simple, correct implementation (interactive and validating input):

import java.util.Scanner;

public class MakeChange {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the purchase amount [0-100]: ");
        if (!sc.hasNextInt()) {
            System.out.println("Error: input must be an integer.");
            sc.close();
            return;
        }
        int price = sc.nextInt();
        if (price < 0 || price > 100) {
            System.out.println("Error: purchase amount must be between 0 and 100.");
            sc.close();
            return;
        }

        int change = 100 - price;
        int quarters = change / 25;
        int rem = change % 25;
        int dimes = rem / 10;
        rem = rem % 10;
        int nickels = rem / 5;
        int pennies = rem % 5;

        System.out.println("Your change of " + change + " cents is given as:");
        System.out.println(quarters + " Quarters");
        System.out.println(dimes + " Dimes");
        System.out.println(nickels + " Nickels");
        System.out.println(pennies + " Pennies");

        sc.close();
    }
}

Troubleshooting notes: use sc.hasNextInt() to avoid InputMismatchException, explicitly check 0 and 100 (100 yields zero change), and if this is for an automated grader, match the exact wording/capitalization requested.

Recommended Answers

All 3 Replies

Where do you exatly do this?

Write a Java application that reads an integer value between 0 and 100 (inclusive)

And why don't you use HINT for this part????

change = 100 - price; // how much change
quarters = change / 2; // number of quarters
dimes = change / 1; // number of dimes
pennies = change / 4; // number of pennies

I thought we were suppose to make it interactive

check the Java api's for the Scanner class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.