Steven Do
Steven Do

Reputation: 3

ChangeMaker Issue with rounding pennies (keeps on removing one)

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

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

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.
$1.13 equals 4 quarters, 1 dimes, 0 nickels, and 2 pennies.
$1.38 equals 5 quarters, 1 dimes, 0 nickels, and 2 pennies.

Working ones:
$1.09 equals 4 quarters, 0 dimes, 1 nickels, and 4 pennies.
$1.62 equals 6 quarters, 1 dimes, 0 nickels, and 2 pennies.
$1.12 equals 4 quarters, 1 dimes, 0 nickels, and 2 pennies.

Any help for this would be great. :)

Upvotes: 0

Views: 240

Answers (1)

ctt_dev
ctt_dev

Reputation: 38

Instead of using double, it may be a better idea to store the money as an integer representing the number of pennies. This should allow you to eliminate all the floating-point arithmetic in calculateChange(), which could be what's causing the loss of information.

Here's an example of what I mean:

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.
}

Upvotes: 2

Related Questions