Results 1 to 6 of 6
  1. #1
    Duranx is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default [Help] Change to give back program

    Hi, i'm trying to complete a few excercies from my "Learn Java by Dietel" textbook, and they didn't post the answers to some of them so i was wondering if you guys could help me out. The program asks the user for the number to input and then it gives the amount of change back. Here is a example of what should be outputted:

    Enter the price of the purchase in cents ($1 or less): 33
    The change from $1.00 is: $0.67
    Number of quarters is: 2
    Number of dimes is: 1
    Number of nickels is: 1
    Number of pennies is: 2

    So the user enters the number 33 and the program delivers the rest of the information. Anyone have any idea how to write the program to do this? :confused:

    Thanks

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    well, finding the change from a dollar is easy right, take the input and subtract it from 100, the remainder is the change.

    To break it down into each piece of currency, in this case how the sample is printed, start from largest and word towards smallest. divide the change by the value of the coin piece.

    In Java, using an integer to divide with for numerators and denominators gives an integer result, or truncating the remainder to get the largest whole number.

    for example.

    Java Code:
    int purchase = 33;
    int change = 100- 33; // = 67
    
    int quarters = change / 25;  // here 67 / 25 = 2
    and so on.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    One way to start is by examining the question and asking "Why is the example output 2 quarters, 1 dime etc?"

    In other words, how would a human change giver figure out what change to give? Once you are able to describe, comprehensively and step-by-step, the process you yourself would take when confronted with someone who wants change for 33c from a dollar, then you will be in a position to start writing some code. The program is less an oracle that "delivers information" and more of a machine to carry out the algorithm that you have described.

    [Edit]

    to slow ...;(

    start from largest and word towards smallest
    In case, having been given the algorithm, there is nothing more that is interesting in the problem, you might want to consider why this works. That is why it leads to a minimum number of coins being given as change. (I take it this is an unstated condition and that you can't just offer 67 pennies as change and be done with it.)
    Last edited by pbrockway2; 02-06-2010 at 01:57 AM.

  4. #4
    Duranx is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    Thats what i thought as well, but then some things came to mind. For example 67 / 25 is 2.68. Wouldn't the program output 2.68 instead of 2? Aswell, each time something is divided the value changes, for example:

    Java Code:
    int purchase = 33;
    int change = 100- 33; // = 67
    
    int quarters = change / 25;  // here 67 / 25 = 2
    int dimes = change / 10;
    int nickels = change / 5;
    int pennies = change / 1;
    The dime, nickels, and pennies really need to be divided by the remaining amounts of 67 /25. Which would be 17, then the nickels would have to be divided by 7, and the pennies by 2. So basically what i'm trying to say is that every time a operation happens the value for "change" changes. How would you work around that?

  5. #5
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Wouldn't the program output 2.68 instead of 2?
    If int data type is used it will always show decimal digit. No real numbers.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    And the line
    Java Code:
    int quarters = change/25;
    doesn't alter the value of the variable change, it simply divides the number stored in the change variable with 25, and fixes that value to the quarters variable, leaving change unaltered.
    Java Code:
    change = change/25;
    This would alter the change variable. And, if you think about it, the change variable should be altered (not by division of course), if you already gave 2 quarters back, the remaining total to be given back is decreased by 50 cents.

Similar Threads

  1. Replies: 2
    Last Post: 04-09-2009, 11:46 PM
  2. Change the color in my program
    By carl in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 01:20 PM
  3. Replies: 6
    Last Post: 12-17-2008, 04:37 PM
  4. how to give a popup with back end data in struts
    By tsaswathy in forum Web Frameworks
    Replies: 0
    Last Post: 10-07-2008, 07:14 PM
  5. Giving Change: A Rudimentary Program (Questions)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 08-14-2008, 12:33 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
  •