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

Thread: Mathematical problem with Money amounts

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Mathematical problem with Money amounts

    Hi All,
    And first of all thanks for taking the time to read my first post.

    I am working through a a java textbook and am stuck on this application problem :

     
     
    "Write an application that prompts for and reads a double value representing a monetary amount. Then determine the fewest number of each bill and coin needed to represent that amount, starting with the highest (assume that a ten dollar bill is the maximum size needed). For example, if the value entered is $47.63, then the program should print the equivalent amount as:
     4 ten dollar bills 
     1 five dollar bills
     2 one dollar bills
     2 quarters
     1 dimes
     0 nickels
     3 pennies   "
     
    I have tackled it by using starting with a total amount (eg 5678 dollars) and using division followed by remainder operator to work down to dollars then cents. (I am working in 10dollarnotes, 5dollarnotes, 1dollarcoin, 2dollarcoin, 10centpice and 20centpiece rather than the currency in the example).
     
    I have found this to work in easier examples but I'm hopeful there is a better way..... At the moment it prints the correct value of 10dollar notes, but FAR too many 2dollarcoins and NaN for cents. Also it doesnt allow me to scan the total so I can enter and rerun with a new total for the terminal. Here is what i'm puttin in and getting out :
     
     
    //RevCoinValueScan.java
    //application which prompts for and reads a monetary amount in dollars 
    //and cents. Then determines the fewest amount of 10dollar, 5dollar
    //notes,
    //5,10 cent 1 and 2dollar coins required to make that amount.
     
    import java.util.Scanner;
     
    public class RevCoinValueScan
    {
    public static void main (String[] args)
    {
      double note10;
      double dollar2;
      double dollar1;
      double cent10;
      double cent5;
     
      double total = 5678; 
      double total2; 
      double total3;
      double total4;
      double total5;
     
      Scanner scan = new Scanner (System.in);
     
      System.out.println ("Enter the monetary amount: " + total);
     
       note10 = total / 10;
       total2 = total % note10;
       dollar2 = total2 / 2;
       total3 = total2 % dollar2;
       dollar1 = total3 / 1;
       total4 = total3 % dollar1;
       cent10 = total4 / 0.10;
       total5 = total4 % cent10;
       cent5 = total5 / 5;
     
      System.out.println (" the total number of 10 dollar notes is: " + note10);
      System.out.println (" Two Dollar coins: " + dollar2);
      System.out.println (" One Dollar coins: " + dollar1);
      System.out.println (" Ten Cent coins: " + cent10);
      System.out.println (" Five Cent coins: " + cent5);
     
       }
    }

    AND HERES WHAT I'M GETTING OUT :

     
    1 error
    craig@craig-laptop:~/Documents/panda/java$ javac RevCoinValueScan.java
    craig@craig-laptop:~/Documents/panda/java$ java RevCoinValueScan
    Enter the monetary amount: 5678.0
     the total number of 10 dollar notes is: 567.8
     Two Dollar coins: 2.2737367544323206E-13
     One Dollar coins: 0.0
     Ten Cent coins: NaN
     Five Cent coins: NaN
    craig@craig-laptop:~/Documents/panda/java$ 
     
     
    ANOTHER AMOUNT : 
     
    craig@craig-laptop:~/Documents/panda/java$ javac RevCoinValueScan.java
    craig@craig-laptop:~/Documents/panda/java$ java RevCoinValueScan
    Enter the monetary amount: 786.908
     the total number of 10 dollar notes is: 78.6908
     Two Dollar coins: 2.8421709430404007E-14
     One Dollar coins: 0.0
     Ten Cent coins: NaN
     Five Cent coins: NaN
    craig@craig-laptop:~/Documents/panda/java$


  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: Mathematical problem with Money amounts

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Try debugging the code by printing out the values of the totalX variables so you can see what the code is working with.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Mathematical problem with Money amounts

    what the hell? It worked then i tried editing again and my whole post disappeared

  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: Mathematical problem with Money amounts

    It should be back now.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Mathematical problem with Money amounts

    Thanks Norm. I am not sure how to print out the values of the totalX variables but I will give it a try now:

      double total = 786.908; 
      double total2; 
      double total3;
      double total4;
      double total5;
     
     <script language="javascript">
    var total2
    document.write (total2); //prints the value of total2
    </script> 
     
     <script language="javascript">
    total3
    document.write (total3); //prints the value of total3
    </script> 
     
     <script language="javascript">
    total4
    document.write (total4); //prints the value of total4
    </script> 
     
     <script language="javascript">
    total5
    document.write (total5); //prints the value of total5
    </script>

    I JUST ENTERED THAT HALFWAY DOWN DOWN THE PROGRAM AFTER THE BIT WHERE YOU DECLARE THE NUMBERS AS 'DOUBLES'.

    AND GOT THIS RESULT :

    RevCoinValueScan2.java:25: error: > expected
     <script language="javascript">
             ^
    RevCoinValueScan2.java:26: error: ';' expected
    var total2
       ^
    RevCoinValueScan2.java:27: error: ';' expected
    document.write (total2); //prints the value of total2
            ^
    RevCoinValueScan2.java:28: error: illegal start of type
    </script> 
     ^
    RevCoinValueScan2.java:28: error: illegal start of expression
    </script> 
      ^
    RevCoinValueScan2.java:30: error: > expected
     <script language="javascript">
             ^
    RevCoinValueScan2.java:31: error: ';' expected
    total3
          ^
    RevCoinValueScan2.java:33: error: illegal start of type
    </script> 
     ^
    RevCoinValueScan2.java:33: error: illegal start of expression
    </script> 
      ^
    RevCoinValueScan2.java:35: error: > expected
     <script language="javascript">
             ^
    RevCoinValueScan2.java:36: error: ';' expected
    total4
          ^
    RevCoinValueScan2.java:38: error: illegal start of type
    </script> 
     ^
    RevCoinValueScan2.java:38: error: illegal start of expression
    </script> 
      ^
    RevCoinValueScan2.java:40: error: > expected
     <script language="javascript">
             ^
    RevCoinValueScan2.java:41: error: ';' expected
    total5
          ^
    RevCoinValueScan2.java:43: error: illegal start of type
    </script> 
     ^
    RevCoinValueScan2.java:43: error: illegal start of expression
    </script> 
      ^
    RevCoinValueScan2.java:43: error: not a statement
    </script> 
            ^
    RevCoinValueScan2.java:45: error: ';' expected
      Scanner scan = new Scanner (System.in);
             ^
    19 errors
    craig@craig-laptop:~/Documents/panda/java$

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Mathematical problem with Money amounts

    Firstly, try and make variable names that make better sense. dollar1 and dollar2 are not the best names. There are far too many totals as well.

    Are you trying to divide the money by the largest denomination (/ divide) and then continue using the remainder to divide by the next one down? Maybe set up some sort of 'fall-through' system in your code. Like a switch with no break.

  7. #7
    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: Mathematical problem with Money amounts

    Why the javascript code in the java code?
    <script language="javascript">
    var total2
    document.write (total2); //prints the value of total2
    </script>
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Mathematical problem with Money amounts

    Ok firstly thank you for your replies Norm and Starstreak. And secondly please excuse me for responding infrequently to this thread because I have very limited internet connection at the moment due to my landlord. Anyway..
    Norm I'm not too sure what you mean by 'javascript code in the java code' - I think a few posts in this thread have disappeared because I tried something yesterday that you had recommended me I think - but I wasnt too sure what I was doing anyway.
    I will investigate a switch with no break and get back to you.
    Thanks guys

    --- Update ---

    Quote Originally Posted by Starstreak View Post

    Are you trying to divide the money by the largest denomination (/ divide) and then continue using the remainder to divide by the next one down?
    Yes that is precisely what i'm doing. I knew when I started that it surely isnt the best way to go about it. But its the only method I knew.

Similar Threads

  1. Creating method to change money?
    By j4ze in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 19th, 2012, 04:21 PM
  2. Deposit Money
    By spyxdaxworld in forum Java Theory & Questions
    Replies: 1
    Last Post: October 25th, 2012, 11:55 PM
  3. NEED HELP! converting change given to money denominatons
    By Mrod359 in forum Object Oriented Programming
    Replies: 1
    Last Post: June 15th, 2012, 05:30 PM
  4. Java:Evaluation of Mathematical Expression only from left to right only.
    By deepakl_2000 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 15th, 2011, 07:35 AM