Results 1 to 5 of 5
  1. #1
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Unhappy Vending Machine Change - LOGIC Understanding ISSUE

    NEWBIE JAVA:

    I have a simple java program to write which tells the user combination of coins that equals to the amount of change i.e:

    user input 87
    output:
    3 quarters
    1 dime
    0 nickels
    2 pennies

    SIMPLE RIGHT?! - we'll my book shows the example BUT my confusion is how the program remembers the remainder which is passed to the next column of let say dime

    i.e

    originalAmount = amount;
    quarters = amount /25;
    amount = amount % 25; <---- this is confusing for me?!?! how can the integer = integer % 25
    dimes = amount / 10; <--- HOW THE PROGRAM remembers the "remainder" instead of the original user input as the code it self tells you dimes = amount where "amount" is what user input NOT remainder. PLease help.
    amount = amount % 10;
    so on ....;


    What I don't understand is HOW this algorithm works. I mean we have
    int amount where user inputs the number we get the first calculation
    amount/25 = how many quarters and then amount %25 WILL tell us about
    the reminder. By looking at this piece of code I would say that the
    system should start the calculation for the dimes again from the
    original number since the code says dimes = amount/10 AND amount =
    amount%10. My understanding is that the calculation should be done
    from the original user input.

    Book or code it self is not clear for me how the reminder is
    "REMEMBERED" and then pass on to the next calculation>!?!?

    UNLESS the code: amount=amount%25 gets the remainder so the next code under it is REQUIRED to read from the last prompt code - Please advise if I think right?

    Please advise,
    dk

  2. #2
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Vending Machine Change - LOGIC Understanding ISSUE

    let's say you insert amount of 90
    quarters = amount/25
    amount = amount % 25; <--- this part is where 'amount' on the left side gets assigned the remainder after the calculations on the right side are done..i.e ( 90%25= 15)
    // amount = 15
    dimes = amount/10 <-- now that amount =15 ... dimes would be equal to 1.
    amount = amount%10 <--- this part is where 'amount' on the left side gets assigned the remainder after the calculations on the right side are done..i.e( 15%10 = 5)
    //amount=5
    etc.
    Do you understand ?

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Vending Machine Change - LOGIC Understanding ISSUE

    the '=' operator assigns a value to a variable. You see in programming, it makes sense to say x = x +1, something that can't hold true.

    amount starts as the original amount and changes throughout the execution. it stores the updated values to the amount variable that's how it 'remembers'

    amount = amount % 25.. means amount becomes what is was modulo 25 and stays that way until changed.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Vending Machine Change - LOGIC Understanding ISSUE

    lol, didn't realize this was a zombie because it had a fresh response...

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Vending Machine Change - LOGIC Understanding ISSUE

    Quote Originally Posted by superhaNds View Post
    lol, didn't realize this was a zombie because it had a fresh response...
    Nevermind; it happens to all of us; you can take a shower around the corner while I close this dead, old thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. vending machine
    By Timmy1596 in forum JCreator
    Replies: 2
    Last Post: 09-16-2011, 09:45 PM
  2. Understanding EJB "machine" ;-)
    By cygan in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 09-08-2011, 09:24 PM
  3. Recursive Change Machine
    By EZlats in forum New To Java
    Replies: 2
    Last Post: 04-01-2011, 03:46 AM
  4. Replies: 1
    Last Post: 03-05-2011, 05:47 PM
  5. Replies: 3
    Last Post: 10-06-2008, 11:03 PM

Posting Permissions

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