I could really use some help with this assignment! Must be a form application in
ID: 3834792 • Letter: I
Question
I could really use some help with this assignment!
Must be a form application in C# also must be basic as I am a beginner.
The code that must be upgraded
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
string operator1 = txtOperator.Text;
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
decimal result = Calculate(operand1, operator1, operand2);
result = Math.Round(result, 4);
this.txtResult.Text = result.ToString();
txtOperand1.Focus();
}
private decimal Calculate(decimal operand1, string operator1,
decimal operand2)
{
decimal result = 0;
if (operator1 == "+")
result = operand1 + operand2;
else if (operator1 == "-")
result = operand1 - operand2;
else if (operator1 == "*")
result = operand1 * operand2;
else if (operator1 == "/")
result = operand1 / operand2;
return result;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
In this exercise, you’ll add exception handling to the Simple Calculator form exercise A4-E1.
1.Open the SimpleCalculator project in the Assignment5SimpleCalculatoException directory.
2. Add a try-catch statement in the btnCalculate_Click event handler that will catch any exceptions that occur when the statements in that event handler are executed.
If an exception occurs, display a dialog box with
the error message,
the type of error,
and a stack trace.
Test the application by entering a nonnumeric value for one of the operands.
3. Add three additional catch blocks to the try-catch statement that will catch
a FormatException,
an OverflowException, and
a DivideByZeroException.
These catch blocks should display a dialog box with an appropriate error message.
4. Test the application again by entering a nonnumeric value for one of the operands. Then, enter 0 for the second operand as shown above to see what happens.
Thanks!
a Simple Calculator X Operand 1 86 Operator: Operand 2 11.11 7.7408 Result: CalculateExplanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Exceptionmath
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
string operator1 = txtOperator.Text;
try
{
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
decimal result = Calculate(operand1, operator1, operand2);
result = Math.Round(result, 4);
this.txtResult.Text = result.ToString();
txtOperand1.Focus();
}
catch (FormatException frex)
{
MessageBox.Show("Dont enter non numeric value" + frex.Message, frex.StackTrace);
}
catch(OverflowException ovex)
{
MessageBox.Show("Overflow exception" + ovex.StackTrace);
}
catch (DivideByZeroException dvzero)
{
MessageBox.Show("Can not divide by zero"+dvzero.StackTrace);
}
}
private decimal Calculate(decimal operand1, string operator1, decimal operand2)
{
decimal result = 0;
if (operator1 == "+")
result = operand1 + operand2;
else if (operator1 == "-")
result = operand1 - operand2;
else if (operator1 == "*")
result = operand1 * operand2;
else if (operator1 == "/")
result = operand1 / operand2;
return result;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.