Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Provided Code: Public Class frmFutureValue Private Sub btnCalculate_Click(sender

ID: 3913742 • Letter: P

Question

Provided Code:

Public Class frmFutureValue

Private Sub btnCalculate_Click(sender As Object,
e As EventArgs) Handles btnCalculate.Click
Try
If IsValidData() Then
Dim monthlyInvestment As Decimal =
Convert.ToDecimal(txtMonthlyInvestment.Text)
Dim yearlyInterestRate As Decimal =
Convert.ToDecimal(txtInterestRate.Text)
Dim years As Integer = Convert.ToInt32(txtYears.Text)

Dim monthlyInterestRate As Decimal = yearlyInterestRate / 12 / 100
Dim months As Integer = years * 12

Dim futureValue As Decimal = Me.FutureValue(
monthlyInvestment, monthlyInterestRate, months)

txtFutureValue.Text = FormatCurrency(futureValue)
txtMonthlyInvestment.Select()
End If
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf &
ex.GetType.ToString & vbCrLf & vbCrLf &
ex.StackTrace, "Exception")
End Try
End Sub

Private Function IsValidData() As Boolean
Return _
IsPresent(txtMonthlyInvestment, "Monthly Investment") AndAlso
IsDecimal(txtMonthlyInvestment, "Monthly Investment") AndAlso
IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000) AndAlso
IsPresent(txtInterestRate, "Yearly Interest Rate") AndAlso
IsDecimal(txtInterestRate, "Yearly Interest Rate") AndAlso
IsWithinRange(txtInterestRate, "Yearly Interest Rate", 1, 15) AndAlso
IsPresent(txtYears, "Number of Years") AndAlso
IsInt32(txtYears, "Number of Years") AndAlso
IsWithinRange(txtYears, "Number of Years", 1, 50)
End Function

Private Function IsPresent(textBox As TextBox, name As String) _
As Boolean
If textBox.Text = "" Then
MessageBox.Show(name & " is a required field.", "Entry Error")
textBox.Select()
Return False
End If
Return True
End Function

Private Function IsDecimal(textBox As TextBox, name As String) _
As Boolean
Dim number As Decimal = 0
If Decimal.TryParse(textBox.Text, number) Then
Return True
Else
MessageBox.Show(name & " must be a decimal value.", "Entry Error")
textBox.Select()
textBox.SelectAll()
Return False
End If
End Function

Private Function IsInt32(textBox As TextBox, name As String) _
As Boolean
Dim number As Integer = 0
If Int32.TryParse(textBox.Text, number) Then
Return True
Else
MessageBox.Show(name & " must be an integer.", "Entry Error")
textBox.Select()
textBox.SelectAll()
Return False
End If
End Function

Private Function IsWithinRange(textBox As TextBox,
name As String, min As Decimal,
max As Decimal) As Boolean
Dim number As Decimal = CDec(textBox.Text)
If number < min Or number > max Then
MessageBox.Show(name & " must be between " & min &
" and " & max & ".", "Entry Error")
textBox.Select()
textBox.SelectAll()
Return False
End If
Return True
End Function

Private Function FutureValue(monthlyInvestment As Decimal,
monthlyInterestRate As Decimal, months As Integer) _
As Decimal
For i As Integer = 1 To months
FutureValue = (FutureValue + monthlyInvestment) *
(1 + monthlyInterestRate)
Next
End Function

Private Sub ClearFutureValue(sender As Object,
e As EventArgs) Handles txtMonthlyInvestment.TextChanged,
txtYears.TextChanged, txtInterestRate.TextChanged
txtFutureValue.Text = ""
End Sub

Private Sub btnExit_Click(sender As Object,
e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class

UI:

Section 2 he i Du Declare module-level variables for a row index and a rectangular array of strings that provides for 10 rows and 4 columns. Use a rectangular array to store future value calculations Add code that stores the values for each calculation in the next row of the array when the user clicks the Calculate button. Store th 3. e monthly investment and future value in currency format, and store the interest rate in percent format. Add code to display the elements in the array in a message box when the user clicks the Exit button. Use tab characters to format the message box so it looks like this: 4. Future Value Calculations Inv/Mo. Rate Years Future Value S100.00 4.0% 5 SI00-00 4.5% 5 S100.00 5.0% 5 $6,652.00 6,739.73 $6,82894 oK 5. Test the program by making up to 10 future value calculations. When you've got this working right, close the solution.

Explanation / Answer

Following additions are made to the program:

1. Added to class variables rowIndex and arrFutureValues to store the row index and array of Future values respectively.

2. Added procedure populateArray to populate the array and invoked this procedure from the btnCalculate_Click method within the Try statement.

3. Added procedure displayArray to display the array and invoked this from the btnExit_Click method.

Note: I have also added a validation in the populateArray method to inform user that only 10 values will be added to the array.

Screenshot:

Code:

Public Class frmFutureValue
    Dim rowIndex As Integer 'Row Index Variable to hold the Row Index
    Dim arrFutureValues(10, 4) As String    'Rectangular Array to hold the Future values

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Try
            If IsValidData() Then
                Dim monthlyInvestment As Decimal =
                Convert.ToDecimal(txtMonthlyInvestment.Text)
                Dim yearlyInterestRate As Decimal =
                Convert.ToDecimal(txtInterestRate.Text)
                Dim years As Integer = Convert.ToInt32(txtYears.Text)

                Dim monthlyInterestRate As Decimal = yearlyInterestRate / 12 / 100
                Dim months As Integer = years * 12

                Dim futureValue As Decimal = Me.FutureValue(
                monthlyInvestment, monthlyInterestRate, months)

                txtFutureValue.Text = FormatCurrency(futureValue)

                'Procedure call to populate the Future Values to the Rectangular Array
                Call populateArray(monthlyInvestment, yearlyInterestRate, years, futureValue)

                txtMonthlyInvestment.Select()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & vbCrLf &
            ex.GetType.ToString & vbCrLf & vbCrLf &
            ex.StackTrace, "Exception")
        End Try
    End Sub
    Private Function IsValidData() As Boolean
        Return _
        IsPresent(txtMonthlyInvestment, "Monthly Investment") AndAlso
        IsDecimal(txtMonthlyInvestment, "Monthly Investment") AndAlso
        IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000) AndAlso
        IsPresent(txtInterestRate, "Yearly Interest Rate") AndAlso
        IsDecimal(txtInterestRate, "Yearly Interest Rate") AndAlso
        IsWithinRange(txtInterestRate, "Yearly Interest Rate", 1, 15) AndAlso
        IsPresent(txtYears, "Number of Years") AndAlso
        IsInt32(txtYears, "Number of Years") AndAlso
        IsWithinRange(txtYears, "Number of Years", 1, 50)
    End Function
    Private Function IsPresent(textBox As TextBox, name As String) _
As Boolean
        If textBox.Text = "" Then
            MessageBox.Show(name & " is a required field.", "Entry Error")
            textBox.Select()
            Return False
        End If
        Return True
    End Function
    Private Function IsDecimal(textBox As TextBox, name As String) _
As Boolean
        Dim number As Decimal = 0
        If Decimal.TryParse(textBox.Text, number) Then
            Return True
        Else
            MessageBox.Show(name & " must be a decimal value.", "Entry Error")
            textBox.Select()
            textBox.SelectAll()
            Return False
        End If
    End Function
    Private Function IsInt32(textBox As TextBox, name As String) _
As Boolean
        Dim number As Integer = 0
        If Int32.TryParse(textBox.Text, number) Then
            Return True
        Else
            MessageBox.Show(name & " must be an integer.", "Entry Error")
            textBox.Select()
            textBox.SelectAll()
            Return False
        End If
    End Function
    Private Function IsWithinRange(textBox As TextBox,
name As String, min As Decimal,
max As Decimal) As Boolean
        Dim number As Decimal = CDec(textBox.Text)
        If number < min Or number > max Then
            MessageBox.Show(name & " must be between " & min &
            " and " & max & ".", "Entry Error")
            textBox.Select()
            textBox.SelectAll()
            Return False
        End If
        Return True
    End Function

    Private Function FutureValue(monthlyInvestment As Decimal,
    monthlyInterestRate As Decimal, months As Integer) _
    As Decimal
        For i As Integer = 1 To months
            FutureValue = (FutureValue + monthlyInvestment) *
            (1 + monthlyInterestRate)
        Next
        Return FutureValue
    End Function

    Private Sub ClearFutureValue(sender As Object,
    e As EventArgs) Handles txtMonthlyInvestment.TextChanged,
    txtYears.TextChanged, txtInterestRate.TextChanged
        txtFutureValue.Text = ""
    End Sub
    Private Sub populateArray(monInv As Decimal, yrlyIntRt As Decimal, yrs As Integer, futVal As Decimal)
        'Procedure to populate the Future Values to the Rectangular Array
        'This Function also provides a message to the user that only 10 values be added
        'If they keep entering more than 10 values
        If rowIndex < 10 Then
            arrFutureValues(rowIndex, 0) = "$" + Format(monInv, "###,###,##0.00")
            arrFutureValues(rowIndex, 1) = Format(yrlyIntRt, "#.0") & "%"
            arrFutureValues(rowIndex, 2) = yrs.ToString()
            arrFutureValues(rowIndex, 3) = "$" + Format(futVal, "###,###,##0.00")
            rowIndex = rowIndex + 1
        Else
            'Message Box when user enters more than 10 values
            MessageBox.Show("Only 10 values can be added to the array. Current values are not added.")
        End If
    End Sub

    Private Sub displayArray()
        'Procedure to display the Rectangular Array of Future Values
        Dim I As Integer
        Dim strDisplay As String
        'Initializing the string with Heading
        strDisplay = "Inv/Mo." & vbTab & "Rate" & vbTab & "Years" & vbTab & "Future Value" & vbCrLf
        'Loop to scan the Rectangular Array
        For I = 0 To rowIndex

            strDisplay = strDisplay & arrFutureValues(I, 0) & vbTab & arrFutureValues(I, 1) & vbTab & arrFutureValues(I, 2) _
                        & vbTab & arrFutureValues(I, 3) & vbCrLf
        Next I
        'Message Box to display the array
        MessageBox.Show(strDisplay, "Future Values")
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        'Procedure call to display the Rectangular Array of Future Values
        Call displayArray()
        Me.Close()
    End Sub
End Class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote