I am working on a java program, the program takes an input of a dollar amount and returns the number of hundreds, fifties, twenties, tens, fives, ones, quarters, dimes, nickels and cents the value contains.
The output should look like this (but with actual numbers):
- 0 hundred
- 0 fifty
- 0 twenty
- 0 ten
- 0 five
- 0 ones
- 0 quarters
- 0 dimes
- 0 nickels
- 0 cents
Here is my code below:
import java.util.Scanner;
public class Main
{
public static void main(String\[\] args)
{
double amount, quarters, dimes, nickels, pennies;
int hundreds, fifties, twenties, tens, fives, ones;
System.out.println("Enter your dollar amount:");
try (Scanner sc = new Scanner(System.in)) {
amount = (double) sc.nextDouble();
hundreds = (int)amount / 100;
amount = amount % 100;
fifties = (int)amount / 50;
amount = amount % 50;
twenties = (int) amount / 20;
amount = amount % 20;
tens = (int)amount / 10;
amount = amount % 10;
fives = (int)amount / 5;
amount = amount % 5;
ones = (int)amount / 1;
amount = amount % 1;
quarters = amount / 25;
amount = amount % 25;
dimes = amount / 10;
amount = amount % 10;
nickels = amount / 5;
amount = amount % 5;
pennies = amount;
System.out.println("You have:");
System.out.println(hundreds " hundreds");
System.out.println(fifties " fifties");
System.out.println(twenties " twenties");
System.out.println(tens " tens");
System.out.println(fives " fives");
System.out.println(ones " ones");
System.out.println(quarters " quarters");
System.out.println(dimes " dimes");
System.out.println(nickels " nickels");
System.out.println(pennies " pennies");
System.out.println("Goodbye!");
}
}
}
I figured out how to calculate the dollar amounts, but cannot figure out the logistics for the cent values. I need it to print out as a whole number, so I know the amount needs to be converted to an integer at some point. Can anyone point me in the right direction?
CodePudding user response:
Since amount
is a double
, you could do something like
long totalCents = Math.round(amount * 100);
The Math.round
method takes a double
value and returns the long
that's closest to it, so it can round up or down. There's also a version that takes a float
value and returns an int
.
Once you've done that, the process for turning your total number of cents into pennies, nickels, dimes and quarters will be very similar to what you've already done for turning dollars into twenties, tens, fives and ones.
CodePudding user response:
Solution 1: Calculate using cents instead of dollars:
One way is to approach this is to convert the amount entered into an integer, and do the calculations in cents instead of dollars:
long amount, quarters, dimes, nickels, pennies;
long hundreds, fifties, twenties, tens, fives, ones;
double amountIn;
System.out.println("Enter your dollar amount:");
Scanner sc = new Scanner(System.in);
amountIn = (double) sc.nextDouble();
amount = Math.round (amountIn * 100.0);
hundreds = amount / 100_00;
amount = amount % 100_00;
fifties = amount / 50_00;
amount = amount % 50_00;
and so on.
I used the underscore character to represent where the decimal point would be when the number is in whole dollars.[1] This may make the code easier to read.
Solution 2: Use BigDecimal:
Floating point is inaccurate when representing almost all decimal fractions. This is one reason it is preferred to do monetary calculations using decimal instead of binary.
Although this is likely not useful for the O/P now, in the future the O/P might want to know how to use Java's BigDecimal
class. I thought I would extend the answer by showing some of how it might be used in this problem:
public static void change2() {
int quarters, dimes, nickels, pennies;
int hundreds, fifties, twenties, tens, fives, ones;
BigDecimal amount;
BigDecimal[] amountDivRemainder;
System.out.println("Enter your dollar amount:");
Scanner sc = new Scanner(System.in);
amount = sc.nextBigDecimal();
amountDivRemainder = amount.divideAndRemainder(new BigDecimal("100.00"));
hundreds = amountDivRemainder[0].intValue();
amount = amountDivRemainder[1];
amountDivRemainder = amount.divideAndRemainder(new BigDecimal("50.00"));
fifties = amountDivRemainder[0].intValue();
amount = amountDivRemainder[1];
amountDivRemainder = amount.divideAndRemainder(new BigDecimal("20.00"));
twenties = amountDivRemainder[0].intValue();
amount = amountDivRemainder[1];
and so on.
Off-Topic: Style
When I see repeated code with only minor differences, I ask this question: Can the code be improved with arrays, loops, and / or additional methods. The O/P code has repeating patterns:
c = a / b;
a = a % b;
Here is one way to use arrays and loops:
public static void change3() {
class Denomination {
String name;
int value;
int quantity;
Denomination(String name, int value) {
this.name = name;
this.value = value;
this.quantity = 0;
}
}
Denomination[] denomination = {
new Denomination("Hundreds", 100_00),
new Denomination("Fifties", 50_00),
new Denomination("Twenties", 20_00),
new Denomination("Tens", 10_00),
new Denomination("Fives", 5_00),
new Denomination("Ones", 1_00),
new Denomination("Quarters", 25),
new Denomination("Dimes", 10),
new Denomination("Nickels", 5),
new Denomination("Pennies", 1)
};
double amountIn;
int amount;
System.out.println("Enter your dollar amount:");
Scanner sc = new Scanner(System.in);
amountIn = (double) sc.nextDouble();
amount = (int) Math.round(amountIn * 100.0);
for (int i = 0; i < denomination.length; i) {
denomination[i].quantity = amount / denomination[i].value;
amount = amount % denomination[i].value;
}
System.out.println("The amount is distributed: ");
for (int i = 0; i < denomination.length; i) {
System.out.println(denomination[i].name ": " denomination[i].quantity);
}
}
Here, this uses a class
in which each instance holds information about the monetary denomination: The name, the value in cents, and the count to be distributed. The count is initially zero.
Use of the Denomination
class avoids use of tightly coupled arrays:
String [] denominationName = {"Hundreds", "Fifites", "Twenties", ...
int [] denominationValue = {100_00, 50_00, 20_00, ...
int [] denominaitonCount = new int [10];
This can be adapted for use in the BigDecimal
approach.
I used two loops to preserve the characteristic in the O/P code where all the calculations are completed prior to printing any result.
Notes:
[1] If you do that, do not use 0
as the first digit. Java will interpret that as an octal value. For example, 0_10
will be 8
, not 10
cents.