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
Write a program that produces output similar to the following examples:
You've been hiking all your life. One day, you come across a fork in the trail
that you're sure wasn't there before. Never one to stray from adventure, you
boldly take the left path. As you progress, the forest around you starts to
change: bark becomes smooth, leaves take on a purple hue, and the ground
itself squishes beneath your boots. As you take in your surroundings, an
ethereal voice seems to come from all directions at once.
"Greetings, traveler."
"We have been watching you for a long time... You are not like other humans;
you only have 9 toes. This pleases us greatly. As a token of our admiration,
we will grant you wealth beyond your wildest dreams! So long as you live, we
will give you the first share of every harvest: the finest rutabagas; far
greater than any your kind has ever known!"
"We will grant this boon one of two ways: you may either opt to receive
372 bushels of rutabagas each year, or you may choose to receive a single
bushel this year, but we will double the amount with each harvest.
Choose wisely!"
Enter "1" if you would prefer to receive 372 bushels each year,
or enter "2" if you would rather risk exponential growth: 1
Total rutabagas received after N harvests
=========================================
Harvest 1 Linear: 372, Exponential: 1
Harvest 2 Linear: 744, Exponential: 3
Harvest 3 Linear: 1116, Exponential: 7
Harvest 4 Linear: 1488, Exponential: 15
Harvest 5 Linear: 1860, Exponential: 31
Harvest 6 Linear: 2232, Exponential: 63
Harvest 7 Linear: 2604, Exponential: 127
Harvest 8 Linear: 2976, Exponential: 255
Harvest 9 Linear: 3348, Exponential: 511
Harvest 10 Linear: 3720, Exponential: 1023
Harvest 11 Linear: 4092, Exponential: 2047
You live to see 11 harvest(s) so you got lucky and
will end up with an extra 2045 bushels of rutabagas!
Do you want to play again? (Y/N)
N
The scenario should outline a situation in which the user is to receive a payout of items such as tacos, cats, donuts, etc at the end of a number of cycles such as weeks, days, years, etc. The user is prompted to choose between a linear (option 1) or exponential (option 2) payout. The linear payout will pay an amount the first cycle, and then double that amount the second cycle, triple the third cycle, and on and on until the end of the scenario. The range of the linear amount will depend on the scenario, but it should be generated at the start and mentioned somewhere in the prompt. The exponential amount, will start at 1 the first cycle and then add an exponential amount (2, 4, 8, 16, ...) each cycle thereafter. The number of cycles of the scenario should be a random number from 1 to 30 and should NOT be told to the user at the start.
After the user makes their choice, the program should print out the payout for each option in a nicely formatted table as shown. In the table shown, each row displays the total number of rutabagas received after N number of harvests. For example, for harvest 3, linear is 1116 (372 three times), and expontential is 7 (1 bushel the first harvest, 2 bushels the second harvest, and 4 bushels the third harvest for a total of 7 bushels).
At the end of the printout, the program should tell user how many cycles the scenario lasted, whether they won by picking the option that produced the highest value at the end, and how much they won or lost by.
After, the user should be prompted to play again.
...
Enter "1" if you would prefer to receive 508 bushels each year,
or enter "2" if you would rather risk exponential growth: 1
Total rutabagas received after N harvests
=========================================
Harvest 1 Linear: 508, Exponential: 1
Harvest 2 Linear: 1016, Exponential: 3
Harvest 3 Linear: 1524, Exponential: 7
Harvest 4 Linear: 2032, Exponential: 15
Harvest 5 Linear: 2540, Exponential: 31
Harvest 6 Linear: 3048, Exponential: 63
Harvest 7 Linear: 3556, Exponential: 127
Harvest 8 Linear: 4064, Exponential: 255
Harvest 9 Linear: 4572, Exponential: 511
Harvest 10 Linear: 5080, Exponential: 1023
Harvest 11 Linear: 5588, Exponential: 2047
Harvest 13 Linear: 6096, Exponential: 4095
Harvest 14 Linear: 6604, Exponential: 8191
You live to see 20 harvest(s). Your poor choice cost
you 1038415 bushels of rutabagas!
Do you want to play again? (Y/N)
N
If the user enters Y to the last question, the entire program should run again with a newly generate linear amount and number of cycles.
Notice that here the user saw 20 harvests, but only 14 of them are shown in the output. That is because once the Exponential option exceeds the linear option, your program should no longer show the total bushels of rutabagas accumulated for each option; however, your program still needs to calculate the accumulations so that it can determine how many more bushels of rutabagas more the exponential option had than the linear option.
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 generate a random number between 0.0 inclusive and 1.0 exclusive you can use call the following method:
Math.random()
.
Just For Fun
Ambitious students may want to:
- Align the numerical output so that it looks like:
Xxxxxxx 9 Linear: 9000, Exponential: 511
Xxxxxxx 10 Linear: 10000, Exponential: 1023
- Place comma separators every three digits:
10,000
instead of10000
. - Add a third growth factor (e.g., n2).
If you have ideas to improve the program that don't comply completely with the specified requirements, consult with your instructor first.
Acknowledgement
This laboratory assignment was developed by Dr. Derek Riley and the CSC1110 instructors.