I am pretty new to java and am trying to create a simple program that converts an amount of money into change. However, I find that the program always prints one penny less than necessary.
I would have just added 1 to my pennies variable before printing but this would mean that when change is 0, it would still add a penny as change.
Here is my code:
change = amtPaid-Math.round((total*1.13)*100)/100.0;
pennies = (int) (change * 100);
twenties = (int)(pennies/2000);
pennies %= 2000;
tens = (int)(pennies/1000);
pennies %= 1000;
fives = (int)(pennies/500);
pennies %= 500;
toonies = (int)(pennies/200);
pennies %= 200;
loonies = (int)(pennies/100);
pennies %= 100;
quarters = (int)(pennies/25);
pennies %= 25;
dimes = (int)(pennies/10);
pennies %= 10;
nickels = (int)(pennies/5);
pennies %= 5;
I get this output after providing sample input:
The change will be: 1.83
To make up this amount, you will need:
0 twenty-dollar bills 0 ten-dollar bills
0 five-dollar bills 0 toonies
1 loonies 3 quarters
0 dimes 1 nickels
2 pennies
Does anyone know why this may be happening and how to fix it?
The way I see it, get your Grand Total (cost and taxes all in) to a decimal precision of no more than 2 (ie: 55.28). Don't allow the decimal precision to be more than 2. Whatever the rounding mechanism you use, make sure your happy with the final result.
Subtract the Amount Owed from the Amount Paid and make sure the final result is to a decimal precision of 2 (ie: 60.00 - 55.2814 = 4.7186 = 4.72 {rounded-up}). By not rounding properly to a decimal precision of 2 is where you can loose or gain a penny when calculating returned change.
Break the Amount To Return into Dollars and Cents. Both should be integer values (ie: 4 Dollars and 72 Cents).
// A value for the sake of this example. Taxes Included!
double totalOwed = 52.26534;
// Round to a precision of 2 (round UP on .5)
double grandTotal = BigDecimal.valueOf(totalOwed)
.setScale(2, RoundingMode.HALF_UP).doubleValue();
// A value for the sake of this example.
double amountPaid = 60.00;
// Round to a precision of 2 (round UP on .5)
double returnValue = BigDecimal.valueOf(amountPaid - grandTotal)
.setScale(2, RoundingMode.HALF_UP).doubleValue();
int dollars = Integer.parseInt(String.valueOf(returnValue).split("\\.")[0]);
int cents = Integer.parseInt(String.valueOf(returnValue).split("\\.")[1]);
Work out the bill currency to be returnd and add it to a List for display later. There are a LOT of different ways to do this. Here is one way:
List<String> change = new ArrayList<>();
int returnVal;
// Some displayable information
change.add("Total Cost: --> " + String.valueOf(grandTotal));
change.add("Total Paid: --> " + String.valueOf(amountPaid));
change.add("Return change: --> " + String.valueOf(returnValue));
// 100 Dollar Bills
if (dollars >= 100) {
returnVal = dollars / 100;
change.add(" " + String.valueOf(returnVal) + " Hundred Dollar Bill(s)");
dollars -= (returnVal * 100);
}
// 50 Dollar Bills
if (dollars >= 50) {
returnVal = dollars / 50;
change.add(" " + String.valueOf(returnVal) + " Fifty Dollar Bill(s)");
dollars -= (returnVal * 50);
}
// 20 Dollar Bills
if (dollars >= 20) {
returnVal = dollars / 20;
change.add(" " + String.valueOf(returnVal) + " Twenty Dollar Bill(s)");
dollars -= (returnVal * 20);
}
// 10 Dollar Bills
if (dollars >= 10) {
returnVal = dollars / 10;
change.add(" " + String.valueOf(returnVal) + " Ten Dollar Bill(s)");
dollars -= (returnVal * 10);
}
// 5 Dollar Bills
if (dollars >= 5) {
returnVal = dollars / 5;
change.add(" " + String.valueOf(returnVal) + " Five Dollar Bill(s)");
dollars -= (returnVal * 5);
}
// 2 Dollar Bills
if (dollars >= 2) {
returnVal = dollars / 2;
change.add(" " + String.valueOf(returnVal) + " Two Dollar Bill(s)");
dollars -= (returnVal * 2);
}
// 1 Dollar Bills
if (dollars >= 1) {
returnVal = dollars / 1;
change.add(" " + String.valueOf(returnVal) + " One Dollar Bill(s)");
dollars -= (returnVal * 1);
}
// Quarters (working against cents now)
if (cents >= 25) {
returnVal = cents / 25;
change.add(" " + String.valueOf(returnVal) + " Quarter(s)");
cents -= (returnVal * 25);
}
// Dimes
if (cents >= 10) {
returnVal = cents / 10;
change.add(" " + String.valueOf(returnVal) + " Dime(s)");
cents -= (returnVal * 10);
}
// Nickles
if (cents >= 5) {
returnVal = cents / 5;
change.add(" " + String.valueOf(returnVal) + " Nickle(s)");
cents -= (returnVal * 5);
}
// Pennies
if (cents >= 1) {
returnVal = cents / 1;
change.add(" " + String.valueOf(returnVal) + " Pennies");
cents -= (returnVal * 1);
}
// Display the Change-Back in Console Window
for (String str : change) {
System.out.println(str);
}
As you can see there is a lot of repetitious code in the example above. You can get rid of this by doing it yet another way. You can utilize an Array or Collection mechanism to hold the Dollar and coin denominations, for example:
// A value for the sake of this example. Taxes Included!
double totalOwed = 52.26534;
// Round to a precision of 2 (round UP on .5)
double grandTotal = BigDecimal.valueOf(totalOwed)
.setScale(2, RoundingMode.HALF_UP).doubleValue();
// A value for the sake of this example.
double amountPaid = 60.00;
// Round to a precision of 2 (round UP on .5)
double returnValue = BigDecimal.valueOf(amountPaid - grandTotal)
.setScale(2, RoundingMode.HALF_UP).doubleValue();
int dollars = Integer.parseInt(String.valueOf(returnValue).split("\\.")[0]);
int cents = Integer.parseInt(String.valueOf(returnValue).split("\\.")[1]);
List<String> change = new ArrayList<>();
int returnVal;
// Using arrays (or a collection mechanism) like this
// allows you to set whatever denominations you like.
String[][] dollarDenominations = {
{"100", " Hundred Dollar Bill(s)"},
{"50" , " Fifty Dollar Bill(s)" },
{"20" , " Twenty Dollar Bill(s)" },
{"10" , " Ten Dollar Bill(s)" },
{"5" , " Five Dollar Bill(s)" },
{"2" , " Two Dollar Bill(s)" },
{"1" , " One Dollar Bill(s)" }
};
// Although not commonly used, you might even want to
// add 50 cent pieces to the Array.
String[][] coinDenominations = {
{"25", " Quarter(s)"},
{"10", " Dime(s)" },
{"5" , " Nickle(s)" },
{"1" , " Pennies" }
};
// Some displayable information
change.add("Total Cost: --> " + String.valueOf(grandTotal));
change.add("Total Paid: --> " + String.valueOf(amountPaid));
change.add("Return change: --> " + String.valueOf(returnValue));
// Dollar Denominations
for (int i = 0; i < dollarDenominations.length; i++) {
int denom = Integer.parseInt(dollarDenominations[i][0]);
String typeString = dollarDenominations[i][1];
if (dollars >= denom) {
returnVal = dollars / denom;
change.add(" " + String.valueOf(returnVal) + typeString);
dollars -= (returnVal * denom);
}
}
// Coin Denominations
for (int i = 0; i < coinDenominations.length; i++) {
int denom = Integer.parseInt(coinDenominations[i][0]);
String typeString = coinDenominations[i][1];
if (cents >= denom) {
returnVal = cents / denom;
change.add(" " + String.valueOf(returnVal) + typeString);
cents -= (returnVal * denom);
}
}
// Display the Change-Back data in Console Window...
for (String str : change) {
System.out.println(str);
}
As said before, there are several other ways to do this sort of thing. The important thing is to work against Change-Back values that are to 2 decimal places. After all...to my knowledge, there is no such denomination as a one-sixth penny (there is no denomination for mills). The US at one time (between 1793 and 1857) use to have a half-Penny but that no longer exists and never ever will again for obvious reasons. In Canada there is no Penny since it costs almost 2 cents to make one and ultimately (in my opinion) the United States will most likely follow this scheme and save millions upon millions in the process since it also costs approx. 1.82+ cents to make a US penny.
Related
I am currently writing a program that prints only the coins used when returning change. However, I get 2 assertions errors which are evaluated on the server's back-end. There are no sample test values, you have to make up your own.
expected:[... Change Due $0.01 [1 penny] ]
but was:[... Change Due $0.01 [2 pennies] ]
expected:[... Change Due $0.19 [1 dime 1 nickel 4 pennies ]
but was:[... Change Due $0.19 [2 dimes ]
/**
* Calculate the amount of change based on user input.
* Output coins with an output greater than 0.
* #param args returns an argument array for string objects
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Total amount? ");
double total = input.nextDouble();
System.out.print("Cash payment? ");
double payment = input.nextDouble();
double changeDue = payment - total;
System.out.println();
System.out.printf("Change Due $%.2f\n\n", changeDue);
int cents = (int) Math.ceil(changeDue * 100);
int dollars = Math.round((int) cents / 100);
cents = cents % 100;
int quarters = Math.round((int) cents / 25);
cents = cents % 25;
int dimes = Math.round((int) cents / 10);
cents = cents % 10;
int nickels = Math.round((int) cents / 5);
cents = cents % 5;
int pennies = Math.round((int) cents / 1);
int[] coins = {dollars,quarters,dimes,nickels,pennies};
String[] names = {"dollar", "quarter", "dime", "nickel", "penny"};
for (int i = 0; i < 5; i++) {
if (coins[i] == 1) {
System.out.println(coins[i] + " " + names[i]);
} else if (coins[i] > 1 && i != 4) {
System.out.println(coins[i] + " " + names[i] + "s");
}
}
if (coins[4] > 1) {
System.out.println(coins[4] + " pennies");
}
}
Your code actually works fine. But the problem is if you just print your "changeDue" variable and see then you can see there is a 9 at the end. It is printed as 0.010000000000000009 not as 0.010000000000000000 as you would expect it to. Then it is ceiled as 2 after multiplying by 100. Then you get 2 pennies as the answer.
Actually this seems to be a problem with how decimals are stored. (Java Double value = 0.01 changes to 0.009999999999999787) It is explained here nicely. So you might have to change your approach a little bit.
You have made a fairly common mistake in Java. Money cannot be represented by double, they are different things. In Java the closest thing we have to money is called BigDecimal. It's a little more awkward because you can't use arithmetic symbols with it (+-/*^%), but it's a lot more accurate.
There is also a formal specification for money: Money and Currency API (JSR 354), for which there are a couple of implementations including Joda Money and the offical reference implementation.
I know my question is repeated but I still couldn't understand what the other answers are explaining. Below is my code and I have calculated it to get to the first line which is 57 pennies + 0 dimes + 0 nickels + 0 quarters and I am thinking to run a loop that will list all the possible combinations of pennies, dimes, nickels, quarters. But, I don't know how.
public class Conversion{
public static void main(String[] args) {
int cents = 57;
int quarter = 25;
int dime = 10;
int nickel = 5;
int penny = 1;
int totalPennies = cents / penny;
cents %= penny;
int totalNickels = cents / nickel;
cents %= nickel;
int totalDimes = cents / dime;
cents %= dime;
int totalQuarters = cents / quarter;
cents %= quarter;
System.out.print(totalPennies + " pennies + ");
System.out.print(totalNickels + " nickels + ");
System.out.print(totalDimes + " dimes + ");
System.out.println(totalQuarters + " quarters");
}
}
Your order of operations is backwards.
The first thing you do is count how many pennies you have. Since pennies are literally valued at 1, you can have 57 pennies and make up $0.57 just fine. That is obviously not what you want to accomplish.
What you want to do is count from your highest denomination and work your way backwards. Here's a sample.
// This should be the first operation
int totalQuarters = cents / quarter;
cents %= quarter;
I leave reordering the rest as an exercise for the reader, but the output then becomes correct once successfully reordered.
2 pennies + 1 nickels + 0 dimes + 2 quarters
i'm currently coding a program that will convert pennies into the correct number of Twenties, tens, fives, dollars, quarters, dimes, nickels, and pennies. I've gotten the first part of the program correct but once i get to dollars my math is not outputting the correct amount. Any help?
import java.util.Scanner;
public class assignment1 {
public static void main(String[] args) {
Scanner inputReader = new Scanner(System.in);
int num, remainder;
System.out.print("Enter a value as a number of pennies: ");
num = inputReader.nextInt();
remainder = num % 2000;
System.out.println(num + " pennies is equal to:");
System.out.println((num / 2000) + " twenties");
System.out.println((remainder / 1000) + " tens");
System.out.println(((remainder % 1000) / 500) + " fives");
System.out.println((((remainder % 1000) % 500) / 100) + " dollars");
System.out.println(((((remainder % 1000) % 500) % 100) / 25) + " quarters");
System.out.println(((((remainder % 1000) % 500) % 100) % 25) / 10 + " dimes");
System.out.println((((((remainder % 1000) % 500) % 100) % 25) % 10) / 5 + " nickels");
System.out.println(((((((remainder % 1000) % 500) % 100) % 25) % 10) % 5) / 1 + " pennies");
}
}
Ok. I see what you've done. Let's start with
System.out.println((num / 2000) + "twenties");
System.out.println((remainder / 1000) + "tens");
System.out.println((remainder / 1000) % 500 + "fives");
System.out.println((((remainder / 1000) % 500) % 100) + "dollars");
The first line is correct. But you cannot use a simple remainder to keep a running tally. Also, num and remainder are terrible variable names. You can do something like this for a running tally,
System.out.print("Enter a value as a number of pennies: ");
int pennies = inputReader.nextInt();
System.out.println(pennies + " pennies is equal to:");
int twenties = pennies / 2000;
pennies -= twenties * 2000;
int tens = pennies / 1000;
pennies -= tens * 1000;
int fives = pennies / 500;
pennies -= fives * 500;
int dollars = pennies / 100;
pennies -= dollars * 100;
int quarters = pennies / 25;
pennies -= quarters * 25;
int dimes = pennies / 10;
pennies -= dimes * 10;
int nickels = pennies / 5;
pennies -= nickels * 5;
System.out.printf("%d twenties, %d tens, %d fives, %d dollars, "
+ "%d quarters, %d dimes, %d nickels, %d pennies%n", twenties,
tens, fives, dollars, quarters, dimes, nickels, pennies);
That would output (with 5869 as input),
5869 pennies is equal to:
2 twenties, 1 tens, 1 fives, 3 dollars, 2 quarters, 1 dimes, 1 nickels, 4 pennies
Or (if you wanted a non-running tally), just remove the subtractions from pennies like
System.out.println(pennies + " pennies is equal to:");
int twenties = pennies / 2000;
int tens = pennies / 1000;
int fives = pennies / 500;
int dollars = pennies / 100;
int quarters = pennies / 25;
int dimes = pennies / 10;
int nickels = pennies / 5;
System.out.printf("%d twenties, %d tens, %d fives, %d dollars, "
+ "%d quarters, %d dimes, %d nickels, %d pennies%n", twenties,
tens, fives, dollars, quarters, dimes, nickels, pennies);
And for 5869 you get
5869 pennies is equal to:
2 twenties, 5 tens, 11 fives, 58 dollars, 234 quarters, 586 dimes, 1173 nickels, 5869 pennies
This is possible with only mathematical operators. In most other programming task you'll encounter, you should use control structures to accomplish the steps in an algorithm.
Process
Begin with the number of pennies,
Divide by 2000 (the value of a $20-bill) and assign to a new variable,
Subtract from the number of pennies the value of the twenties,
Repeat for subsequent denominations ($10 bills, $5 bills, etc.)
I think the problem is that each time you divide your number it returns a new number as an Integer. So if you divide 100 pennies by 1000 it returns 0 so the rest of your math is going to return 0 no matter what you do.
example based on your code:
//assuming the input is 100 pennies the first remainder/1000 will equal to 0
System.out.println(((((((remainder / 1000) % 500) % 100) % 25) % 10) %5) % 1 + "pennies");
You haven't specified that you have to use modulus so i am assuming you're not.
Here's what i would do:
System.out.println(num + " pennies is equal to:");
System.out.println((num / 2000) + "twenties");
System.out.println((num / 1000) + "tens");
System.out.println((num / 500) + "fives");
System.out.println((num / 100) + "dollars");
System.out.println((num / 25) + "quarters");
System.out.println((num / 10) + "dimes");
System.out.println((num / 5) + "nickels");
System.out.println(num + "pennies");
Java input;
import java.util.*;
public class NetPay3
{
public static void main()
{
// Define Scanner object
Scanner inLine = new Scanner (System.in);
// Define other variables
float pay;
int OneHundredPounds, FiftyPounds, TwentyPounds, FivePounds,
OnePound, FiftyPence, TwentyPence, FivePence, TwoPence, OnePenny;
// Ask for the time in seconds
System.out.print ("Enter Net Pay : ");
pay = inLine.nextFloat();
// Calculate the hours. There are (3600)
// i.e. 60 x 60 seconds in every hour
OneHundredPounds = (int) pay / 100;
// Calculate what is left over and store back into seconds
pay = pay % 100;
// Calculate the minutes. There are 60 seconds
// in a minute.
FiftyPounds = (int) pay / 50;
// Whatever is left over must be the seconds
pay = pay % 50;
// Calculate the hours. There are (3600)
// i.e. 60 x 60 seconds in every hour
TwentyPounds = (int) pay / 20;
// Calculate what is left over and store back into seconds
pay = pay % 20;
// Calculate the hours. There are (3600)
// i.e. 60 x 60 seconds in every hour
FivePounds = (int) pay / 5;
// Calculate what is left over and store back into seconds
pay = pay % 5;
// Calculate the hours. There are (3600)
// i.e. 60 x 60 seconds in every hour
OnePound = (int) pay / 1;
// Calculate what is left over and store back into seconds
pay = pay % 1;
// Calculate the hours. There are (3600)
// i.e. 60 x 60 seconds in every hour
FiftyPence = (int) pay / 2;
// Calculate what is left over and store back into seconds
pay = pay % 2;
// Display the hours, minutes and seconds
System.out.println ("Amount of £100 notes " + OneHundredPounds);
System.out.println ("Amount of £50 notes " + FiftyPounds);
System.out.println ("Amount of £20 notes " + TwentyPounds);
System.out.println ("Amount of £5 notes " + FivePounds);
System.out.println ("Amount of £1 coins " + OnePound);
System.out.println ("Amount of 50p coins " + FiftyPence);
}
}
Screen input and output;
Enter Net Pay : 176.50
Amount of £100 notes 1
Amount of £50 notes 1
Amount of £20 notes 1
Amount of £5 notes 1
Amount of £1 coins 1
Amount of 50p coins 0
Hi relatively new to programming,
having trouble with me modulus and int operators in terms of getting them to function with the correct output on screen, previous syntax's worked correctly bar the 50p, anyone care to shed any light? thanks :)
Try changing FiftyPence = (int) pay / 2; toFiftyPence = (int) (pay / 0.5f);
Here is your code corrected and improved.
Don't use floats here, use integer arithmetic.
import java.util.*;
public class NetPay3 {
public static void main(String[] args) {
// Define Scanner object
Scanner inLine = new Scanner(System.in);
// Define other variables
int pay;
int OneHundredPounds, FiftyPounds, TwentyPounds, FivePounds, OnePound, FiftyPence, TwentyPence, FivePence, TwoPence, OnePenny;
System.out.print("Enter Net Pay : ");
float pay1 = inLine.nextFloat();
pay = (int) (100 * pay1);
OneHundredPounds = (int) pay / 10000;
pay = pay % 10000;
FiftyPounds = (int) pay / 5000;
pay = pay % 5000;
TwentyPounds = (int) pay / 2000;
pay = pay % 2000;
FivePounds = (int) pay / 500;
pay = pay % 500;
OnePound = (int) pay / 100;
pay = pay % 100;
FiftyPence = (int) pay / 50;
pay = pay % 50;
System.out.println("Amount of £100 notes " + OneHundredPounds);
System.out.println("Amount of £50 notes " + FiftyPounds);
System.out.println("Amount of £20 notes " + TwentyPounds);
System.out.println("Amount of £5 notes " + FivePounds);
System.out.println("Amount of £1 coins " + OnePound);
System.out.println("Amount of 50p coins " + FiftyPence);
System.out.println("Leftover pence: " + pay);
}
}
But I would further simplify this to (for example) this program:
import java.util.*;
public class NetPay3 {
public static void main(String[] args) {
Scanner inLine = new Scanner(System.in);
float[] val = new float[]{100, 50, 20, 5, 1, 0.5f, 0.2f, 0.05f, 0.02f, 0.01f};
int pay;
System.out.print("Enter Net Pay : ");
float pay1 = inLine.nextFloat();
pay = (int) (100 * pay1);
for (int i=0; i<val.length; i++){
int m = ((int)(val[i] * 100));
int cnt = pay / m;
String s1 = val[i] < 1 ? " coins: " : " notes: ";
String s2 = val[i] < 1 ? "" : "£";
String s3 = val[i] < 1 ? "p" : "";
String s4 = val[i] < 1 ? m + "" : (m/100) + "";
System.out.println("Amount of " + s2 + s4 + s3 + s1 + cnt);
pay = pay % m;
}
}
}
I have tried to do a simple math calculation for a formula involving variables. However, an error pops up in the compiler indicating that the variable types don't match. I have tried casting and changing the variable types, but they do not work. How do I fix this without destroying the basic format of my code.
I am not experienced at Java, so any pointers will help.
Here is the code. It is for a money conversion program. The error is in the second part of the code, the else part.
import java.util.Scanner;
public class MConvert
{
public static void main (String[] args)
{
int penny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar, twentyDollar, fiftyDollar, hundredDollar;
//can sub $ sign for dollar in variable, convention otherwise
double totalMoney;
Scanner scan = new Scanner (System.in);
System.out.println ("Are you converting to the total? If so, type true. \nIf you are converting from the total, then type false.");
boolean TotalorNot = true;
TotalorNot = scan.nextBoolean();
if (TotalorNot) {
System.out.println ("Type in the number of one-hundred dollar bills.");
hundredDollar = scan.nextInt();
System.out.println ("Type in the number of fifty dollar bills.");
fiftyDollar = scan.nextInt();
System.out.println ("Type in the number of twenty dollar bills.");
twentyDollar = scan.nextInt();
System.out.println ("Type in the number of ten dollar bills.");
tenDollar = scan.nextInt();
System.out.println ("Type in the number of five dollar bills.");
fiveDollar = scan.nextInt();
System.out.println ("Type in the number of one dollar bills or coins.");
dollar = scan.nextInt();
System.out.println ("Type in the number of half-dollar coins.");
halfDollar = scan.nextInt();
System.out.println ("Type in the number of quarter-dollar coins.");
quarter = scan.nextInt();
System.out.println ("Type in the number of dimes.");
dime = scan.nextInt();
System.out.println ("Type in the number of nickels.");
nickel = scan.nextInt();
System.out.println ("Type in the number of pennies coins.");
penny = scan.nextInt();
totalMoney = (hundredDollar * 100) + (fiftyDollar * 50) + (twentyDollar * 20) + (tenDollar * 10) + (fiveDollar * 5) + (dollar * 1) + ((double)halfDollar * 0.5) + ((double)quarter * 0.25) + ((double)dime * 0.1) + ((double)nickel * 0.05) + ((double)penny * 0.01);
System.out.println ("Here is total monetary value of the bills and coins you entered: " + totalMoney);
} else {
System.out.println ("Type in the total monetary value:");
totalMoney = scan.nextDouble();
hundredDollar = ((int)totalMoney / 100);
fiftyDollar = ((int)totalMoney - (hundredDollar * 100)) / 50;
twentyDollar = ((int)totalMoney - (fiftyDollar * 50)) / 20;
tenDollar = ((int)totalMoney - (twentyDollar * 20)) / 10;
fiveDollar = ((int)totalMoney - (tenDollar * 10)) / 5;
dollar = ((int)totalMoney - (fiveDollar * 5)) / 1;
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
quarter = ((int)totalMoney - (halfDollar * 0.5)) / 0.25;
dime = ((int)totalMoney - (quarter * 0.25)) / 0.1;
nickel = ((int)totalMoney - (dime * 0.1)) / 0.05;
penny = ((int)totalMoney - (nickel * 0.05)) / 0.01;
System.out.println (hundredDollar + " hundred dollar bills");
System.out.println (fiftyDollar + " fifty dollar bills");
System.out.println (twentyDollar + " twenty dollar bills");
System.out.println (tenDollar + " ten dollar bills");
System.out.println (fiveDollar + " five dollar bills");
System.out.println (dollar + " one dollar bills or coins");
System.out.println (halfDollar + " half-dollar coins");
System.out.println (quarter + " quarter-dollar coins");
System.out.println (dime + " dimes");
System.out.println (nickel + " nickel");
System.out.println (penny + " penny");
}
}
}
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
Is entirely wrong , first remove this instruction.
Also change your code with below lines.
quarter = (int)((totalMoney - (halfDollar * 0.5)) / 0.25);
dime = (int) ((totalMoney - (quarter * 0.25)) / 0.1);
nickel =(int)( (totalMoney - (dime * 0.1)) / 0.05);
penny = (int)((totalMoney - (nickel * 0.05)) / 0.01);
You also need to improve logic of your code, I can clearly see it do not serve what you want to achieve.
At this line
((int)totalMoney - (tenDollar * 10)) / 5;
the cast is applied to just totalMoney
to cast the value of whole expression you must do this
(int)(totalMoney - (tenDollar * 10)) / 5);
The problem is in the following line:
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
What is the meaning of the (double) here? You cannot cast the left side of a variable assignment (the variable itself) to a double. The variable is declared to be an int, and that cannot be changed. Never.
This line compiles (but may be incorrect concerning semantics):
halfDollar = (int) ((totalMoney - (dollar * 1)) / 0.5);
Apart from that: I don't think, the program does, what you want it to do...
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
What are you trying to achieve with the double cast on the halfDollar. Remove that first. It took me 5 mins to find out why there were so many red lines in your code.
Next, you can do a simple case like this:-
halfDollar = (int)((totalMoney - (dollar * 1)) / 0.5);
But its not advisable to cast double values to int as you may tend to lose a lot of decimal data. Why not simple have all penny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar,twentyDollar, fiftyDollar, hundredDollar; as double itself?