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:
Code :
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:
Code :
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
Re: Program to calculate pay and total sum with daily increment. Urgent!
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.
Re: Program to calculate pay and total sum with daily increment. Urgent!
Quote:
Originally Posted by
Mr.777
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..?
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?
Re: Program to calculate pay and total sum with daily increment. Urgent!
Quote:
Originally Posted by
ppme
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.
Re: Program to calculate pay and total sum with daily increment. Urgent!
Quote:
Originally Posted by
ePerKar3
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
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..
Re: Program to calculate pay and total sum with daily increment. Urgent!
New variable, "total"
total += amount;
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."
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
}
Re: Program to calculate pay and total sum with daily increment. Urgent!
Quote:
Originally Posted by
ppme
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
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.