Time for action - calculate interest on lump sum

For this calculation, we will allow the user to set the interest rate, but we'll assume a static starting amount of $15,000. You decide whether or not you want to calculate dollars, candy bars, pine cones, or something else entirely.

The formula to calculate simple interest is:

Interest Earned = Principal Amount * Interest Rate * Time

  1. Add a new sprite to the stage. Feel free to choose any sprite you want, but I'm going to select the fantasy13 sprite from the d folder. Name the sprite Lump, as in lump sum.
  2. Create three new for this sprite only variables for the Lump sprite, named lumpSum, interestEarned, and interestRate.
  3. Build the calculation for the Lump sprite. Add the when key pressed block to the Lump sprite's script area. Select the up arrow from the drop-down list.
  4. From the Variables palette, add the set to block to the when key pressed block. Select interestEarned from the variables list.
  5. First, calculate the interest on 15,000 for a time interval of 1 month. Add the multiplication block from the Operators palette to the set to block. Type 15000 in the first field of the multiplication block. This is our principal amount.
  6. Add another multiplication block to the second field of the first multiplication block.
  7. Type the number 1 in the second field of the multiplication block you just added. This is the value for the time interval.
  8. We need to turn the interest rate into a decimal value before we use it in our equation. Add a division block to the first field of the multiplication block. Type 100 as the divisor (the second field in the division block).
  9. From the Variables palette, add the interestRate block as the dividend.
  10. Now that we have the interest earned, we need to add it to the principal amount. Add a set to block to the end of the script. Select lumpSum from the list of variables.
  11. Add an addition block to the set to block. Type 15000 in the first field. Add the interestEarned block to the second field.
  12. Press the up arrow to calculate the interest in our equation. If you look at the lumpSum monitor on the stage, it reports 15000 because we calculated a zero percent interest by default.
Time for action - calculate interest on lump sum

What just happened?

Mathematically speaking, there was nothing complex about this calculation. We divided the interest by 100 to convert the interest rate into a decimal value, which we then multiplied by our time interval of 1. Finally, we multiplied that amount by our starting amount, 15000, to get the total interest we earned.

We determined the final amount by adding the interest earned to the principal amount.

If you wanted to, you could have set up variables for the principal amount and the time interval. I chose to keep things simple in this calculation for demonstration purposes.

Have a go hero

Set some limits on the interestRate variable so that users can enter only a range, such as 0.1 to 10.0. Allow the user to set the interest rate via the slider.

After you set the min and max slider values for the interestRate variable, make the sprite say the lumpSum amount.

Run the script multiple times to test it out.

Round to nearest whole number

The interest calculation produces numbers to the nearest one hundredth value, as in 15602.59. There's nothing wrong with that, but since our other calculation reports whole numbers, we should be consistent and round off to the nearest whole number.

The Operators palette contains a round block that we can apply to any number. The following screenshot shows two acceptable ways to implement the round block in the context of the previous exercise:

Round to nearest whole number

Standard rounding rules apply. Values of .5 and higher will round up to the next whole number. For example, 6.5 becomes 7. Values less than .5 will round down to the nearest whole number. For example, 6.4 becomes 6.

Have a go hero

We have a reasonably complete program that compares two possible ways to accumulate money, but it needs some finishing touches. This hero exercise asks you to add those finishing touches:

  • Add a directions sprite to the stage that asks the user for an answer to the following problem: Would you rather earn 4% interest on $15,000 for one month or receive $1 that doubles every day for 15 days?
  • Change the Lump sprite to use a broadcast message as the control to calculate the interest amount instead of the up arrow.
  • Add a control to reset the interestRate variable on the Lump sprite when the flag is set.
  • Document some instructions in the project notes field.

The list could be endless. Feel free to customize this as you see fit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.177.14