CMPT14x Lab 00: Template Lab Solution
Problem:
Compute the amount there will be in my bank account after one year if
the interest is compounded quarterly and any withdrawals I make are entered
immediately after the interest is paid.
Problem Suitability:
The bank account example given above is well within the reach of a simple
calculator, and is therefore of somewhat marginal suitability for computer
solution. However, the solution does illustrate how a number of the
programming ideas discussed in chapter one are actually implemented in Modula-2.
Problem Restatement:
- Given: A bank account, possibly containing money.
- To Do: Compute interest, make withdrawals every 3 months.
- Desired Result: Final balance.
- Formula: interest = principal * rate * time
Libraries:
This problem has both input and output requirements. It will, therefore
make use of the standard I/O Modula-2 libraries STextIO, SWholeIO, and
SRealIO. Because of the withdrawals, four separate simple interest
calculations will be made, rather than one compound interest calculation. No
mathematical or financial libraries are required.
Problem Refinement (natural language pseudocode):
- Input Section:
- obtain bank balance
- obtain interest rate
- obtain quarterly withdrawal amounts
- Computation:
- calculate interest each quarter
- subtract withdrawal each quarter
- Output:
Second Refinement (natural language pseudocode):
- Input Section:
- Ask the user for the initial balance
- Read the balance and assign the value to a real variable
- Ask the user for the interest rate
- Read the rate and assign the value to another real variable
- Computation:
- Set quarter to one
- Repeat: For the current quarter:
- Compute the interest using interest = principal * rate * time
- Add this to the original balance: balance = balance + interest
- Ask user for widthdrawl amount
- Read the amount and assign the value to a real variable
- Subtract the withdrawal from last balance:
balance = balance - withdrawal
- add one to the quarter until quarter is greater than four
- Output:
- Print out the final balance
Pseudocode (third refinement; a fourth may also be necessary):
In this refinement, only portions of the code that involve several steps,
formulas, or computations (i.e. an algorithm) are detailed. In this example,
the last natural language refinement is followed very closely, so this
section might have been left out in this case, but it is included to make the
model for students to follow as complete as possible.
- quarter := 1
- Repeat:
- interest := principal * rate * time
- balance := balance + interest
- write message "what is widthdrawl amount?"
- read amount
- balance := balance - widthdrawl
- quarter := quarter + 1
- Until quarter > four
Data Table:
Variables:
- principal, rate, interest, withdrawal: all reals
- quarter: cardinal
Constants:
Imports:
- From STextIO: WriteString, WriteLn, SkipLine;
- From SWholeIO: WriteCard;
- From SRealIO: ReadReal, WriteFixed;
Sample I/O:
Input:
- What is the opening balance? 1000.00
- What is the annual interest rate?
Enter as a decimal; Example: 6.5% is 0.065. 0.08
- How much do you wish to withdraw in quarter number 1? 10.00
- How much do you wish to withdraw in quarter number 2? 30.00
- How much do you wish to withdraw in quarter number 3? 12.10
- How much do you wish to withdraw in quarter number 4? 6.00
Output:
- The final balance in your account is 1044.57
User Manual:
Description:
Bank Interest is a small application designed to maintain a
simple bank account record. It allows the user to specify the opening balance,
and annual interest rate at the beginning of the program, and then to make
quarterly withdrawals. Interest is calculated on the balance before the
withdrawal.
Operation:
If in an MS-DOS or UNIX environment, type the name of the program from the
operating system prompt. If in a Graphics User Interface (GUI) environment,
click twice in close succession on the icon representing the program.
A banner will appear giving the name of the program and some
information about the author. Following this the prompt
What is the opening balance?
will appear on the screen. Type in the amount that the bank account had at
the start of the year. Following this, the prompt
What is the annual interest rate?
Enter as a decimal; Example: 6.5% is 0.065.
will appear on the screen. Type in the interest rate in the form shown.
Now, for each quarter, a message like
How much do you wish to withdraw in quarter number 1?
will appear on the screen. Respond with the amount being withdrawn for that
quarter in the form 10.23. That is, give the number in decimal form and
without a dollar sign. The program will finally respond with a message like:
The final balance in your account is 103.47
Code:
MODULE BankInterest;
(* Name: Nellie Hacker
* Student Number: 922001
* CMPT 140 Fall 2005
* Lab 00
* Bank Interest Computation
*)
FROM STextIO IMPORT
WriteString, WriteLn, SkipLine;
FROM SWholeIO IMPORT
WriteCard;
FROM SRealIO IMPORT
ReadReal, WriteFixed;
CONST
time = 0.25; (* i.e., one quarter *)
VAR
principal, rate, interest, withdrawl: REAL;
quarter: CARDINAL;
BEGIN
WriteString ("This program computes interest on an account");
WriteLn;
WriteString ("quarterly, and then allows for withdrawals.");
WriteLn;
WriteLn;
WriteString ("It was written by");
WriteLn;
WriteString ("Nellie Hacker");
WriteLn;
WriteString ("for CMPT 140");
WriteLn;
WriteString ("Lab 00 Due 14 Sep 2005");
WriteLn;
WriteLn;
WriteString ("What is the opening balance? ");
ReadReal (principal);
SkipLine;
WriteLn;
WriteString ("What is the annual interest rate? ");
WriteLn;
WriteString ("Enter as a decimal; E.g.: 6.5% is 0.065. == ");
ReadReal (rate);
SkipLine;
WriteLn;
quarter := 1;
WHILE quarter <= 4
DO
interest := principal * rate * time;
principal := principal + interest; (* compute new principal *)
WriteString ("What is the withdrawal in quarter number ");
WriteCard (quarter, 0);
WriteString ("? == "); (* complete a readable prompt *)
ReadReal (withdrawal);
SkipLine;
WriteLn;
principal := principal - withdrawal; (* recompute principal *)
quarter := quarter + 1;
END;
WriteString ("The final balance in your account is $ ");
WriteFixed (principal, 2, 0);
WriteLn;
END BankInterest.
Actual Output
This program computes interest on an account
quarterly, and then allows for withdrawals.
It was written by
Nellie Hacker
for CMPT 140
Lab 00 Due 14 Sep 2005
What is the opening balance? 1000.00
What is the annual interest rate?
Enter as a decimal; E.g.: 6.5% is 0.065. == 0.08
What is the withdrawal in quarter number 1? == 11.00
What is the withdrawal in quarter number 2? == 9.00
What is the withdrawal in quarter number 3? == 5.00
What is the withdrawal in quarter number 4? == 6.00
The final balance in your account is $1050.30
NOTE: An actual lab assignment submission should include several
sample runs with different input.