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: Program to calculate pay and total sum with daily increment. Urgent!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Program to calculate pay and total sum with daily increment. Urgent!

    Hi, i'm working on the last of five programs i need to hand in tonight for an assignment and i'm completely stuck. The instructions are to create a JFrame Form program that calculates the money for someone who has "been hired to a very dangerous job in the amazon rainforest, your pay starts at 0,01 the first day and then doubles to 0,02 next day, then 0,04 etc. How many days must you survive before becoming a millionaire? Create a GUI that shows in a text area how much you make each day. Also show how many days you survived before becoming a millionaire."

    I'm supposed to do the calculation using a for loop, or if preferred, a while loop. The relevant part of the code i have at the moment is:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    DecimalFormat pay = new DecimalFormat("0.00");
     
    jTextArea1.setText("Day        " + "        Pay" +"\n");
        double amount = 0;
        double initial = 0.01;
        double rate = 1;
     
        // calculate amount on deposit for each of ten years
        for ( double day = 1; amount <= 1000000; day++ )
        {
     
            // calculate new amount for specified day
            amount = initial * Math.pow(1 + rate, day );
            // display the day and the amount
            jTextArea1.append( day + "                      " + pay.format(amount) + "\n");
     
        } // end for
     
    }

    The output is supposed to start at 0.01 on day 1 like i said, but mine starts at 0.02 and therefore takes one day less than it is supposed to if it was correct. What am i missing here?

    Output:
    Day                     Pay
    1.0                      0,02
    2.0                      0,04
    3.0                      0,08
    4.0                      0,16
    5.0                      0,32
    6.0                      0,64
    7.0                      1,28
    8.0                      2,56
    9.0                      5,12
    10.0                      10,24
    11.0                      20,48
    12.0                      40,96
    13.0                      81,92
    14.0                      163,84
    15.0                      327,68
    16.0                      655,36
    17.0                      1310,72
    18.0                      2621,44
    19.0                      5242,88
    20.0                      10485,76
    21.0                      20971,52
    22.0                      41943,04
    23.0                      83886,08
    24.0                      167772,16
    25.0                      335544,32
    26.0                      671088,64
    27.0                      1342177,28


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    What do you expect from?
    Math.pow(2,1)
    It will return you 2, not 1 as you want, So,
    Math.pow(2,0)
    will give you your desired result.
    Now look into your code. I hope this will help.

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

    ePerKar3 (December 5th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    Quote Originally Posted by Mr.777 View Post
    What do you expect from?

    It will return you 2, not 1 as you want, So,

    will give you your desired result.
    Now look into your code. I hope this will help.
    Sorry, but i still don't really get it. Maybe it's because i've been staring at this code for like 10 hours and i'm going mad, i don't know, but either way, i'm not sure what i am supposed to change in my Math.pow. Or is it in one of the declarations i need to make a change?

    I tried changing the value of "day" to 0, which gives me the right numbers on the pay side but then of course the days start counting form 0 at the same time...
    And changing the value of "rate" to 0 instead just makes the program go nuts.
    So, what to do..?
    Last edited by ePerKar3; December 5th, 2011 at 06:04 AM.

  5. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    Making "day" a global variable and putting this line : jTextArea1.append( day + " " + pay.format(amount) + "\n");
    Before the for loop should also fix the problem.

    Or an if statement in the loop: if(pay == 0.01)
    {
    jTextArea1.append( day + " " + pay.format(amount) + "\n");
    }

    Also, why is "day" a double?

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    Quote Originally Posted by ppme View Post
    Making "day" a global variable and putting this line : jTextArea1.append( day + " " + pay.format(amount) + "\n");
    Before the for loop should also fix the problem.

    Or an if statement in the loop: if(pay == 0.01)
    {
    jTextArea1.append( day + " " + pay.format(amount) + "\n");
    }

    Also, why is "day" a double?
    Well, i tried making day a global variable and putting the line "jTextArea1.append( day + " " + pay.format(amount) + "\n");" before the loop and when i do that it only displays one day and the money being 0,00. I also tried putting the if statement in the loop instead, and then i just got the "day" and "pay" words but no numbers whatsoever.

    And i changed "day" from a double to an int, that was just some silly mistake.

  7. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    Quote Originally Posted by ePerKar3 View Post
    Well, i tried making day a global variable and putting the line "jTextArea1.append( day + " " + pay.format(amount) + "\n");" before the loop and when i do that it only displays one day and the money being 0,00. I also tried putting the if statement in the loop instead, and then i just got the "day" and "pay" words but no numbers whatsoever.

    And i changed "day" from a double to an int, that was just some silly mistake.

    jTextArea1.append( day + " " + pay.format(amount) + "\n"); Outside and Inside the loop! Sorry should of been more clear

  8. The Following User Says Thank You to ppme For This Useful Post:

    ePerKar3 (December 5th, 2011)

  9. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    Ok i got the program so far to work, all i needed to do was change the line "amount = initial * Math.pow(1 + rate, day );" to "amount = initial * Math.pow(1 + rate, day-1 );"

    But now to the next part, to the right of that column there also has to be one that counts your total income thus far after each days earnings. How would i write a formula for that? I'm lost again... Looking at the pattern in the example i guess it should be like the current total is equal to the current day pay plus yesterdays total, but i have no idea how to do that..
    Last edited by ePerKar3; December 5th, 2011 at 06:53 AM.

  10. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    New variable, "total"

    total += amount;

  11. The Following User Says Thank You to ppme For This Useful Post:

    ePerKar3 (December 5th, 2011)

  12. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    I got the right column to work now too. Next step is to have a text at the bottom informing the user when he has earned more than one million in total and also when he earns more than one million per day. For example:

    "It takes me 27 days to earn over a million, 1342177,27 to be exact."

    "From day 28 i earn more than one million per day, 1342177,28 to be exact."
    Last edited by ePerKar3; December 5th, 2011 at 07:40 AM.

  13. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    total - 1mil > 0
    {
    tell users earned more than 1 mil
    }
    amount - 1mil >0
    {
    tell users more than 1 mil a day
    }

  14. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    Quote Originally Posted by ppme View Post
    total - 1mil > 0
    {
    tell users earned more than 1 mil
    }
    amount - 1mil >0
    {
    tell users more than 1 mil a day
    }
    Thanks a lot for all the help so far. Would you mind elaborating this last one just a little bit though or showing a bit more clearly what you meant? I know i'm being a handful, sorry 'bout that :p

  15. #12
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Program to calculate pay and total sum with daily increment. Urgent!

    Your "amount" variable holds the amount paid on one particular day. So if you take this amount and minus 1 million form it.. the pay must be greater than 1 million if > 0 holds true.

Similar Threads

  1. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 08:50 PM
  2. I trying to calculate total price.
    By Muhanguzi in forum JDBC & Databases
    Replies: 6
    Last Post: June 15th, 2011, 04:35 PM
  3. program wont renew total
    By that_guy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 01:00 PM
  4. How to get a System's total CPU usage percentage using a java program
    By userj2ee in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 01:28 AM
  5. Calculate federal taxes program! Help!
    By ocmjt in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 11th, 2010, 11:25 AM

Tags for this Thread