Apr 17, 2009

I'm wanting a simple application that will take a user's input, consider the input, and output a result. If you remember in about 3rd grade, we were taught how to use "Math Machines" that could take a number, do something to it, and then output a result. What our application will do is very similar in concept.

1) Basic Math Machine: +3

Our application needs to take the user's input, add three to it, and output the result. Save this application as BasicMathMachine_PlusThree.

Input: -4, 0, 3Expected output:-4) -10) 33) 6

2) Basic Math Machine: +3 Loop 3

That would be a start to our masterpiece. Lets learn about a "for loop" next. After the user's input, add a for loop that goes from 0 to 2, counting by 1's. Move the calculation and output steps into the for loop. Save this application as BasicMathMachine_PlusThree_LoopThree.

Input: -4, 0, 3Expected output:-4) -1, 2, 50) 3, 6, 93) 6, 9, 12

3) Basic Math Machine: +x Loop 3

Story time! The government of an unnamed state once funded a business to write an application that would do some major math concerning state taxes. In their documentation for the creation of this application, they specified the tax rate that was current. They failed to realize in time that tax rates often change. Since they didn't ask for that part of the application to be a variable, it was hard coded.

Each time the tax rate changes, they had two options. Commission someone else to write a new application for them, or ask the business to alter it for them. This was unnecessarily expensive, much to the business's enjoyment.

Currently, our "+3" is hard coded into our application. Depending on what that three represents, hard code isn't necessarily bad. If we were calculating the circumference of a circle, for example, we would want to use pi. I'm going out on a limb, but pi isn't going to change in the near future.

Lets say that that three represents the size of shipments of our mysterious product. Business is growing, and we now have other options available for shipments. Modify the previous application so that we may alter the shipment size at run time. Save this application as BasicMathMachine_PlusX_LoopThree.

Input:x = -4, 0, 3s = 5

Expected output:-4) 1, 6, 110) 5, 10, 153) 8, 13, 18

4) Basic Math Machine: +/-x Loop y

Two more changes before our application is ready to be sold. The first is to un-hard code the number of shipments that occur, allowing the user to select how many times to loop at run time. Save this application as BasicMathMachine_PlusX_LoopY.

5EC) Extra Credit:

We've had a few unhappy customers who wish to ship some of our product back. Alas, we are only able to add to our total sales. Un-hard code the type of math we use, allowing for adding or subtracting the user's imput. Save this application as BasicMathMachine_PlusOrSubtractX_LoopY.