Homework 5
comp 125-609, Goldwasser
Due: 6:00pm Tuesday, October 26, 1999 (worth 5 points)
Late Option: if submitted between 6:00pm October 26 and 6:00pm November 2, you can recieve up to 3 of 5 points. No late homeworks will be accepted after 6:00pm November 2.
Purpose: Using General Procedures and Functions
Overview: Revisiting our mortgage calculator

Back in lab 3, we designed a simple mortgage calculator. The user was responsible for entering the following parameters (using appropriately labeled text boxes):

  • Amount of Loan
  • Annual Interst Rate
  • Length of Loan (in months)
  • In the original lab, we assumed that the values entered by the user were valid (i.e., there were numeric, and within a reasonable range). In this assignment, we will write code which explicitly ensures that input is valid.
    Program Specifics: Your form should contain three text boxes for input, with appropriate labels, one picture box for displaying the output, and a command button with caption "Calculate" which updates the monthly payment. The initial values for the parameters should be set as:
  • Amount of Loan = 120000
  • Annual Interst Rate = 0.10
  • Length of Loan (in months) = 36
  • The legal ranges for the parameters are as follows:

  • Amount of Loan must be between $1000 and $1000000, inclusive.
  • Annual Interst Rate must be between 5% and 25%, inclusive.
  • Length of Loan (in months) must between 1 and 360, inclusive.
  • When the program starts, you should immediately display the monthy payment amount for the terms of the loan based on the initial parameters. You can use the Form_Load() event routine to do so. You will also want to make sure that the AutoRedraw property of both the form and the picture box is set to True so that it is displayed properly. (as a check of the formula, the initial monthly payment should equal $3872.06, although your program must do the calculation itself)

    To check the validity of the user's input, we will rely on the LostFocus event routine for the three text boxes. In this way, the user can enter whatever he or she wishes in the text box, but we will not let them leave the text box with an invalid entry typed. Intuitively we want the following to happen inside LostFocus(),

    If the currently set text is NOT a valid entry Then
      Use the SetFocus method to force the focus back to this text box
    End If
    

    We have three different text boxes used for input. Rather than write all of the "dirty work" three times, we will use procedures and parameters to help keep all three LostFocus procedure relatively short. Specifically, you must write and use the following function:

  • A function named IsValid() which takes four parameters:
  • A Double which specifies the low end of the valid numeric range
  • A Double which specifies the high end of the valid numeric range
  • A String which specifies the text entered by the user
  • A String which gives a description of the parameter meaning (e.g. "Annual Interest Rate"). This string will be used in giving an appropriate error message to the user.
  • This function should return a Boolean which is equal to True if the user entered a valid parameter, and equal to False otherwise. Also, it is the responsibility of this function to give an error message to the user. This should be done by using a MsgBox as described on page 103 of the textbook. The prompt you use should be descriptive, such as "The Annual Interest Rate must be between 0.05 and 0.25"
  • Additionally, the monthly payment result gets calculated and displayed both in the Form_Load event and the Command1_Click. Rather than writing the dirty work of the formula twice (as we know it was a pain to get correct!), we will write one more general procedure:
  • A procedure named UpdatePayment() which does not take any parameters or return any values. This function is responsible for recalculating the monthly payment (based on the current values of the text boxes), and redisplaying the monthly payment amount in the picture box, formmatted as currency. For review, the formula can be expressed in Visual Basic as:
    Payment = (Amount*AnnRate/12)/(1 - (1+AnnRate/12)^(-Months))

  • Advice: Remeber to set the form's AutoRedraw Property to true. This is especially important if you are using the debugger, since the form will be hidden at times. A sample of a working program can be downloaded from, www.cs.luc.edu/~mhg/comp125/homework/sampHW5.exe .
    Extra Credit (1 point): For extra credit this week, I will suggest a very difficult task which really has nothing to do with the topic of the week. (but it will give you a chance to really dig into more Visual Basic oddities). The 1 point received is certainly not worth the time I expect you will spend, but for those martyrs...

    When originally creating this assignment, I wanted to do away with the "Calculate" Button, and to have the monthly payment updated automatically whenever the user changed a parameter value (in a legal way) and moves the focus. My thought was that this too could easily be done in the LostFocus routine by adding an Else clause which specifies to update the payment display if the parameter value is indeed valid. Unfortunately, when writing the demo, I learned that this did not quite work. If I literally typed "abc" in as a parameter and then click on one of the other text boxes, the program crashes. The reason I learned by tracing through the sequence of LostFocus events that get called along the way. I believe this idea will still work with only some minor changes. Getting it to work properly is your extra credit challenge.


    To submit your homework you should,
  • Print out your project as follows. Click "File". Click "Print". In the Range box, select "Current project". In the Print What box, click on all three of "Form Image", "Code" and "Form as Text".
  • Save both the form and the project to a floppy disk
  • Place the printouts and the floppy disk inside a large manilla envelope. Please make sure that your name appears on the envelope as well as the disk and the printouts.