CE 311 K - Introduction to Computer Methods

Lab 3: Elements of Programming


Content


Introduction

Recall your Fahrenheit to Celsius converter project from Lab-2.  Your final GUI should have looked something like this:

Results of the Temperature Conversion

In this lab, we want to build on this program as an example.  In this example we will provide a user the option of converting from Fahrenheit to Celsius or from Celsius to Fahrenheit (see page 89 in the text).

Access Visual Basic

  1. Open Visual Basic from the start menu of your computer, that is Start\Program\Microsoft Visual Studio 6.0\Microsoft Visual Basic 6.0
  2.  Select “Standard EXE” from the “New Project window” and open it.
  3. Before starting the exercise, save your work by selecting the menu File\Save Project As.
  4. Name the file as “your initiallab3” and save it in a convenient directory (be sure to REMEMBER where you saved it!!!)

Prepare the Graphic User Interface

Develop the "form" shown in the figure above that we will use in the project.  The form should contain a label, a text box, two command buttons, and a picture box with the following properties:

Object Property Setting
Form Caption
Name
Temperature Conversion
frmTempConv
Label Autosize
Caption
Name
True
Temperature
lblTemp
Text Box Name
Text
txtTemp
Empty (Null String)
Command Button 1 Caption
Name
Convert Fahrenheit to Celsius
cmdFtoC
Command Button 2 Caption
Name
Convert Celsius to Fahrenheit
cmdCtoF
Picture Box Name picOutput

Your form should look like this:

Add VB Code to the Project

Next, let’s add some code to the cmdFtoC and cmdCtoF buttons. 

First, double click on the cmdCtoF button and type in the following code (note that it is slightly simpler than the code on page 89 in the text):

In the code, we declare the variables "C" and "F" to be "form-level" global variables so that we can use them in the code for both the cmdCtoF and cmdFtoC buttons.  When the user clicks the button cmdCtoF, the procedure cmdCtoF_Click( ) is invoked and the 4 lines of code are executed.  The user is supposed to enter some temperature value in the text box (either degrees F or C) before clicking one of the buttons.  However, if they don't what will happen?  The first line of code gets the characters typed in the txtTemp box and converts them to a number which then gets assigned to the variable "C".  What happens if you type letters into the text box instead of numbers?  What would happen if the "Val( )" function were not used in this case?  Next, the value of "C" is converted to degrees F by the formula.  What happens if the integer division symbol "\" is used instead of the regular division symbol "/" (try it with a value of 100 degrees C)?  Finally the code clears the picture box and prints out the values and text indicated.  Chr(176) is the ASCII representation of the degree sign.

Next, double click on the cmdFtoC button and type in the following code:

Test your code.


Assignment

Problem 2, page 96 in the text.  Sam has a three-year auto loan at an 8/5% annual interest rate.  His monthly payment is $350.  He has made payments on the loan for the past k months, and he wants to know the payoff balance.  Write a program to determine the payoff balance of Sam's auto loan.  Demonstrate your program for k = 18 months, but be sure that you could enter other months and the program would work for those as well.  The applicable formula is:

where

    B = the payoff balance ($)
    P = the monthly payment ($)
    i = the monthly interest rate (in decimal form)
    k = the number of monthly payments already made
    n = the total number of monthly payments

Turn in:

  1. A screenshot of your program working for k = 18 months.
  2. A printout of the VB code used in your program.

McKinney | CE311K Civil Engineering | UT Austin