CS 100  Programming I
Stepwise Refinement for Project 0

Revision Date: January 8, 2017

Stepwise-refinement

Printable Version


Attacking the problem

Stepwise refinement is the process of steadily building up a solution by solving one small piece of the problem at a time. For this class, we've organized each piece of the problem into levels, where each level builds upon the last. If you need assistance with a particular level, you must first demonstrate that you've successfully completed the previous level.

Level 0
On a sheet of paper, figure out how to calculate the solution. If you use the following values:

Then you should calculate the following answers:

1028.5 Yearly cost of gasoline without modification
970.35 Yearly cost of gasoline with modification
0.9446 Number of years for pay off (decimal)
0 years and 344 days Number of years and days for pay off (integers)

If you don't have a calculator on hand, you can use the python3 interpreter as a simple calculator.

Example:

>>> 10 / 2
5.0
>>> 5.0 * 12
60.0
>>> 60 / 8
7.5
Level 1
For each mathematical operation performed in Level 0, assign the value of that operation to a variable (still just on paper). For example, if you add two numbers, SalesDay1 + SalesDay2, then you might call that value totalSales. Do this for each math operation you performed in the previous step (every instance of *, /, +, or -).

So, for example, if you needed to calculate the average sales over three days, given SalesDay1, SalesDay2, and SalesDay3, you would write something like the following down on your sheet of paper:

Subtotal = SalesDay1 + SalesDay2
Total = Subtotal + SalesDay3
Average = Total / 3.0

For this level, You should not have multiple operations such as:

Average = (SalesDay1 + SalesDay2 + SalesDay3) / 3.0

Level 2
In your project0 directory, create a file called level2.py. In this file, write code which will prompt the user for the 5 numbers needed to perform the project's calculation. Then print out each of those variables with a useful description. E.G. print("Miles per year given:", milesPerYear)
Level 3
Copy the contents of level2.py into a file called level3.py (unix command: cp level2.py level3.py). In this file, add new code to perform each step of the calculation for finding the yearly cost of gasoline without the modification. Use the same step by step approach you used in Level 1. Print out each intermediate value with an appropriate description. Once you have calculated the final value and assigned it to an appropriately name variable, print out the final value with an appropriate description.
Level 4
Copy the contents of level3.py into a file called level4.py. In this file, add new code to perform each step of the calculation for finding the yearly cost of gasoline WITH the modification. Use the same step-by-step approach you used in Level 1. Print out each intermediate value with an appropriate description. Once you have calculated the final value and assigned it to an appropriately name variable, print out the final value with an appropriate description.
Level 5
Copy the contents of level4.py into a file called level5.py. In this file, add new code to perform each step of the calculation for finding the time until pay off in years. Use the same step-by-step approach you used in Level 1. Print out each intermediate value with an appropriate description. Once you have calculated the final value and assigned it to an appropriately name variable, print out the final value with an appropriate description.
Level 6
Copy the contents of level5.py into a file called level6.py. In this file, add new code to perform each step of the calculation for converting the number of decimal years into an integer number of years and days. Use the same step-by-step approach you used in Level 1. Print out each intermediate value with an appropriate description. Once you have calculated the final value and assigned it to an appropriately name variable, print out the final value with an appropriate description.
Level 7
At this point, your program is fully functional. However, it is not ready for submission. The next levels will be focus on the reorganization of the code to fit the project description. Copy the contents of level6.py into a file called level7.py. In this file, wrap the entire contents of Level 6's code in a function called main. Then call this function below its definition. Run your program and make sure it still works.
Level 8
Copy the contents of level7.py into a file called level8.py. Create a file called mpg.py. In mpg.py, copy the code created in Level2 into a new function definition, name the function appropriately. Specifically, we are referring to the code which prompts for 5 numbers and prints them out. Also, now that this code is in a function, it must return the values that it gets from the user. In level8.py, import the entire contents of mpg.py. Then remove the prompt code in level8.py, and instead, use the function you just defined in mpg.py.
Level 9
Copy the contents of level8.py into a file called level9.py. In mpg.py, copy the code to calculate the yearly cost of gasoline without modification into a new function definition, name the function appropriately. Remove that code from the main function in level9.py. Instead, use the function you just defined in mpg.py to calculate the value. Make sure you are still printing that value in the main function of level9.py.
Level 10
Copy the contents of level9.py into a file called level10.py. In mpg.py, copy the code to calculate the yearly cost of gasoline WITH modification into a new function definition, name the function appropriately. Remove that code from the main function in level9.py. Instead, use the function you just defined in mpg.py to calculate the value. Make sure you are still printing that value in the main function of level10.py.
Level 11
Copy the contents of level10.py into a file called level11.py. In mpg.py, copy the code for calculating the time until pay off in years into a new function definition, name the function appropriately. Remove that code from the main function in level9.py. Instead, use the function you just defined in mpg.py to calculate the value.
Level 12
Copy the contents of level11.py into a file called level12.py. In the main function of level12.py, use the calculated value of years in decimals to calculate the number of years and days to which it is equivalent. Print out the years and days.
Level 13
In mpg.py, comment out any lingering print statements found in your functions. Additionally, make sure there are NO code statements which are not within a function definition. Run your code to verify that it still works. Once you've verified that it still works, remove the commented-out print statements from mpg.py. If desired, you may now condense your math formulas and remove the intermediate steps in your functions. Again, make sure your program works after you do this.
Level 14
Copy the contents of level12.py into a file called payoff.py. Comment-out any extraneous print statements. Run your program and make sure it still works. Then remove the commented-out print statements from payoff.py. Your program should now print 3 things:

lusth@cs.ua.edu