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

Thread: Java Change Maker need help with rounding up the pennies

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Java Change Maker need help with rounding up the pennies

    So im new to programing and need help solving my program because my teacher never taught us about how to solve the issue. it works and compiles as of right now but if you run it. the issue is when i input for ex. .92 cents for the total bill and i input 1 dollar for tendered i need 8 cents but i only get 1 nickel and 2 pennies. she said because it rounds down the last penny.

    /**
    * Name: Salvatore LoCricchio
    * Date January 22, 2013
    * Class/Section: CIT160-03
    * Problem: Calcualte change
    *
    *
    * Sample Input: Enter bill total for .92 cents
    * Enter tendered in dollars 1 dollar
    *
    * Sample Output: Change = 8 cents
    */
    import java.util.Scanner;

    public class Locricchiochangmaker
    {
    public static void main(String args[])
    {
    float spent, tendered;
    int dollars,quarters, dimes, nickels, pennies,change;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Bill Total");
    spent = keyboard.nextFloat( );
    System.out.println("Tendered.");
    tendered = keyboard.nextFloat( );

    change = (int) ((tendered - spent)*100);


    dollars = (change/100);
    change = change%100;
    quarters = (change/25);
    change = change%25;
    dimes = (change/10);
    change = change%10;
    nickels = (change/5);
    change = change%5;
    pennies = (change);

    System.out.println(dollars + " dollars");

    System.out.println(quarters + " quarters");

    System.out.println(dimes + " dimes");

    System.out.println(nickels + " nickels");

    System.out.println(pennies + " pennies");
    }//end main
    }//end class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,951
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    Can you copy the contents of the console window and paste it here that shows the problem?

    Convert the amounts to cents before doing any math with them.

    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.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    Is this what your asking for? thanks for replying!




    /**
    	 * Name: Salvatore LoCricchio
    	 * Date January 22, 2013
    	 * Class/Section: CIT160-03
    	 * Problem: Calcualte change 
    	 * 
    	 * 
    	 * Sample Input: Enter bill total for .92 cents
    	 * 				 Enter  tendered in dollars 1 dollar
    	 * 
    	 * Sample Output: Change = 8 cents
    	 */
    import java.util.Scanner;
     
    public class Locricchiochangmaker
    {
        public static void main(String args[])
        {
            float spent, tendered;
             int dollars,quarters, dimes, nickels, pennies,change;
     
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Bill Total");
            spent = keyboard.nextFloat( );
            System.out.println("Tendered.");
            tendered = keyboard.nextFloat( );
     
    		change =  (int) ((tendered - spent)*100);
     
     
            dollars = (change/100);
            change = change%100;
            quarters = (change/25);
            change = change%25;
            dimes = (change/10);
            change = change%10;
            nickels = (change/5);
            change = change%5;
            pennies = (change);
     
            System.out.println(dollars + " dollars");
     
            System.out.println(quarters + " quarters");
     
            System.out.println(dimes + " dimes");
     
            System.out.println(nickels + " nickels");
     
            System.out.println(pennies + " pennies");
        }//end main
    }//end class




    Bill Total
    .92
    Tendered.
    1
    0 dollars
    0 quarters
    0 dimes
    1 nickels
    2 pennies

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,951
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    Did you see this:
    Convert the amounts to cents before doing any math with them.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    What do you mean? Convert to cents before I put it through the this:

    dollars = (change/100);
    change = change%100;
    quarters = (change/25);
    change = change%25;
    dimes = (change/10);
    change = change%10;
    nickels = (change/5);
    change = change%5;

    And how would I go about that if I'm wrong?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,951
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    Convert the contents of these to cents BEFORE doing any math: tendered and spent
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    i think that its done here:
    change = (int) ((tendered - spent)*100);

    where whats left is converted to pennies

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,951
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    That does the multiply and cast AFTER the subtraction. Do it BEFORE any math operations.

    Print out the value of change from that expression to see.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    i think this is what you mean if not it works



    /**
    	 * Name: Salvatore LoCricchio
    	 * Date January 22, 2013
    	 * Class/Section: CIT160-03
    	 * Problem: Calcualte change 
    	 * 
    	 * 
    	 * Sample Input: Enter bill total for .92 cents
    	 * 				 Enter  tendered in dollars 1 dollar
    	 * 
    	 * Sample Output: Change = 8 cents
    	 */
    import java.util.Scanner;
     
    public class Locricchiochangmaker
    {
        public static void main(String args[])
        {
            float spent, tendered;
             int dollars,quarters, dimes, nickels, pennies,change = 0;
     
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Bill Total");
            spent = keyboard.nextFloat( );
            System.out.println("Tendered.");
            tendered = keyboard.nextFloat( );
     
            spent = (spent*100);
            tendered = (tendered*100);
     
     
    		change =  (int) (tendered - spent);
     
     
            dollars = (change/100);
            change = change%100;
            quarters = (change/25);
            change = change%25;
            dimes = (change/10);
            change = change%10;
            nickels = (change/5);
            change = change%5;
            pennies = (change);
     
            System.out.println(dollars + " dollars");
     
            System.out.println(quarters + " quarters");
     
            System.out.println(dimes + " dimes");
     
            System.out.println(nickels + " nickels");
     
            System.out.println(pennies + " pennies");
        }//end main
    }//end class




    my out put is this :

    Bill Total
    .92
    Tendered.
    1


    0 dollars
    0 quarters
    0 dimes
    1 nickels
    3 pennies

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,951
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    Convert each of the variables contents to cents in an int variable BEFORE subtracting.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    im not understanding exactly what you mean exactly? im a noob to this

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,951
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Java Change Maker need help with rounding up the pennies

    Convert dollars in double to cents in int:
    int val = (int)(doubleVal*100);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. program : giving back change with dollars, dimes and pennies
    By hellynam in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 24th, 2012, 10:21 PM
  2. Rounding Question
    By FrozenFox in forum Algorithms & Recursion
    Replies: 1
    Last Post: July 23rd, 2012, 03:29 PM
  3. Game Maker Language and Game Maker and Zelda Classic thread
    By Fira in forum Other Programming Languages
    Replies: 3
    Last Post: April 17th, 2012, 09:59 AM
  4. Rounding the numbers
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 2nd, 2011, 01:45 AM
  5. Rounding an int. Help please
    By Akim827 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2010, 12:36 AM