Question

In: Computer Science

For Java 3.25 LAB: Exact change Write a program with total change amount in pennies as...

For Java

3.25 LAB: Exact change

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.

Ex: If the input is:

0

the output is:

No change

Ex: If the input is:

45

the output is:

1 Quarter
2 Dimes 

270738.1591718

LAB ACTIVITY

3.25.1: LAB: Exact change

What did I do wrong here?

My Code:

import java.util.Scanner;

public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
  
int num_Pennies;
int num_Nickels;
int num_Quarters;
int num_Dollars;
  
num_Pennies = scnr.nextInt();
  
if (num_Pennies == 0){
System.out.println("No Change");
}
else {
num_Dollars = num_Pennies / 100;
num_Quarters = ((num_Pennies - (num_Dollars * 100)) / 25);
num_Dimes = (num_Pennies - ((num_Dollars * 100) + (num_Quarters * 25))/ 10 );
num_Nickels = (num_Pennies - ((num_Dollars * 100) + (num_Quarters * 25) + (num_Dimes * 10)) / 5 );
num_Pennies = (num_Pennies - ((num_Dollars * 100) + (num_Quarters * 25) + (num_Dimes * 10) + (num_Nickels * 5)) / 5 );
if (num_Dollars == 1) {
System.out.println(num_Dollars + "Dollar");
}
else {
System.out.println(num_Dollars + "Dollars");
}
if (num_Quarters == 1) {
System.out.println(num_Quarters + "Quarter");
}
else {
System.out.println(num_Quarters + "Quarter");
}
if (num_Dimes == 1) {
System.out.println(num_Dimes + "Dime");
}
else {
System.out.println(num_Dimes + "Dimes");
}
if (num_Nickels == 1) {
System.out.println(num_Nickels + "Nickel");
}
else {
System.out.println(num_Nickel + "Nickels");
}
if (num_Pennies == 1) {
System.out.println(num_Pennies + "Penny");
}
else {
System.out.println(num_Pennies + "Pennies");
}

}
}

Solutions

Expert Solution

import java.util.Scanner;
public class LabProgram {
    public static void ExactChange(int userTotal, int[] coinVals) {
        coinVals[0] = userTotal / 100;
        userTotal %= 100;
        coinVals[1] = userTotal / 25;
        userTotal %= 25;
        coinVals[2] = userTotal / 10;
        userTotal %= 10;
        coinVals[3] = userTotal / 5;
        userTotal %= 5;
        coinVals[4] = userTotal;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] coins = new int[5];
        int value = in.nextInt();
        if(value <= 0) {
            System.out.println("No change");
        } else {
            ExactChange(value, coins);
            if (coins[0] == 1) System.out.println(coins[0] + " dollar");
            else if (coins[0] >= 1) System.out.println(coins[0] + " dollars");
            if (coins[1] == 1) System.out.println(coins[1] + " quarter");
            else if (coins[1] > 1) System.out.println(coins[1] + " quarters");
            if (coins[2] == 1) System.out.println(coins[2] + " dime");
            if (coins[2] > 1) System.out.println(coins[2] + " dimes");
            if (coins[3] == 1) System.out.println(coins[3] + " nickel");
            if (coins[3] > 1) System.out.println(coins[3] + " nickels");
            if (coins[4] == 1) System.out.println(coins[4] + " penny");
            if (coins[4] > 1) System.out.println(coins[4] + " pennies");
        }
    }
}


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
C++ 19.32 LAB: Exact change - functions Write a program with total change amount as an...
C++ 19.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs 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. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2...
6.32 LAB: Exact change - functions Write a program with total change amount as an integer...
6.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs 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. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2 dimes...
USE PYTHON 4.15 LAB: Exact change Write a program with total change amount as an integer...
USE PYTHON 4.15 LAB: Exact change Write a program with total change amount 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. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars sold and total sales amount of a car salesperson for a given month. The salesperson’s paycheck is computed as follows: a. Every sales person gets 10% (commission) of total sales b. Sales totals greater than $50,000 get 5% of total sales amount c. 8 or more cars sold earns the salesperson an extra 3% Please remove 30% (taxes) of the gross pay and list...
Write a program with total change amount as an integer input, and output the change using...
Write a program with total change amount 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. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes c++ please
Write a program in pyton with total change amount as an integer input, and output the...
Write a program in pyton with total change amount 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. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes Can you...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT