Overview
The rate at which something grows along with the amount of time the growth is allowed to continue has a significant impact on the final result. A classic demonstration of exponential growth outpacing linear growth can be observed in the output produced by the solution to this laboratory assignment.
Details
Suppose you have a wealthy uncle who gives you two options for your inheritance. He will add to your inheritance every week until he dies according to one of two schemes:
- Starting with a given amount (such as $5000), he will add that amount to your total inheritance every week, or
- Starting with one penny, he will double your inheritance every week.
Option 2 will always outpace option 1 at some point in the future, but the question is when will option 2 be a better financial decision for you than option 1 and will it happen before your uncle dies?
For this lab you will be creating a game that explains the options briefly and allows a user to pick option 1 or option 2. For your game, you must use your creativity to describe a different scenario (not your uncle dying) to motivate the two options and the corresponding output. The starting amount for option 1 should be randomly generated (such as between 0 and 5000) each time the user runs the game. Also, the number of remaining weeks of life for your uncle (or whatever fits your scenario) should be randomly generated (such as between 0 and 40 weeks).
You should display the value of the inheritance for each option, each week until either:
- Option 2 exceeds option 1, or
- Your uncle dies
You should tell the user whether they "won" or not and how much they won or lost. They win if they chose the option that causes them to end up with more money. They lose if they chose the option that leaves them with less money. The amount won or lost is based on the total number of weeks before your uncle dies, not the number of weeks that are displayed. For instance, in the example program run below, the amount the user missed out on is $21,404,654.73 (the difference between linear and exponential at 32 weeks) not $31,249.77 (the difference between linear and exponential for the last week displayed (week 24)).
Lastly, you should give the user the option of playing the game again every time they see their results.
Here is an example run of the program:
[[Explanation of the program deleted - replace this with a summary of your scenario]] Enter which option you would prefer: 1 Week 1 Linear: $2193.18, Exponential: $0.01 Week 2 Linear: $4386.36, Exponential: $0.02 Week 3 Linear: $6579.54, Exponential: $0.04 Week 4 Linear: $8772.72, Exponential: $0.08 Week 5 Linear: $10965.90, Exponential: $0.16 Week 6 Linear: $13159.08, Exponential: $0.32 Week 7 Linear: $15352.26, Exponential: $0.64 Week 8 Linear: $17545.44, Exponential: $1.28 Week 9 Linear: $19738.62, Exponential: $2.56 Week 10 Linear: $21931.80, Exponential: $5.12 Week 11 Linear: $24124.98, Exponential: $10.24 Week 12 Linear: $26318.16, Exponential: $20.48 Week 13 Linear: $28511.34, Exponential: $40.96 Week 14 Linear: $30704.52, Exponential: $81.92 Week 15 Linear: $32897.70, Exponential: $163.84 Week 16 Linear: $35090.88, Exponential: $327.68 Week 17 Linear: $37284.06, Exponential: $655.36 Week 18 Linear: $39477.24, Exponential: $1310.72 Week 19 Linear: $41670.41, Exponential: $2621.44 Week 20 Linear: $43863.59, Exponential: $5242.88 Week 21 Linear: $46056.77, Exponential: $10485.76 Week 22 Linear: $48249.95, Exponential: $20971.52 Week 23 Linear: $50443.13, Exponential: $41943.04 Week 24 Linear: $52636.31, Exponential: $83886.08 Your uncle dies after 32 weeks so you [[Update language for your scenario]] got unlucky and missed out on $21404654.73. Do you want to play again? (1 for yes) or (0 for no) 1 [[Explanation of the program deleted - replace this with a summary of your scenario]] Enter which option you would prefer: 1 Week 1 Linear: $1873.25, Exponential: $0.01 Week 2 Linear: $3746.50, Exponential: $0.02 Week 3 Linear: $5619.75, Exponential: $0.04 Week 4 Linear: $7492.99, Exponential: $0.08 Week 5 Linear: $9366.24, Exponential: $0.16 Week 6 Linear: $11239.49, Exponential: $0.32 Week 7 Linear: $13112.74, Exponential: $0.64 Week 8 Linear: $14985.99, Exponential: $1.28 Week 9 Linear: $16859.24, Exponential: $2.56 Week 10 Linear: $18732.49, Exponential: $5.12 Week 11 Linear: $20605.74, Exponential: $10.24 Week 12 Linear: $22478.98, Exponential: $20.48 Week 13 Linear: $24352.23, Exponential: $40.96 Your uncle dies after 14 weeks so you [[Update language for your scenario]] got lucky and ended up with an extra $26143.56. Do you want to play again? (1 for yes) or (0 for no) 0
Just For Fun
Feel free to add additional features to make the program more interesting but be sure to complete all of the requirements first. If you have ideas to improve the program that don't comply completely with the specified requirements, consult with your instructor first.
Notes
- You must place your program in a class called
GrowthRate
. - You must place your name and a short description of your program in a comment at the top of the file.
- You must use meaningful variable names and indentation appropriate to the "good" style demonstrated in class.
- To get the output to format with two decimal places, you can use the following code:
System.out.format("Week %2d Linear: $%.2f\n", numberOfWeeks, linearAmount);
- To generate a random number between 0.0 and 1.0 you can use call the following method:
Math.random()
.
Acknowledgement
This laboratory assignment was developed by Dr. Derek Riley.