coins output in vending maching? [duplicate] - java

This question already has answers here:
Java program that tells what coins to give out for any amount of change from 1 cent to 99 cents
(3 answers)
Closed 7 years ago.
I'm not sure where I'm wrong. I am not being able to get the proper output as expected. Below is my piece of program:
public static void insertMoney(){
double dollars, quarters, dimes, nickels, pennies;
double paymentSum=0;
System.out.println("Enter amount of money in cents.");
Scanner input = new Scanner(System.in);
paymentSum = input.nextDouble();
//paymentSum = (paymentSum)/100;
if (paymentSum <= 0){
System.out.println("Please feed me more money, so I can feed you!. Try again next time :)");
return;
}
System.out.println("You entered: " +paymentSum+" Cent(s).");
System.out.println((paymentSum/100) + " Cent(s) in coins can be given as: ");
dollars = paymentSum /100;
paymentSum = paymentSum%100;
quarters = paymentSum/25;
paymentSum = paymentSum % 25;
dimes = paymentSum/10;
paymentSum = paymentSum % 10;
nickels = paymentSum/5;
paymentSum = paymentSum % 5;
pennies = paymentSum;
System.out.println(dollars + " dollar(s)");
System.out.println(quarters + " quarter(s)");
System.out.println(dimes + " dime(s)");
System.out.println(nickels + " nickel(s) and");
System.out.println(pennies + " pennies");
}
I'm trying to get output as this:
You entered: 26.0 Cent(s).
0.26 Dollar(s) in coins can be given as:
0.0 dollar(s)
1.0 quarter(s)
0.0 dime(s)
0.0 nickel(s) and
1.0 pennies
But I get this:
You entered: 26.0 Cents(s).
0.26 Dollar(s) in coins can be given as:
0.26 dollar(s)
1.04 quarter(s)
0.1 dime(s)
0.2 nickel(s) and
1.0 pennies
What am I doing wrong in my code? Any help would be appreciated. Thanks!

The problem is your expecting integer division but using a double paymentSum.
As noted in this article, since paymentSum is a double, any division will also produce a double. That's why your quarters are showing up as 1.04 instead of 1, and why your dimes are 0.1 instead of 0, etc...

Related

java code to convert cents into dimes, nickels

System.out.print("Enter the AMOUNT in CENTS: ");
int cents = scan.nextInt();
scan.close();
int dimes = (cents % 25)/10;
System.out.println("Dimes is " +dimes);
int nickels = ((cents % 25) %10) /5;
System.out.println("Nickels is " +nickels);
int pennies = ((cents % 25) %10) %5;
System.out.println("Pennies is " +pennies);
I can't seem to convert the cents into dimes, nickels, and pennies. the output should be like this:
Enter AMOUNT in CENTS : 28
Dimes is 2
Nickels is 1
Pennies is 3
but the output of my program is showing is:
Enter the AMOUNT in CENTS: 28
Enter the AMOUNT in CENTS: 28
Dimes is 0
Nickels is 0
Pennies is 3
can someone help me with the formula?
Try this:
int dimes = cents / 10;
System.out.println("Dimes is " +dimes);
int nickels = (cents % 10) / 5;
System.out.println("Nickels is " +nickels);
int pennies = (cents % 10) % 5;
System.out.println("Pennies is " +pennies);
A dime is 10 cents. A nickel is 5 cents. A penny is 1 cent.

Why won't my code divide the remaining cents by dimes, nickels, and pennies after it divided by quarters?

String choice = "y";
while(choice.equalsIgnoreCase("y")){
if (cents <= 99 && cents > 0){
quarters = cents / quarters;
System.out.println("Quarters: " + quarters);
dimes = cents / dimes;
System.out.println("Dimes: " + dimes);
nickels = cents / nickels;
System.out.println("Nickels: " + nickels);
pennies = cents / pennies;
System.out.println("Pennies: " + pennies);
Do i need to implement a modulus operator after each step?
Ah you have a can't see the wood for the trees problem.
Old school trick this called a dry run
Cents Quarters Dimes Nickels Pennies Remaining Cents
41 1 16
16 1 6
6 1 1
1 1 0
You are starting from 41 each calculation.

Trying to make a program that separates an amount in to coins [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I'm trying to make a program that takes an input amount and separates them into how many coins it would equal. So far what i wrote gives me the right amount most of the time but sometimes it will be a penny off and i don't know why.
public static void main(String[] args) {
double amount;
System.out.println("This program will display the number of "
+ "quarters, dimes, nickels and pennies based on the "
+ "amount you enter below.");
System.out.println();
System.out.print("Please enter an amount: ");
Scanner scan = new Scanner(System.in);
amount = scan.nextDouble();
double quarter = amount/0.25;
amount = amount % 0.25;
double dime = amount/0.10;
amount = amount % 0.10;
double nickel = amount/0.05;
amount = amount % 0.05;
double penny = amount/0.01;
System.out.println("Quarters: " + (int)quarter);
System.out.println("Dimes " + (int)dime);
System.out.println("Nickels " + (int)nickel);
System.out.println("Pennies " + (int)penny);
When i input 2.47, i get:
Please enter an amount: 2.47
Quarters: 9
Dimes: 2
Nickels: 0
Pennies: 2
But when i input 1.47, i get:
Please enter an amount: 1.47
Quarters: 5
Dimes: 2
Nickels: 0
Pennies: 1
The most likely reason for your problem is that floating point arithmetic is subject to rounding errors. At a certain point, one of the intermediate floating point results is not completely accurate, and the error is amplified when you use a cast to convert the double to an int.
For a full explanation of why this sort of thing happens, read the answers to Is floating point math broken?
Solution You should recode this using the int (or long) type to represent a whole number of cents.
Start with this:
long amount = (long) (100 * scan.nextDouble());
and then recode the rest of the method accordingly.

Even though the numbers too large, it's still being processed (ignore, I fixed it) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
import java.io.*;
import java.util.*;
public class CH04 {
// Constants Declaration Section
//******************************
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
// Variables Declaration Section
//******************************
int quarters;
int dimes;
int nickles;
int numberOfQuarters;
int numberOfDimes;
int remainingAmount;
int numberOfNickles;
int numberOfPennies;
int maxSize;
// Variables Initialization Section
//*********************************
Scanner data = new Scanner(new File("C:\\testing3.txt")); //Reads from text file
ArrayList<Double> datadata = new ArrayList<Double>(); //Creates Array
quarters = 25;
dimes = 10;
nickles = 5;
maxSize = 10000;
// Code Section
//*************
while(data.hasNextDouble()) //Retrieves data from text file and places it in array
{
datadata.add(data.nextDouble()); //Retrieves data from text file and places it in array
}
for (int i = 0; i < datadata.size(); i++) //Loops through the data
{
double value = datadata.get(i); //returns number found in file as a double 'value' then runs it through the calculator
System.out.println(value);
while(value > maxSize) //If value is greater than 10,000, it will be skipped and not read through the calculator
{
}
remainingAmount = (int)Math.ceil((value) * 100); //multiplies value by 100
numberOfQuarters = remainingAmount / quarters; //divides value by 25
remainingAmount = remainingAmount % quarters; //returns the remaining value
numberOfDimes = remainingAmount / dimes; //divides the remaining value by 10
remainingAmount = remainingAmount % dimes; //returns the remaining value of that
numberOfNickles = remainingAmount / nickles; //divides the remaining value from dimes by 5
remainingAmount = remainingAmount % nickles; //returns remaining value
numberOfPennies = remainingAmount; //divides remaining value by 1
System.out.print("Your amount of $" + value + " consists of: \n" + numberOfQuarters + //prints out the amount of quarters, dimes, nickels, pennies
" quarteres, " + numberOfDimes + " dimes, " + numberOfNickles + " nickles, and " + //that make up each value found inside the file.
numberOfPennies + " pennies");
System.out.print("\n\n");
}
//Output Section
//**************
//Resource Cleaning
//*****************
data.close();
}
}
**Output:
10001.0
Value is too large to be processed 10001.0
Your amount of $10001.0 consists of:
40004 quarteres, 0 dimes, 0 nickles, and 0 pennies
9755.35
Your amount of $9755.35 consists of:
39021 quarteres, 1 dimes, 0 nickles, and 0 pennies
875.4
Your amount of $875.4 consists of:
3501 quarteres, 1 dimes, 1 nickles, and 0 pennies
78.99
Your amount of $78.99 consists of:
315 quarteres, 2 dimes, 0 nickles, and 4 pennies
5.0
Your amount of $5.0 consists of:
20 quarteres, 0 dimes, 0 nickles, and 0 pennies
0.7
Your amount of $0.7 consists of:
2 quarteres, 2 dimes, 0 nickles, and 0 pennies
Values being used: [10001.0, 9755.35, 875.4, 78.99, 5.0, 0.7]
**
For some reason I am receiving 1 penny less for the italicized portion of the output. It's confusing me because the rest of the output is correct.
The values inside my text value are:
10,001.00 -
9,755.35 -
875.40 -
78.99 -
5.00 -
0.70
I will not write about the code itself. I guess that it is handmade as an example. The general rule of thumb of debugging for such a problem is: look at the very end or at the very start'.
The division is working correctly, hence must it happen at the very start and that is where your value is casted into an int. If you check the result of the product of value * 100, then will you see 7898.999999999999, not 7899 as you expected. You should use Math.ceil to get the correct value like this:
int result = (int)Math.ceil(value * 100);
You're dealing with floating point arithmetic error.
If you print out the value * 100, you'll notice it comes out to 7898.9999...
Floating point arithmetic error is only off by a very small amount when it occurs.
The simple solution for your case would be to just add .1 and then cast to an int.
Change this line:
remainingAmount = (int)(value * 100);
to:
remainingAmount = (int)(value * 100 + .1);
The only way to break this is if the test file contained values that use more than 2 decimal places. But since you're dealing with money, you shouldn't have to worry about that.

Java cash register: Getting change from in different subunits

Teaching myself Java, I reached an example problem that asks me to create a cash register program that will calculate the total change back to the customer in Dollars, Nickels, Quarters, Dimes, and pennies.
I worked out the user input but getting the correct sub-units is tricky part. For example: it's was easy to write the line of code that returned $1 dollar back if the cost was $4 and amount tendered was $5. But the trouble I am having is putting how many dimes and pennies needed in the correct field. ex2: total cost $4.62, tendered: $5, change: $0.38
System.out.print("Enter the sale amount: $ ");
double sale = user.nextDouble();
System.out.print("Enter the amount tendered by customer: $ ");
double tendered = user.nextDouble();
double totalChange = (tendered - sale);
System.out.printf("TOTAL CHANGE: $ %.2f\n", totalChange);
dollars = totalChange/1;
quarters = totalChange/25;
dimes = totalChange/10;
nickels = totalChange/5;
pennies = totalChange/1;
System.out.printf("DOLLARS: %.2f\n", dollars);
System.out.printf("QUARTERS: %.2f\n", quarters);
System.out.printf("DIMES: %.2f\n", dimes);
System.out.printf("NICKELS: %.2f\n", nickels);
System.out.printf("PENNIES: %.2f\n", pennies);
I don't need the answer (because I want to learn and teach myself), but just need some advice on how to go about doing so.
EDIT: What am I doing wrong with this algorithm:
dollars = totalChange/1;
dollars = (int)dollars;
dl = totalChange % 1;
quarters = dl/0.25;
q = quarters % 0.25;
quarters = (int)quarters;
dimes = q / .10;
d = dimes % .10;
nickels = d / .5;
n = nickels % .5;
pennies = n / .1;
n = pennies/ .1;
You are on the right track. After determining the largest number of whole dollars in change, subtract it. Then determine the largest number of quarters you can get from what is left. Repeat for each. It's the same way you do it yourself as a human.

Categories

Resources