Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring

Saint Louis University

Computer Science 1300/5001
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2018

Computer Science Department

Programming Assignment 05

Interest Calculator

Due: 11:59pm, Monday, 29 October 2018


Contents:


Collaboration Policy

For this assignment, you are allowed to work with one other student if you wish (in fact, we suggest that you do so). If any student wishes to have a partner but has not been able to locate one, please let the instructor know so that we can match up partners.

Please make sure you adhere to the policies on academic integrity in this regard.


Overview

The interest calculator is an interactive program which prompts the user for an interest rate and a principal, then calculates and prints the balance that would be after one year.

For this project, in addition to producing the desired user interactions, we are requiring that your internal design make use of several functions, as defined later in this description.

This project also differs from earlier projects in that there is an increased focus on error checking. For past projects, we have allowed you to assume that all user input is valid. For this project, you must gracefully handle any input entered by the user.


Example Session

This interest calculator will ask you to select an interest rate,
followed by a principal value.  It will then calculate and display
the principal, interest rate, and balance after one year.  You will
then be invited to execute the process again or terminate.

Please select an interest rate:
A) 3%
B) 5%
C) 7%
D) 10%
Enter A-D: 5
That is not a valid selection.

Please select an interest rate:
A) 3%
B) 5%
C) 7%
D) 10%
Enter A-D: B

Enter the principal: $100

Initial Principal   Interest Rate   End of Year Balance
=======================================================
$100.00             0.05            $105.00

Another Computation [y/n]? y

Please select an interest rate:
A) 1%
B) 5%
C) 8%
D) 10%
E) 14%
F) 19%
Enter A-F: H
That is not a valid selection.

Please select an interest rate:
A) 1%
B) 5%
C) 8%
D) 10%
E) 14%
F) 19%
Enter A-F: D

Enter the principal: zero
Please enter a number

Enter the principal: -0.50
You must enter a positive amount.

Enter the principal: 1000001
The principal can be at most 10000000.

Enter the principal: $2500.925
The principal must be specified in dollars and cents.

Enter the principal: 2500.93

Initial Principal   Interest Rate   End of Year Balance
=======================================================
$2500.93            0.10            $2751.02

Another Computation [y/n]? n

Quitting program. Bye.

Requirements

This program is to use functions and do complete error checking and recovery.

Please note the following requirements:

  1. Create a function greeting() that displays the initial text. This function should be called once, when the program begins.

  2. Create a function getRate(choices) that manages the user interaction for selecting an interest rate from among a nonempty list of integer percentages.

    1. The percentage choices should be labeled with letters A, B, C, and so on, depending on the number of choices.
    2. The user should select a choice by indicating the appropriate label (e.g., B).
    3. Any other entry must be rejected:
      1. Print a polite rejection message.
      2. Redisplay the menu and re-prompt for a selection.
    4. Return the integer that was selected by the user.
    To make the main program interesting, each time you call getRate a list of between 2 and 6 choices, all taken as percentages between 1 and 19, can be generated as follows. Import the functions sample and randint from the random module as follows:
    from random import sample, randint
    Then you can generate a new list of choices to send as a parameter using the command
    sorted(sample(range(1,20), randint(2,6)))

  3. Create a function getPrincipal(limit) that collects and returns a floating point number from the user that represents the principal amount.

    1. The principal value must be strictly greater than zero and strictly less than the indicated limit.
    2. The user may enter the value as either an integer, or a floating-point value, but if as a floating-point number, at most 2 digits may be entered after the decimal point.
    3. When entering the principal value, the user may (but need not) place a dollar sign symbol $ in front of the principal value.
    4. If any of the above validations fail then issue a polite, informative message (as shown in the example session above) and re-prompt the user.
    5. Return the principal as a floating-point value.
    For the sake of this program, use $1 million as the limit.

  4. Create a function, computeBalance(principal, rate), that calculates and returns the year-end balance given the parameters.
    Use the formula:

    balance = principal + (principal * rate)

  5. Create a function to display the results as shown in the example session:

    1. Note well in the example that the first character of each displayed value must be perfectly aligned with the first character of the column header. For example in the very first example output, the "$" of the displayed $105.00 is aligned with the "E" in "End of Year Balance".
    2. We have not yet discussed string formatting in class, but you can use the command
      display = '%.2f' % value
      to generate a string representation of the floating-point value with two digits after the decimal point. (You can later print the display string as part of the output.)
    3. You can align the output, either by using string formatting operations, or by placing an appropriate number of spaces where desired. Remember that you can use a syntax, k*' ', to produce k consecutive spaces.

  6. For the "Another Computation" question, create a standalone function, askYesNo(prompt) that accepts any line that has either 'y' or 'Y' as the first non-blank character to indicate yes, and any line that has either 'n' or 'N' as the first non-black character to indicate no; any other response is inconclusive in which case the questions should be repeated.
    For example,

    	y
    	Yes
    	yah
    	
    would all be accepted to indicate yes.

    Note that the only responsibilities for this function are the user interactions involved in determining whether to return True (for yes) or False (for no). The job of restarting the process should remain within the primary flow of the script.

  7. The source code should be organized so that all of the funtion definitions are given at the beginning of the file, and all top-level code (i.e., that not within the body of a function) should be given at the end of the file (i.e., after all the functions have been defined).

In general, make sure that I cannot crash your program with any input I might give it. Seeing an exception is considered a crash. Don't forget to check conversions; int() and float() can raise exceptions.

Submitting Your Assignment

You should create a file, named interest.py, that contains all of your own code. This file must be submitted electronically.

You should also submit a separate 'readme' text file, as outlined in the general webpage on programming assignments.

Please see details regarding the submission process from the general programming web page, as well as a discussion of the late policy.


Grading Standards

The assignment is worth 40 points, apportioned as follows:


Extra Credit

In addition to displaying the required end-of-year balance, display twelve monthly balances, assuming that interest is compounded monthly. Treat the indicated interest rate as an Annual Percentage Rate (APR). An effective monthly interest rate can be computed using the following formula in Python:

monthly = pow(1+annual, 1.0/12) - 1
Applying that rate monthly should result in the same end-of-year balance as in the original computation.

Display your results as follows:

Initial Principal   Interest Rate   End of Year Balance
=======================================================
$950.00             0.12            $1064.00

Month   Starting Balance    APR     Ending Balance
==================================================
Jan     $ 950.00            0.12    $ 959.01
Feb     $ 959.01            0.12    $ 968.11
Mar     $ 968.11            0.12    $ 977.30
Apr     $ 977.30            0.12    $ 986.57
May     $ 986.57            0.12    $ 995.94
Jun     $ 995.94            0.12    $1005.39
Jul     $1005.39            0.12    $1014.93
Aug     $1014.93            0.12    $1024.56
Sep     $1024.56            0.12    $1034.28
Oct     $1034.28            0.12    $1044.09
Nov     $1044.09            0.12    $1054.00
Dec     $1054.00            0.12    $1064.00


Michael Goldwasser
Last modified: Sunday, 09 December 2018