Docsity

Lab Exercise: Understanding Loops and Computing Interest and Savings, Lab Reports of Information Technology

Toggle sidebar
bg1
IST 256
Lab Week 4, Part 2 – September 17, 2008
1. Understanding Loops – showing the variables of a program
For this problem, you are not to put any part of the program into Visual Studio. You are
to write your answers on this lab sheet.
A. Consider the following program for a button as demonstrated in class:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' use the penny variable to compute money as a decimal value
Dim penny As Single
' number of times to double a penney and the loop index
Dim numtimes, counter As Integer
' initialize to the value of a single penney
penny = 0.01
numtimes = CInt(TextBox1.Text)
For counter = 1 To numtimes Step 1
penny = penny * 2
Next
' Show the result
Label2.Text = "Result: " & Format(penny, "c")
End Sub
In the following boxes, write the values of the variables as this program is executed.
Assume that the user types in 5 in TextBox1:
Variable Val1 Val2 Val3 Val4 Val5 Val6 Val7
numtimes
counter
penny
What number will be shown to the user? (write it here):
pf3
pf4
pf5

Partial preview of the text

Download Lab Exercise: Understanding Loops and Computing Interest and Savings and more Lab Reports Information Technology in PDF only on Docsity!

IST 256

Lab Week 4, Part 2 – September 17, 2008

1. Understanding Loops – showing the variables of a program

For this problem, you are not to put any part of the program into Visual Studio. You are

to write your answers on this lab sheet.

A. Consider the following program for a button as demonstrated in class:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' use the penny variable to compute money as a decimal value Dim penny As Single ' number of times to double a penney and the loop index Dim numtimes, counter As Integer

' initialize to the value of a single penney penny = 0. numtimes = CInt(TextBox1.Text)

For counter = 1 To numtimes Step 1 penny = penny * 2 Next

' Show the result Label2.Text = "Result: " & Format(penny, "c") End Sub

In the following boxes, write the values of the variables as this program is executed.

Assume that the user types in 5 in TextBox1:

Variable Val1 Val2 Val3 Val4 Val5 Val6 Val

numtimes

counter

penny

What number will be shown to the user? (write it here):

B. Consider the following program for a button as demonstrated in class:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' use the penny variable to compute money as a decimal value Dim penny As Single ' Goal amount to achieve by doubling Dim goal As Single ' number of times the penny was doubled Dim numtimes As Integer

' initialize the value of a single penny and the number penny = 0. numtimes = 0 ' Get the Goal amount from the user goal = CSng(TextBox1.Text)

While penny < goal penny = penny * 2 numtimes = numtimes + 1 End While

' Show the result Label2.Text = "Number of times doubled: " & CStr(numtimes) End Sub

In the following boxes, write the values of the variables as this program is executed,

assume that the user types in 1.00:

Variable Val1 Val2 Val3 Val4 Val5 Val6 Val7 Val

goal

numtimes

penny

What number will be shown to the user? (write it here):

For lab today, you are to extend this Compute Interest application. In the extended

version, the user can also type in an amount that he or she proposes to add to the bank

balance as an investment each year.

Write this application, allowing the user to type in:

Initial bank balance

Number of years to compute a new balance

Interest rate, given as a decimal number

Annual investment

Add code to the Calculate button to also add in the investment amount each year. What

type of variable should you add for the annual investment? Suppose that this variable is

called investment. Then the new formula for a new bank balance every year would be:

balance = balance + interestamount + investment

Test your program and write one example here:

Initial bank balance typed by user

Number of years

Interest rate

Annual investment

New bank balance computed

3. Compute Savings – modifying the compute interest program

For this application, you are going to create a similar application to the one that you did in

the last example, called something like ComputeSavings. Before starting this next

program, keep open Visual Studio to the ComputeInterest project. Go to the Form1.vb

tab and copy the program that you wrote for the Calculate button. Save this program in a

text editor such as Notepad or Word so that you can refer to it while writing the

ComputeSavings program.

Now open Visual Studio and create a new project named something like

ComputeSavings. This new project should be in the same lab projects folder as usual.

For this example , you are to write a similar application to the ComputeInterest project,

except that this time the user wants to achieve a certain amount of savings and needs to

compute how many years it will take to achieve their goal amount.

Write this application, allowing the user to type in:

Initial bank balance

Interest rate, given as a decimal number

Annual investment

Goal savings amount

There should be two buttons, one for Compute Number of Years and one for Close. The

Compute Number of Years button should get the user inputs and use a while loop to

compute new bank balances until the goal amount is achieved.

Look at the code that you saved for the ComputeInterest program. After the declarations

of variables, set the numberyears variable to start at 0. Modify the For loop in that

program to be a while loop in your new ComputeSavings program. The condition for the

while loop should be to keep computing a new balance as long as the balance is less than

the savings goal. Note that you must explicitly add one to the number of years variable

during the while loop. After the while loop, display the number of years to the user.

Test your program and write one example here:

Initial bank balance typed by user

Interest rate

Annual investment

Savings goal

Number of years to achieve goal

To hand in your lab for the week, staple together the lab handouts from Monday

and Wednesday, together with printouts of the two programs from Wednesday’s

lab.