Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Python | Schedule | Submit

Saint Louis University

Computer Science 150
Introduction to Object-Oriented Programming

Michael Goldwasser

Spring 2013

Dept. of Math & Computer Science

Programming Assignment 05

Interest Calculator

Due: 11:59pm, Tuesday, 5 March 2013


Contents:


Collaboration Policy

For this assignment, you must work individually in regard to the design and implementation of your project.

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: 1000000
The principal must be less than $1 million.

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.12             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 selection as the interest rate. That is, 3% returns 0.03, 5% returns 0.05, and so forth.
    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. 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.)
    2. You can align the output 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 Principal" question, accept 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 this functionality is very similar to the yesOrNo function that is described as exercise Practice 5.31 in the text (see solution in the appendix), except that our rule for what constitutes a yes or no is different.

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 10 points.


Extra Credit

Rather than displaying only the 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:

Month   Starting Balance    APR     Ending Balance
==================================================
Jan     $100.00             0.05    $100.41
Feb     $100.41             0.05    $100.82
Mar     $100.82             0.05    $101.23
Apr     $101.23             0.05    $101.64
May     $101.64             0.05    $102.05
Jun     $102.05             0.05    $102.47
Jul     $102.47             0.05    $102.89
Aug     $102.89             0.05    $103.31
Sep     $103.31             0.05    $103.73
Oct     $103.73             0.05    $104.15
Nov     $104.15             0.05    $104.57
Dec     $104.57             0.05    $105.00


Michael Goldwasser
Last modified: Wednesday, 13 March 2013