ChangeMaker Help
Author 
Message 
Grentex

Posted: Sat Oct 10, 2015 3:29 pm Post subject: ChangeMaker Help 


So I have to make a changemaker using methods and what not.
Lab says
Use the minimum number of coins. $1.39 also equals 0 quarters, 0 dimes, 0 nickels and 139 pennies, but you're not going to have a very successful career as a cashier if you make change in this way.
In this second program, keep your ChangeMaker class flexible by NOT having it include any input or output to/from the console. This way, it can be used in a console application, windows application, or web application!
Also, please note that I've included a toString() method which is a standard method that returns a string to describe the class.
Also test with $1.15 to make sure you get the right answer. This number gets stored as 1.14999999, so it needs some rounding to get it right. Be sure to handle this in your solution.
Tester class
code: 
import java.util.Scanner;
public class Assignment1ChangeMaker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ChangeMaker c1 = new ChangeMaker();
System.out.print("Please set the amount of money that you wish to convert into change: $");
c1.setMoney(sc.nextDouble());
c1.calculateChange();
System.out.println(c1.toString());
}
}

Method Class
code: 
public class ChangeMaker {
private double money;
private int numberOfQuarters;
private int numberOfDimes;
private int numberOfNickels;
private int numberOfPennies;
public ChangeMaker(){
money = 0.00;
numberOfQuarters = 0;
numberOfDimes = 0;
numberOfNickels = 0;
numberOfPennies = 0;
}
public double getMoney(){
return money;
}
public void setMoney(double amount){
this.money = (double) Math.round(amount * 100.0) / 100.0;
}
public void calculateChange(){
numberOfQuarters = (int) Math.floor(money / 0.25) ;
numberOfDimes = (int) Math.floor((money  0.25 * numberOfQuarters) / 0.10);
numberOfNickels = (int) Math.floor((money  0.25 * numberOfQuarters  0.10 * numberOfDimes) / 0.05);
numberOfPennies = (int) Math.floor((money  0.25 * numberOfQuarters  0.10 * numberOfDimes  0.05 * numberOfNickels) / 0.01);
}
public String toString(){
return "$" + money +" equals " + numberOfQuarters + " quarters, " + numberOfDimes + " dimes, " + numberOfNickels + " nickels, and " + numberOfPennies + " pennies.";
}
}

It was going fine but whenever you input 1.4999999 you suddenly lose a penny. In fact, after doing some testing apparently it always loses a penny whenever there is one quarter, one dime, and more than 2 pennies.
For example:$0.43 equals 1 quarters, 1 dimes, 1 nickels, and 2 pennies. Working ones: $1.12 equals 4 quarters, 1 dimes, 0 nickels, and 2 pennies.
$1.13 equals 4 quarters, 1 dimes, 0 nickels, and 2 pennies. $1.62 equals 6 quarters, 1 dimes, 0 nickels, and 2 pennies.
$1.38 equals 5 quarters, 1 dimes, 0 nickels, and 2 pennies. $1.09 equals 4 quarters, 0 dimes, 1 nickels, and 4 pennies.
Any help for this would be great. 





Sponsor Sponsor



Insectoid

Posted: Sat Oct 10, 2015 5:17 pm Post subject: RE:ChangeMaker Help 


This is the result of floating point precision errors, and this is usually the assignment where students discover the problem. There's a really, really easy solution that involves not using floating point numbers at all. See if you can figure it out. 





Grentex

Posted: Sat Oct 10, 2015 7:11 pm Post subject: RE:ChangeMaker Help 


Alright you lost me, from what I can tell its telling me to use BigDecimal, which I can't use. So either I break the lab rules, or not be able to hand it in. great 





DemonWasp

Posted: Sat Oct 10, 2015 9:25 pm Post subject: RE:ChangeMaker Help 


You almost never get an assignment in school that's actually impossible  if it looks impossible, then either you aren't reading the assignment correctly or you're missing some way of solving the problem.
In this case, ask yourself: what is the smallest unit of currency you will need to handle? (hint: not dollars, since you sometimes need fractions of a dollar) 





Grentex

Posted: Sun Oct 11, 2015 12:15 pm Post subject: Re: RE:ChangeMaker Help 


DemonWasp @ Sat Oct 10, 2015 9:25 pm wrote: You almost never get an assignment in school that's actually impossible  if it looks impossible, then either you aren't reading the assignment correctly or you're missing some way of solving the problem.
In this case, ask yourself: what is the smallest unit of currency you will need to handle? (hint: not dollars, since you sometimes need fractions of a dollar)
If you are talking about essentially doing this
code: 
private int numberOfPennies;
public void setMoney(double money) {
numberOfPennies = (int) Math.round(money * 100);
}
public void calculateChange() {
// subtract 25s, then 10s, then 5s, then 1s, until 0.
}

I does not work, I tried and then STILL ended up with 1 penny missing even though I am using penny as the the full amount. 





Insectoid

Posted: Sun Oct 11, 2015 3:00 pm Post subject: RE:ChangeMaker Help 


code:  private int numberOfPennies 
You're on the right track here. Tell me, why do you need to use doubles, or even decimal points, at all? 






