
08-08-2008, 12:57 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 31
Rep Power: 0
|
|
Giving Change: A Rudimentary Program (Questions)
|
Quote:
|
Exercise P2.9. Giving Change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Compute the difference, and compute the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return.
First, transform the difference into an integer balance, denominated in pennies. Then compute the whole dollar amount. Subtract it from the balance. Compute the number of quarters needed. Repeat for dimes and nickels. Display the remaining pennies.
|
Here's what I have so far:
|
Code:
|
/*----------------------------------------------------
Exercise P2.9
-----------------------------------------------------*/
import ccj.*;
public class Change
{
public static void main(String[] args)
{
System.out.println("Amount Due in Dollars?");
double amountd = Console.in.readDouble();
System.out.println("Amount Received in Dollars?");
double amountr = Console.in.readDouble();
double difference = (amountr - amountd);
//Convert to Pennies
int balance = difference * 100;
System.out.println("The difference is $" + difference + ".");
System.out.println("The difference is " + balance + " pennies.");
}
} |
I'm stuck in the step where I calculate the change received. I've succesfully converted the balance to pennies, but I'm stuck on what to do next.
I'm thinking maybe that with the use of "if" statements, that I can succesfully give the amount of change necessary in dollars, quarters, dimes, nickels, and pennies.
But here's what I'm also thinking: there are several combinations of dollars, quarters, dimes, nickels, and pennies. Does it not matter what combination I use?
Thanks.
|
|

08-08-2008, 01:23 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
Quote:
|
|
several combinations of dollars, quarters, dimes, nickels, and pennies. Does it not matter what combination I use?
|
That would be a question for your instructor.
I would think an answer would be the most of the largest denomination.
To find that use integer division. For example given 225 cents
change, divide by 100 (one dollar) = 2 dollar bills. Then subtract off that amount and continue down the line.
|
|

08-11-2008, 10:27 PM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 31
Rep Power: 0
|
|
Hello Norm,
Thank you for that response--it was not combination specific--the largest denomination would have sufficed.
Here is the revised code:
|
Code:
|
/*----------------------------------------------------
Exercise P2.9
-----------------------------------------------------*/
import ccj.*;
public class Change
{
public static void main(String[] args)
{
System.out.println("Amount Due in Dollars?");
double amountd = Console.in.readDouble();
System.out.println("Amount Received in Dollars?");
double amountr = Console.in.readDouble();
double difference = (amountr - amountd);
//Convert to Pennies
int balance = difference * 100;
int dollars = balance / 100;
balance = (balance - (100 * dollars));
int quarters = balance / 25;
balance = (balance - (25 * quarters));
int dimes = balance / 10;
balance = (balance - (10 * dimes));
int nickels = balance / 5;
balance = (balance - (5 * nickels));
int pennies = balance / 1;
balance = balance - (1 * pennies);
}
} |
However, I am getting an error at line 21:
|
Quote:
|
Change.java:21: possible loss of precision
found : double
required: int
int balance = difference * 100;
|
How can I get around the conversion from dollars ($) to pennies? The program won't compile because of a "possible loss of precision?"
P.S. - the way I calculate the change is aided with the use of dividing two integers. (When two integers are divided...the result of the division is always an integer...any remainder is discarded).
Please let me if the code is cloudy, or if anything is logically incorrect. Thanks!
|
|

08-12-2008, 01:22 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
You can suppress the error message with casting the results of the multiply to int.
If you know that the input will not include parts of a cent ie you won't get an input of $1.234 (4/10 of a cent) then you are safe in casting.
|
|

08-13-2008, 07:28 PM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 31
Rep Power: 0
|
|
|
Norm,
I apologize for my incompetency. Several google searches have led me to website's such as: javabeginner.com/java-object-type-casting.htm that provides in-depth tutorials on casting.
Although I am not opposed to studying this tutorial, please head me towards the right direction. Is this the correct topic that I should be learning or is there a shortcut "casting code" that I can perform to suppress the error message?
Thank you for your help by the way--you've been a great asset to my endeavors on learning a new programming language.
|
|

08-13-2008, 08:35 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 571
Rep Power: 2
|
|
Originally Posted by carlodelmundo
|
|
How can I get around the conversion from dollars ($) to pennies? The program won't compile because of a "possible loss of precision?"
|
Never, never use floating point for money. Never. double/float, makes no difference, never use them for currancy.
Store the money as an integer number of pennies. Or pence, yen, etc.
Use integer arithmetic.
If you really want to get advanced (not here in new to java) create a money or currency class.
Your problem is that you are violating the fundamental rule: never use floating point for money.
|
|

08-13-2008, 09:21 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
The problem is the program uses the readDouble() method to get the amount in dollars. Normal usage would think in dollars and a person would enter an amount as $1.25 vs 125 pennies.
To cast, inclose the type in parans before the expression (also in parans):
int pennies = (int) (dollarAmt * 100);
|
|

08-13-2008, 09:37 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 571
Rep Power: 2
|
|
Originally Posted by Norm
|
|
The problem is the program uses the readDouble() method to get the amount in dollars.
|
Yes, that is a fatal problem.
I am not saying that external presentation of money should be in pennies, yen, etc. That is bad UI design.
You should read $1.25 as a string and parse it. And on output, format it properly.
In Java, use a filter to throw away the $ ¥, £, etc. then use split to pull out the commas and periods.
Using float or double for money is a sin.
|
|

08-14-2008, 12:33 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 31
Rep Power: 0
|
|
Norm,
Thanks again! That suppresed the error message.
Fishtoprecords,
Thank you for your suggestions also. I will keep in mind that money--as a general rule of thumb--should be kept as an integer. I'll also look into advance topics (as you stated...a money class).
Thank you both. Here's the resulting code utilizing the CCJ package for anyone's reference:
|
Code:
|
/*----------------------------------------------------
Exercise P2.9
-----------------------------------------------------*/
import ccj.*;
public class Change
{
public static void main(String[] args)
{
System.out.println("Amount Due in Dollars?");
double amountd = Console.in.readDouble();
System.out.println("Amount Received in Dollars?");
double amountr = Console.in.readDouble();
double difference = (amountr - amountd);
//Convert to Pennies
int balance = (int)(difference * 100);
int dollars = balance / 100;
balance = (balance - (100 * dollars));
int quarters = balance / 25;
balance = (balance - (25 * quarters));
int dimes = balance / 10;
balance = (balance - (10 * dimes));
int nickels = balance / 5;
balance = (balance - (5 * nickels));
int pennies = balance / 1;
balance = balance - (1 * pennies);
System.out.println("The difference is " + dollars + " dollars, " + quarters + " quarters, "
+ dimes + " dimes, " + nickels + " nickels, " + pennies + " pennies.");
}
} |
This is Exercise P2.9 of Computing Concepts with Java Essentials by Cay Horstmann.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 01:59 PM.
|
|