Results 1 to 9 of 9
  1. #1
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default Giving Change: A Rudimentary Program (Questions)

    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:

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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    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.

  3. #3
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Hello Norm,

    Thank you for that response--it was not combination specific--the largest denomination would have sufficed.

    Here is the revised code:

    Java 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:

    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!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    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.

  5. #5
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    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.

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    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);

  8. #8
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

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

  9. #9
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    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:

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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •