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 6 of 6

Thread: Math Remainder Issue

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    Florida
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Math Remainder Issue

    Hi, everyone. I would like to take the time to thank you all in advance for the time you take to review and assist with my dilemma(s). This is my first post and I apologize if I make any errors pertaining to the prescribed forum guidelines. I appreciate any criticism!

    I began my Java journey buy reading a free online pdf textbook about programming in Java. I am half way through and now have an elementary understanding of the basics of Java. I am trying to master the resources I have read through creating programs including each section. The program I am trying to code is a simple change counter where the user gives a value (i.e. $232.75) and the program gives them the breakdown of bills that will be given back in change. This is a similar concept as to what a modern cash register does. I have written the code and anytime the input consists of something other than 100's or 20's, the program displays a value of 0 for the rest of forms of money such as quarters, 10's, pennies...

    import java.util.Scanner;
     
    public class Change {
     
    	private static Scanner input;
     
    	public static void main(String[] args) {
    		input = new Scanner (System.in);
     
    		double money; //User specified value to convert
    		int change; //Converted "Money" to int type
    		int hund, twend, tend, fived, oned, quarters, dimes, nickels, pennies; //The forms change can be given in
    		int afthund, afttwend, afttend, aftfived, aftoned, aftquarters, aftdimes, aftnickels; //Money left over following each form of change
     
    		System.out.println("How much money would you like me to give you change for?"); //Ask the user for a value
    		money = input.nextDouble();
    		money = money * 100; //Multiply double "money" by 100 to later work with integers
    		change = (int)(money); //Typecast double "money" to an int value
     
    			hund = change / 10000; //Divide change by 10,000 (hund) -- hundred dollars
    			afthund = change % 10000; //then get amount left over (afthund) -- after hundred dollars
     
    			twend = afthund / 2000; //Divide afthund by 2000 (twend)
    			afttwend = twend % 2000; //then get amount left over (afttwend)
     
    			tend = afttwend / 1000; //Divide aftwend by 1000 (tend)
    			afttend = tend % 1000; //then get amount left over (afttend)
     
    			fived = afttend / 500; //Divide afttend by 500 (fived)
    			aftfived = fived % 500; //then get amount left over (aftfived)
     
    			oned = aftfived / 100; //Divide aftfived by 100 (oned)
    			aftoned = oned % 100; //then get amount left over (aftoned)
     
    			quarters = aftoned / 25; //Divide aftoned by 25 (quarters)
    			aftquarters = quarters % 25; //then get amount left over (aftquarters)
     
    			dimes = aftquarters / 10; //Divide aftquarters by 10 (dimes)
    			aftdimes = dimes % 10; //then get amount left over (aftdimes)
     
    			nickels = aftdimes / 5; //Divide aftdimes by 5 (nickels)
    			aftnickels = nickels % 5; //then get amount left over (aftnickels)
     
    			pennies = aftnickels; //Remainder is pennies
     
    			System.out.print("Hundreds: " + hund); //Print hundreds
    			System.out.println();
     
    			System.out.print("Twenties: " + twend); //Print twenties
    			System.out.println();
     
    			System.out.print("Tens:     " + tend); //Print tens
    			System.out.println();
     
    			System.out.print("Fives:    " + fived); //Print fives
    			System.out.println();
     
    			System.out.print("Ones:     " + oned); //Print ones
    			System.out.println();
     
    			System.out.print("Quarters: " + quarters); //Print quarters
    			System.out.println();
     
    			System.out.print("Dimes:    " + dimes); //Print dimes
    			System.out.println();
     
    			System.out.print("Nickels:  " + nickels); //Print nickels
    			System.out.println();
     
    			System.out.print("Pennies:  " + pennies); //Print pennies
    			System.out.println();
     
    	}
     
     
    }

    I have provided as much documentation as possible. If you have any ideas for a solution or if I need to start over, please let me know.
    Have a great day,
    bzhagar


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

    Default Re: Math Remainder Issue

    Try debugging the code by printing out the results of each computation with a println() statement so you can see the results of each expression. Be sure to add labels to all the printouts. Something like this:
            System.out.println("ae="+ae);
    Change ae to the names of the variables used in your code.

    If you can't understand what is happening in the code, copy and paste here what is printed out by the added printlns and ask questions about the results that you don't understand.

    displays a value of 0
    A common reason for that is the code is using integer arithmetic: 4/10 = 0


    Please copy and paste here the console from when you execute the program that shows the input and its output.
    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Last edited by Norm; September 12th, 2012 at 01:31 PM.
    If you don't understand my answer, don't ignore it, ask a question.

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

    bzhagar (September 12th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Location
    Florida
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math Remainder Issue

    I am glad to say that given the wise advice of Norm, I came up with a working program. I had to test a few things before finally getting the program right.
    1. The main problem I was experiencing was due to the integer arithmetic issue -- I solved this by simply making the variables of type double
    2. I was unable to carry the variables to the next expression as well as print them in the same form -- My solution was to create a third set of variable to specifically print from.
    3. Simple mathematical errors

    Now my problem is that when I debug the code by printing out each set of computations, I recieve long decimals that are a fraction off of the true value. Such as "2.6999999999999886" when it should really be "2.70".

    import java.util.Scanner;
     
     
    public class Change {
     
    	private static Scanner input;
     
    	public static void main(String[] args) {
    		input = new Scanner (System.in);
     
    		double change; //User input value
    		double hund, twend, tend, fived, oned, quarters , dimes, nickels;
    		double afthund, afttwend, afttend, aftfived, aftoned, aftquarters, aftdimes, aftnickels;
    		double hundreds, twenties, tens, fives, ones, twentyfivecents, tencents, fivecents;
     
    		System.out.println("How much money would you like me to give you change for?");
    		change = input.nextDouble();
     
    			hund = change; 
    			afthund = hund % 100.00; 
    			hund = hund - afthund;
    			hundreds = hund / 100;
     
    			System.out.println("lna1= " + hund);
    			System.out.println("lna2= " + afthund);
    			System.out.println();
     
     
    			twend = afthund;
    			afttwend = twend % 20.00;
    			twend = twend - afttwend;
    			twenties = twend / 20;
     
    			System.out.println("lnb1= " + twend);
    			System.out.println("lnb2= " + afttwend);
    			System.out.println();
     
     
    			tend = afttwend; //Divide aftwend by 10.00 (tend)
    			afttend = tend % 10.00; //then get amount left over (afttend)
    			tend = tend - afttend;
    			tens = tend / 10;
     
    			System.out.println("lnc1= " + tend);
    			System.out.println("lnc2= " + afttend);
    			System.out.println();
     
     
    			fived = afttend;
    			aftfived = fived % 5.00;
    			fived = fived - aftfived;
    			fives = fived / 5;
     
    			System.out.println("lnd1= " + fived);
    			System.out.println("lnd2= " + aftfived);
    			System.out.println();
     
     
    			oned = aftfived;
    			aftoned = oned % 1.00;
    			oned = oned - aftoned;
    			ones = oned / 1.00;
     
    			System.out.println("lne1= " + oned);
    			System.out.println("lne2= " + aftoned);
    			System.out.println();
     
     
    			quarters = aftoned;
    			aftquarters = quarters % .25;
    			quarters = quarters - aftquarters;
    			twentyfivecents = quarters / .25;
     
    			System.out.println("lnf1= " + quarters);
    			System.out.println("lnf2= " + aftquarters);
    			System.out.println();
     
     
    			dimes = aftquarters;
    			aftdimes = dimes % .10;
    			dimes = dimes - aftdimes;
    			tencents = dimes / .10;
     
    			System.out.println("lng1= " + dimes);
    			System.out.println("lng2= " + aftdimes);
    			System.out.println();
     
     
    			nickels = aftdimes;
    			aftnickels = nickels % .05;
    			nickels = nickels - aftnickels;
    			fivecents = nickels / .05;
     
    			System.out.println("lnh1= " + nickels);
    			System.out.println("lnh2= " + aftnickels);
    			System.out.println();
     
     
    			System.out.print("Hundreds: " + (int)hundreds); //Print hundreds
    			System.out.println();
     
    			System.out.print("Twenties: " + (int)twenties); //Print twenties
    			System.out.println();
     
    			System.out.print("Tens:     " + (int)tens); //Print tens
    			System.out.println();
     
    			System.out.print("Fives:    " + (int)fives); //Print fives
    			System.out.println();
     
    			System.out.print("Ones:     " + (int)ones); //Print ones
    			System.out.println();
     
    			System.out.print("Quarters: " + (int)twentyfivecents); //Print quarters
    			System.out.println();
     
    			System.out.print("Dimes:    " + (int)tencents); //Print dimes
    			System.out.println();
     
    			System.out.print("Nickels:  " + (int)fivecents); //Print nickels
    			System.out.println();
     
    			System.out.print("Pennies:  " + (int)aftnickels); //Print pennies
    			System.out.println();
     
    	}
     
     
    }

    Strangely this does not affect the end result (even if the values are not typecasted into integers..)
    ** Except when there are pennies involved!

    Much appreciated,
    bzhagar
    Last edited by bzhagar; September 12th, 2012 at 04:30 PM.

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

    Default Re: Math Remainder Issue

    You should leave the variables as ints and scale them up to the number of cents (*100) vs the number of dollars to keep from having values such as 2.699999999999988

    The code needs more testing. Try this value:
    		change = 237.77; //input.nextDouble();


    NOTE: Your labels on the print out are not the name of the values being printed:
    System.out.println("lna1= " + hund);
    vs
    System.out.println("hund= " + hund);
    Last edited by Norm; September 12th, 2012 at 04:37 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Location
    Florida
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math Remainder Issue

    Went back to the original idea of using integers and a little R & D. Now have a fully functioning, bug free program. Maybe I'll make a GUI for it...
    Thanks for the immensely helping advice, Norm. You helped me not only better my program, but also my knowledge of Java by making me explore the possibilities and guiding me when needed.
    Till next time!

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

    Default Re: Math Remainder Issue

    I'm glad you got it working. See you next time.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  2. [SOLVED] Making Binary Converter script from scratch, running into math issue.
    By mwebb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 8th, 2011, 07:47 PM
  3. Remainder Assignment Operator Help
    By jsam0123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2011, 06:04 PM
  4. Chinese Remainder therom
    By Joy123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 16th, 2011, 07:11 AM
  5. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM