Merry Christmas everyone. I do have a question before Santa arrives.
I'm new with Java, so i'm skimming through my Java book and I'm doing the exercises on my first chapter.
Here's the question, "Write and application that determines the value of the coins in a jar and prints the total in Dollars and Cents. Read integer values that represent the number of quarters, dimes, nickels and pennies."
I actually did this program. But I wonder if I did it right.
public class PP28 {
public static void main(String[] args) {
// cents = pennies.
double quarters = 0.25 * 40;
开发者_C百科double dimes = 0.1 * 200;
double nickels = 0.05 * 400;
double pennies = 0.01 * 150;
int total;
int cent;
total = (int) quarters + (int) dimes + (int) nickels + (int) pennies;
cent = 100 % total;
System.out.println(total+" dollars and "+cent+" cents");
}
}
It compiles and runs fine. Also, I wonder if this is mathematically right? Should I get 49 cents that is almost equivalent to 51.(5)$? Because all quarters, dimes, nickels and pennies has the sum of 51.5 dollars.
I would avoid using a floating point type for this due to rounding errors.
Here is a good article explaining the issue (What Every Computer Scientist Should Know About Floating-Point Arithmetic).
Use integers to get the count in cents.
Something like this:
integer quarters = 25 * numOfQuarters;
You can get the dollar amount by diving with 100.
Oded's answer is definitely one to implement. Use integers.
You have another problem however: cent = 100 % total;
This code is not doing what you think it's doing.
Because total is an integer and is summed like this:
total = (int) quarters + (int) dimes + (int) nickels + (int) pennies;
Your result is 51. That's 51 dollars even. You lost all of the information on fractional dollars by converting the double values (quarters, dimes, nickels and pennies) to integers. If the value of quarters would have been 10.25, then (int) quarters would be 10.
Now, you are trying to get the number of leftover cents using cent = 100 % total;
This gives you the integer remainder of 100 / total. In your case 100 / 51. The remainder leftover is 49 which you stored in 'cent'. It just happened in your example to be close to the correct value. This is why your answer was incorrect, not rounding.
All that said, you should STILL be using integers for all of your values.
It is easier to work on one representation, for example cents, and then later translate it into dollars. Oded's comment about floating point rounding errors are correct.
I do find it strange that you convert pennies into an integer when the value is 1.5, this doesn't seem correct. Something like this can be done instead:
int quarters = 25 * 40;
int dimes = 10 * 200;
int nickels = 5 * 400;
int pennies = 1 * 150;
int sum = quarters + dimes + nickels + pennies;
int dollars = sum / 100;
int cents = sum % 100;
When printing the values of dollars and cents I get 51 dollars and 50 cents exactly.
In addition to the problems mentioned about, the problem says read the number of quarters, dimes, nickels and pennies. That means read from a file, not have hard-coded as constants.
Also, the monetary values of quarters, dimes, nickels and pennies are unlikely to change anytime soon. They should be declared as static final variables:
class PP28 {
final int QUARTER = 25;
...
精彩评论