Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: I may be over-complicating this...

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Question I may be over-complicating this...

    I'm currently in an online highschool course that is teaching me the basics of programming in java. My current assignment is:


    Create a Java program that will:
    1. Ask the user to input an amount of money as an integer value. Example: 123
    2. Display the coins necessary to make up the amount entered by the user.
    For example, if the user enters the integer value 123, your program
    should output:

    toonies = 0
    loonies = 1
    quarters = 0
    dimes = 2
    nickles = 0
    pennies = 3
    I've written up a small portion of what I think the code should look like but I believe that I'm over-complicating what I've written so-far. If anyone has a few minutes to look over the below code and possibly suggest an easier way to complete this assignment it would be very much appreciated.

    (I'm allowed to get help on these questions as long as I write the code myself.)


    import java.io.*; //tells Java input will be used
     
    class Assignment4Question3
    {
        public static void main (String[] args) throws IOException
        {
            InputStreamReader inStream = new InputStreamReader (System.in);
            BufferedReader userInput = new BufferedReader (inStream);
            String inputData; //Creates a string variable called 'inputData'.
            System.out.println ("Enter an amount of money as an interger. (Example: 123 = $1.23):"); 
            inputData = userInput.readLine ( ); //Places input into inputData string variable.
     
    		int pennies = 0;
    		int nickles = 0;
    		int dimes = 0;
    		int quarters = 0;
    		int loonies = 0;
    		int toonies = 0;
    		int intInputData = Integer.parseInt(inputData);
     
    		pennies = (intInputData % 10);
    		if (pennies >= 5) 
    		{
    			nickles = 1;
    			pennies = (pennies - 5);
    			dimes = ((((intInputData % 100) - (pennies)) - 5) / 10);
    			if (dimes == 3)
    			{
    				nickles = (nickles + 1);
    				dimes = (dimes - 3);
    				quarters = 1;
    				if (nickles == 2)
    				{
    					nickles = (nickles - 2);
    					dimes = (dimes + 1);
    				}
    			}
    			else if (dimes == 5)
    			{
    				dimes = (dimes - 5);
    				quarters = 2;
    			}
    			else if (dimes >= 8)
    			{
    				nickles = (nickles + 1);
    				dimes = (dimes - 7);
    				quarters = (quarters + 3);
    				if (nickles == 2)
    				{
    					nickles = (nickles - 2);
    					dimes = (dimes + 1);
    				}
    			}
    		}
     
    		System.out.println("Pennies = "+pennies+"");
    		System.out.println("Nickles = "+nickles+"");
    		System.out.println("Dimes = "+dimes+"");
    		System.out.println("Quarters = "+quarters+"");
        }
    }

    At the moment the above is crudly calculating the pennies, nickles, dimes and quarters with a few errors but it mostly works.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I may be over-complicating this...

    Look at using the /, % and - operators to solve this.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: I may be over-complicating this...

    Quote Originally Posted by Norm View Post
    Look at using the /, % and - operators to solve this.
    While writing a response to your reply an idea just came to me...

    int exampleInteger = (12345 % 100) // This would equal 45.
    double exampleDouble = (exampleInterger / 10) // This would equal 4.5

    Would there be a way to get everything on the left side of the decimal point and then save it to an integer, because if there is I believe I may be able to shorten the code tremendously.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I may be over-complicating this...

    Would there be a way to get everything on the left side of the decimal point and then save it to an integer,
    casting a double to an int will drop the fractional parts.

    When working with money, look at using pennies instead dollars with 2 decimal points.
    This will require scaling dollars to pennies by multiplying by 100.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: I may be over-complicating this...

    Thank's for the help so-far. After reading what you said today I've re-written the program and I've expanded it. Although it is a lot shorter now there is still one part left of the program to write; I know that I can just copy and paste this:

    		if (dimes == 2 && nickles >= 1)
    		{
    			dimes = (dimes - 2)
    			nickles = (nickles - 1)
    			quarters = (quarters + 1)
    		}

    for the code to sort out the change if there is an excess of dimes and nickles.


    Would there be a way to simply tell the program to repeat the above if statement until it can't anymore and then continue along to the final statements?




    Here is what the program currently looks like:
    import java.io.*; //tells Java input will be used
     
    class Assignment4Question3
    {
        public static void main (String[] args) throws IOException
        {
            InputStreamReader inStream = new InputStreamReader (System.in);
            BufferedReader userInput = new BufferedReader (inStream);
            String inputData; //Creates a string variable called 'inputData'.
            System.out.println ("Enter an amount of money as an interger. (Example: 123 = $1.23):"); 
            inputData = userInput.readLine ( ); //Places input into inputData string variable.
     
    		int pennies = 0;
    		int nickles = 0;
    		int dimes = 0;
    		int quarters = 0;
    		int loonies = 0;
    		int toonies = 0;
    		int intInputData = Integer.parseInt(inputData);
    		int inputDataStringLength = ((inputData.length()) - 3);
    		String toonieString = inputData.substring(0, inputDataStringLength);
     
    		pennies = (intInputData % 10);
    		dimes = ((intInputData % 100) / 10);
    		loonies = ((intInputData % 1000) / 100);
    		toonies = ((Integer.parseInt(toonieString)) / 2);
     
    		if (pennies >= 5)
    		{
    			nickles = (nickles + 1);
    			pennies = (pennies - 5);
    		}
     
    		if (dimes == 3)
    		{
    			dimes = (dimes - 3);
    			nickles = (nickles + 1);
    			quarters = (quarters + 1);
    		}
     
    		if (dimes == 4)
    		{
    			dimes = (dimes - 3)
    			nickles = (nickles + 1)
    			quarters = (quarters + 1)
    		}
     
    		if (dimes == 5)
    		{
    			dimes = (dimes - 5)
    			quarters = (quarters + 2)
    		}
     
    		if (dimes == 6)
    		{
    			dimes = (dimes - 5)
    			quarters = (quarters + 2)
    		}
     
    		if (dimes == 7)
    		{
    			dimes = (dimes - 5)
    			quarters = (quarters + 2)
    		}
     
    		if (dimes == 8)
    		{
    			dimes = (dimes - 8)
    			nickles = (nickles + 1)
    			quarters = (quarters + 3)
    		}
     
    		if (dimes == 9)
    		{
    			dimes = (dimes - 8)
    			nickles = (nickles + 1)
    			quarters = (quarters + 3)
    		}
     
    		if (dimes >= 2 && nickles >= 1) // WORKING HERE ATM
    		{
    			dimes = (dimes - 2)
    			nickles = (nickles - 1)
    			quarters = (quarters + 1)
    		}
     
     
    		System.out.println("Pennies = "+pennies+"");
    		System.out.println("Nickles = "+nickles+"");
    		System.out.println("Dimes = "+dimes+"");
    		System.out.println("Quarters = "+quarters+"");
    		System.out.println("Loonies = "+loonies+"");
    		System.out.println("Toonies = "+toonies+"");
        }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I may be over-complicating this...

    nickles = (nickles - 1)
    quarters = (quarters + 1)
     
    // can be shortened to:
    nickles--;      //  subtract one
    quarters++; // add one

    You need to spend more time on designing how the code should work considering the use of arithmetic operators and less on writing code.
    For example:
    123 / 100 = 1
    123 % 100 = 23
    Last edited by Norm; September 13th, 2012 at 05:26 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: I may be over-complicating this...

    Quote Originally Posted by Norm View Post
    nickles = (nickles - 1)
    quarters = (quarters + 1)
     
    // can be shortened to:
    nickles--;      //  subtract one
    quarters++; // add one

    You need to spend more time on designing how the code should work considering the use of arithmetic operators and less on writing code.
    For example:
    123 / 100 = 1
    123 % 100 = 23
    I'll look more into those after I post this comment. We've only learned the basics of the arithmetic operators at the moment so I don't anything other than +, -, /, %, *, unary +, unary - and the two new ways to use them as you've just showed me.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I may be over-complicating this...

    Take a value and manually reduce it to the change values. Start at the big valued coins and move down to pennies. If you have 57 then 57/10 = 5 and 57&10 = 7 7/5 = 1 & 7%5 = 2

    As you do the computations, add a println immediately following it to print out the results.
    Last edited by Norm; September 13th, 2012 at 05:45 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    tyeeeee1 (September 13th, 2012)

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I may be over-complicating this...

    Quote Originally Posted by tyeeeee1 View Post
    double exampleDouble = (exampleInterger / 10) // This would equal 4.5[/code]
    No it does not. Try it and see what it gives you.

    Quote Originally Posted by tyeeeee1 View Post
    Would there be a way to get everything on the left side of the decimal point and then save it to an integer, because if there is I believe I may be able to shorten the code tremendously.
    Seriously, try it.

  11. #10
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: I may be over-complicating this...

    Quote Originally Posted by jps View Post
    No it does not. Try it and see what it gives you.

    Seriously, try it.

    @jps O.o It equaled 4.0, this doesn't make much sense to me now... Could you explain what's going on jps?
    @Norm Thanks for all of the advice, I'll try writing up another version of the program with that in mind.

  12. #11
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: I may be over-complicating this...

    the reason it equals 4.0 is because the variable u r dividing by 10 is of type int... i dont think you know about typecasting yet but if u change 10 to 10.0 it should give u the answer you're looking for

  13. The Following User Says Thank You to C++kingKnowledge For This Useful Post:

    tyeeeee1 (September 14th, 2012)

  14. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I may be over-complicating this...

    So you have a variable of type double. That means you can store a value of type double in it. You can store a value of type integer in a type double because there is no loss of precision in the cast from integer to double. All that has to be done is add .0 to the integer, right? So the compiler does this cast for you.
    Now look at your line of code:
    double exampleDouble = (exampleInterger / 10);
    When the calculation happens, the right side is evaluated, and then assigned to the left side. So step one is taken as (exampleInteger/10). So here we have integer divided by integer. The math is done as integer math because they are both integers. This short-handed math just calculates the integer portion and never does the extra work of anything past the decimal. (Since there is no reason to do the work for integers, it just cuts out extra work). Once the answer 4 is returned, 4 is automatically casted to double, giving 4.0, which is what is assigned to exampleDouble. That is how you get 4.0 with 45/10

    If you really wanted the 4.5 you have to explicitly tell the compiler not to do integer math. There are several ways to do this, so the following examples will produce the same result of 4.5, but basically the rule is just to use at least one double in the calculation and you will get a double as a result.

    double exampleDouble = (exampleInterger / 10.0); //10.0 is a double value
    double exampleDouble = ((double)exampleInterger / 10); //exampleInteger is treated as a double value for the current operation, in this case the division

    What do you think the result will be from this line of code?

    double exampleDouble = (double)(exampleInterger / 10);
    Think about it and try it, see if you understand what is going on.

  15. The Following User Says Thank You to jps For This Useful Post:

    tyeeeee1 (September 14th, 2012)

  16. #13
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: I may be over-complicating this...

    Thanks for all the help everyone, I've just submitted the assignment to my teacher and hopefully I'll get 90+ =P

    @jps Your detailed example really helped, I don't think I would have easily figured out that all I needed to do was add '.0' at the end. Thanks for teaching me that.