Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-08-2008, 12:57 AM
Member
 
Join Date: Aug 2008
Posts: 31
Rep Power: 0
carlodelmundo is on a distinguished road
Default 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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-08-2008, 01:23 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-11-2008, 10:27 PM
Member
 
Join Date: Aug 2008
Posts: 31
Rep Power: 0
carlodelmundo is on a distinguished road
Default
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!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-12-2008, 01:22 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-13-2008, 07:28 PM
Member
 
Join Date: Aug 2008
Posts: 31
Rep Power: 0
carlodelmundo is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-13-2008, 08:35 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Originally Posted by carlodelmundo View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-13-2008, 09:21 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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);
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-13-2008, 09:37 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Originally Posted by Norm View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-14-2008, 12:33 AM
Member
 
Join Date: Aug 2008
Posts: 31
Rep Power: 0
carlodelmundo is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change the color in my program carl New To Java 5 04-03-2009 01:20 PM
Status of tomcat is not giving "true" keshari Web Frameworks 1 05-16-2008 07:31 AM
a few questions on structuring my program... sdkevinb New To Java 0 02-06-2008 07:48 AM
Just a Few Questions pringle New To Java 21 01-09-2008 07:21 PM
3 Questions hiranya AWT / Swing 4 11-14-2007 05:57 AM


All times are GMT +2. The time now is 01:59 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org