Write a program that illustrates the growth of money in a savings account. When
ID: 3641348 • Letter: W
Question
Write a program that illustrates the growth of money in a savings account. When the user presse the button, values for Amount and Interest Rate are obtained from the text boxes and use to calculate the number of years until the money triples and the number of years until you hae $1,000,000. Your program should use a Do While loop and functions for calculating the number of years until the money triples and the number of years until you have $1,000,000.
Note: The balance at the end of each year is (1 + r) times the previous balance, where r is the annual interest rate in decimal form.
I basically need help in writing this program in Visual Basic for my class. The above diagram is how it should look like when 1000 is placed into amount and 0.04 is placed into annual interest rate.
"Determine Years to Triple", "Determine years to $1,000,000", "Clear", and "End" are buttons.
"Amount", "Annual Interest Rate" are labels.
The two boxes next to the two labels are textboxes where information is inserted.
The listbox is below where is displays the answer.
Explanation / Answer
I have attached the code you need below, but I'll explain how I went about this and what you need to do. You need to design your own GUI for this program as I cannot attach files to this answer. Here is what I named my controls for this program, and you need to name them the same thing or you will have to change the code around to fit your own naming conventions.
The form is called the default name: Form1
The textbox next to Amount is called: txtAmount
The textbox next to Annual Interest rate is called: txtInterestRate
The "Determine Years to Triple" button is called: btnYearsTriple
The "Determine Years to $1,000,000" button is called: btnYears1mil
The listbox is called: lstOutput
The clear button is called: btnClear
The exit button is called: btnExit
You can name the labels whatever you want to. If you want to change these names, make sure you also change the code to match your names. I declared a class-level integer constant named intMILLION to hold the amount for $1,000,000 so I could use it around the program freely. I also declared two functions, one named YearsToTriple() which accepts the amount and rate entered by the user for its parameters and returns an integer that holds the amount years that passed for the money to triple. The YearsToMillion() function is similar with its parameters and also returns the years passed for the money to reach $1,000,000. After the two function definitions, I wrote the event handler for clicking the btnYearsTriple and btnYears1mil buttons. They are very similar to each other except that they call their respective functions that they need. I included error handling within these event handlers with Double.TryParse so that the user cannot enter non-numeric data in the textboxes. The btnClear and btnExit event handlers are self-explanatory and are listed afterwards. Here is the code, remember, you need to design your GUI yourself with the appropriate names I listed for your controls. The code may look sloppy here, but paste it into Visual Studio and it should look fine.
-------------------------------------------------------------------------------------------------------------
Public Class Form1
Const intMILLION As Integer = 1000000 ' Holds the amount for $1,000,000
Function YearsToTriple(ByVal dblAmount As Double, ByVal dblRate As Double) As Integer
Dim dblFutureAmount As Double = dblAmount
Dim intYearCount As Integer = 0 ' To hold the number of years to triple
Do While (dblFutureAmount <= dblAmount * 3)
intYearCount += 1 ' Increments the year by 1 every time the loop executes
dblFutureAmount = (1 + dblRate) * dblFutureAmount
Loop
Return intYearCount ' Returns the years passed until amount is tripled
End Function
Function YearsToMillion(ByVal dblAmount As Double, ByVal dblRate As Double) As Integer
Dim dblFutureAmount As Double = dblAmount
Dim intYearCount As Integer = 0 ' To hold the number of years to triple
Do While (dblFutureAmount <= intMILLION)
intYearCount += 1
dblFutureAmount = (1 + dblRate) * dblFutureAmount
Loop
Return intYearCount ' Returns the years passed until amount is tripled
End Function
Private Sub btnYearsTriple_Click(sender As System.Object, e As System.EventArgs) Handles btnYearsTriple.Click
Dim dblAmount As Double
Dim dblRate As Double
Dim intYearsToTriple As Integer
If (Double.TryParse(txtAmount.Text, dblAmount)) And dblAmount >= 0 Then
If (Double.TryParse(txtInterestRate.Text, dblRate)) And dblRate >= 0 Then
'Calls YearsToTriple function
intYearsToTriple = YearsToTriple(dblAmount, dblRate)
' Displays output to the listbox
lstOutput.Items.Add("The number of years to triple is " & CStr(intYearsToTriple))
Else
MessageBox.Show("Error: Enter non-negative numerical values only for the rate.")
End If
Else
MessageBox.Show("Error: Enter non-negative numerical values only for the amount.")
End If
End Sub
Private Sub btnYears1mil_Click(sender As System.Object, e As System.EventArgs) Handles btnYears1mil.Click
Dim dblAmount As Double
Dim dblRate As Double
Dim intYearsToMillion As Integer
If (Double.TryParse(txtAmount.Text, dblAmount)) And dblAmount >= 0 Then
If (Double.TryParse(txtInterestRate.Text, dblRate)) And dblRate >= 0 Then
'Calls YearsToMillion function
intYearsToMillion = YearsToMillion(dblAmount, dblRate)
' Displays output to the listbox
lstOutput.Items.Add("The number of years to $1,000,000 is " & CStr(intYearsToMillion))
Else
MessageBox.Show("Error: Enter non-negative numerical values only for the rate.")
End If
Else
MessageBox.Show("Error: Enter non-negative numerical values only for the amount.")
End If
End Sub
Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
' Clears the forms textboxes and listbox
txtAmount.Clear()
txtInterestRate.Clear()
lstOutput.Items.Clear()
End Sub
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
' Closes the program
Me.Close()
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.