Results 1 to 11 of 11
  1. #1
    Jamesing is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Help with coin assigning program

    Hi. I'm trying to make a program, with which the user can input a number of pennies and the program will assign each part of the value to the appropriate coin.
    For example, if the user inputs 123, the program would output:
    Java Code:
    toonies = 0
    loonies = 1
    quarters = 0
    dimes = 2
    nickles = 0
    pennies = 3
    Or, if the user inputs 546, the program would output:
    Java Code:
    toonies = 2
    loonies = 1
    quarters = 1
    dimes = 2
    nickles = 0
    pennies = 1
    It needs to be able to do that with any int value. Here's what I have so far. I've finished all the user input code and I'm in the middle of trying to make it assign the value to the correct coins, but I think I'm on the wrong track.
    Java Code:
    import java.io.*;
    class partThree
    {
       public static void main(String[] args) throws IOException
       {
         InputStreamReader inStream = new InputStreamReader (System.in);
    	 BufferedReader stdin = new BufferedReader(inStream);
    	 String inData;
    	 
    	 int money = 0;
    	 int toonies = 0, loonies = 0, quarters = 0, dimes = 0,  nickles = 0,  pennies = 0;
    	 
    	 System.out.println("Enter your amount of money in pennies:");
    	 inData = stdin.readLine();
    	 money = Integer.parseInt(inData);
    	 
    	 if (money >= 200)
    	 {
    	   while (money != 0)
    	   {
    	     toonies = 1;
    	     money -= 200;
    	     if (money >= 100) 
                 {
    I'm hoping someone can tell me how to make it assign the coins correctly.
    If you need any additional information, let me know.
    Last edited by Jamesing; 03-11-2011 at 04:32 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Use divide and mod.

    Java Code:
    int value = 123;
    System.out.println(value / 100);
    System.out.println(value % 100);

  3. #3
    Jamesing is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    That helps, but I'm not sure how I would connect it with the coin variables
    Java Code:
    int toonies, loonies, quarters, dimes, nickles, pennies;

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I'm not Canadian so do not know what toonies and loonies are worth. I assume though they are in descending order.

    You start by dividing your amount by toonies to see how many toonies there are. then you mod the amount by toonies to get the remainder.
    You then divide your amount by loonies etc.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    I'm not sure how I would connect it with the coin variables
    Maybe if you think about it some more you will. 7 minutes may not be long enough.

    db
    Last edited by DarrylBurke; 03-11-2011 at 04:52 AM.

  6. #6
    Jamesing is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    Okay, thanks for the help. :)

  7. #7
    Jamesing is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Maybe if you think about it some more you will. 7 minutes may not be long enough.

    db
    I'd already thought about it, but thanks for the advice, however useless it may be.

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

    Default

    ...however useless it may be.

    We're getting a bit of an outbreak of this.

    @OP: This is not an attitude you are going to find helpful.

    Rather as db suggests, stop and think about the problem. It's the sort of task primary school children might be given: from some large pile of tokens representing pennies, figure out how much money you have.

    The only difference is that we use mathematical words. The children would form heaps of tokens (big toony heaps, smaller loony heaps with the remainder etc). Finding the number of heaps of a given size is called "division". Finding the remainder is called "modulus". And Java has built in operators for both.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I just did this earlier in c++, the way you were doing it will work fine, it's how I did it. Take some time and really think about how to do it. Also take the time to understand how it can be done with modulus instead(%)

    Your way will actualy not work easily. You can however; do it with one if else clause per coin type.
    Last edited by sunde887; 03-11-2011 at 05:35 AM.

  10. #10
    Jamesing is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Maybe if you think about it some more you will. 7 minutes may not be long enough.

    db
    Quote Originally Posted by pbrockway2 View Post
    We're getting a bit of an outbreak of this.

    @OP: This is not an attitude you are going to find helpful.

    Rather as db suggests, stop and think about the problem. It's the sort of task primary school children might be given: from some large pile of tokens representing pennies, figure out how much money you have.

    The only difference is that we use mathematical words. The children would form heaps of tokens (big toony heaps, smaller loony heaps with the remainder etc). Finding the number of heaps of a given size is called "division". Finding the remainder is called "modulus". And Java has built in operators for both.
    Yes, I understand. I have already put a lot of thought into the problem. I have considered using the "/" and "%" operators, but I initially wasn't sure how I would use them, so I tried using the "if" statements instead, as you can see in my code, but after I was a little ways through that, I thought about it some more and it didn't seem like it was going to work, so I thought someone on this forum might know how. But I think the issue is solved now. Thanks.

  11. #11
    Jamesing is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    I just did this earlier in c++, the way you were doing it will work fine, it's how I did it. Take some time and really think about how to do it. Also take the time to understand how it can be done with modulus instead(%)

    Your way will actualy not work easily. You can however; do it with one if else clause per coin type.
    Thanks, I'll definitely do that. :)

Similar Threads

  1. Java Coin Acceptor?
    By starzsimon in forum Advanced Java
    Replies: 5
    Last Post: 08-06-2011, 07:50 AM
  2. Coin application
    By kkGG in forum New To Java
    Replies: 4
    Last Post: 11-08-2010, 12:24 AM
  3. Coin Change
    By Growler in forum New To Java
    Replies: 7
    Last Post: 09-23-2010, 05:33 PM
  4. coin toss program(Eclipse)
    By ccie007 in forum New To Java
    Replies: 5
    Last Post: 08-06-2010, 07:03 PM
  5. Random coin flip application
    By Boomer1 in forum New To Java
    Replies: 8
    Last Post: 12-18-2009, 02: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
  •