Question: This is C# 1. Add a try catch statement to the btnCalculate_click methodthat catches and handles any formatException or OverflowExceptionthat might occur. These catch blocks

This is C#

1. Add a try catch statement to the btnCalculate_click methodthat catches and handles any formatException or OverflowExceptionthat might occur. These catch blocks should display dialog boxeswith appropriate messages.

2. Ann another catch block to the try catch statement that willcatch any other exception that might occur. This catch block shoulddisplay a dialog box that displays the message contained in theexception object along with the exception type and teh stacktrace.

3. Add a throw statement to the calculateFutureValue method thatthrows a new exception of the Exception class regardless of theresult of the calculation. This statement will be used to test theenhancements of step 4, and it should specificy a generic messagethat indicates an unknown error. Then, test the application byentering valid values in the three text boxes and clicking thecalculate button. If the exception is thrown and the last catchblock works correctly, end the application and comment out thethrow statement.

4. Code four generic validation methods named IsPresent,IsDecimal, IsInt32, and IsWithinRange that test whether a text boxcontains an entry, a valid decimal value, a valid int value, and avalue within a given range. If the validation is unsuccessful, eachmethod should display a dialog box with a message that includes thename of the textbox thats being validated.

5. Code an IsValidData method that calls the four genericmethods that were created to validate the data the user enters intothe three text boxes. Each text box should be tested for threetypes of invalid data: nothing entered, invalid format, invalidrange.

6. Modify the code in the event handler for the Calculate buttonso it uses the IsValidData method to validate the data before itprocesses it. Then, test the application to be sure it workscorrectly. If it does, end the application and comment out theFormatException and OverflowException catch blocks.

Here is my code:

namespace FutureValue
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnCalculate_Click(object sender, EventArgse)
{


decimal monthlyInvestment =Convert.ToDecimal(txtMonthlyInvestment.Text);
decimal yearlyInterestRate =Convert.ToDecimal(txtInterestRate.Text);
int years = Convert.ToInt32(txtYears.Text);

int months = years * 12;
decimal monthlyInterestRate = yearlyInterestRate / 12 / 100;

decimal futureValue = this.CalculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
txtFutureValue.Text = futureValue.ToString("c");
txtMonthlyInvestment.Focus();
}

private decimal CalculateFutureValue(decimalmonthlyInvestment,
decimal monthlyInterestRate, int months)
{
decimal futureValue = 0m;
for (int i = 0; i < months; i++)
{
futureValue = (futureValue + monthlyInvestment)
* (1 + monthlyInterestRate);
}
return futureValue;
}

private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!