Program 1: Mortgage Problem w/o Error Checking |
Write a mortgage calculation program that provides text boxes (controls) for the user to enter a number of parameters (size of the loan, number of years, percent yearly interest rate ) and then calculates the size of the payment for each period as well as the total interest paid over the life of the loan.
Use text boxes for each of the input parameters and two labels for displaying the resulting size of the associated payment and the total interest. Use a command button labeled "Calculate Payment Size" to actually perform the calculations and an "Exit" button to exit the program. By prefacing the button text property with an "&" it allows the button to be activated by using the Alt key in combination with the letter that immediately follows the "&."
The TabIndex property of each control should be set so that when the program begins the focus is in the initial field, and also so that the focus moves from field to field in the proper order whenever the Tab key is hit. After the results of the calculations have been displayed, use the Exit button's Focus method to place the focus on the Exit button.
Remember to test your program by trying all different possible combinations of input. When testing your program, be sure to enter some incorrect data, such as text instead of numbers, decimal percentage rate instead of integer percentage rate, and unfilled fields to witness how the program behaves. The next programming assignment will result in a program that handles such problems--this program will not.
In this version of the program you will not be required to do any error checking, but in Part II extensive error checking will be incorporated. The program is divided into two parts so that you can see the complexity that detailed error checking can produce.
The following table provides a description of the variables used. Note that the variable names chosen clearly describe the purpose of the variables. Be sure to follow this guideline in each of your programs.
Variable Name |
Explanation |
paymentsPerYear |
number of payments per year |
loanAmount |
amount of loan |
paySize |
size of each payment |
percentInterest |
percent interest per year |
loanDuration |
number of years |
factor |
intermediate variable |
totalPayments |
total payments |
decIntPerPayment |
decimal interest per payment |
decIntPerYr |
decimal interest per year |
totalInt |
total interest paid |
The intermediate variable decIntPerPayment is a number between zero and one, not a percentage. In most cases the user knows the Annual Interest Rate rather than the decimal interest per payment. Thus you need a text box named txtPercentInterestPerYear, from which the program can derive values for the "single" variables decIntPerYr and decIntPerPayment. Use the formulas below:
Variable | Formula |
totalPayments | loanDuration multiplied by paymentsPerYear |
decIntPerYr | percentInterest divided by 100 |
decIntPerPayment | decIntPerYr divided by paymentsPerYear |
factor | 1 - (1 + decIntPerPayment)^{-totalPayments} decIntPerPayment |
paySize | loanAmount divided by factor |
totalInt | (paySize * paymentsPerYear * loanDuration) - loanAmount |
The number of payments per year should be set to 12.
NOTE: many students incorrectly translate the formula for factor into Visual Basic. The entire top portion of the formula is divided by decIntPerPayment. And (1+decintPerPayment) is raised to the power of –totalPayments.
The interface should look as much as possible like the image below.
Experiment with VB until you learn how to set the font to MS Sans Serif, font style to Bold, and font size to 10. In addition, figure out how to right-justify text in a text box.
Be sure that you code is documented thoroughly.
In all programs you should include a comment block at the beginning or
your program with your name, the assignment number, and a brief description of
the purpose of the program. You
should embed comments in your code whenever the purpose of subsequent lines may
be unclear to subsequent reviewers.
Note that this program follows the same structure that we saw in the warm-up program:
Turn in both a CD and a printout!!!
Hints:
Function
FormatCurrency
can be used to display a dollar sign, $, two places to the right of the
decimal point and commas after every three digits. FormatNumber and FormatPercent are also useful. |