Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Enamespace SimpleCalculator public partial class Form1 Form 13 14 public Form1() 16 InitializeComponent ); 18 double firstNumber; double secondNumber 0 public bool tryParse() try 26

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Enamespace SimpleCalculator public partial class Form1 Form 13 14 public Form1() 16 InitializeComponent ); 18 double firstNumber; double secondNumber 0 public bool tryParse() try 26 if (I(double.TryParse(txtOpr1.Text, out firstNumber)) 11 (double.TryParse(txtopr2.Text, out secondNumber))) // if conversion fails display result using MessageBox.Show) MessageBox.Show( "Please enter a valid number."); //return false-that is invalid input return false 28 30 //if conversion is valid return true for input return true; 34 catch (Exception e1) MessageBox. Show( Error:e1.Message); return false; 10 private void btnCalculate_Click (object sender, EventArgs e) 14 try //calling tryParse( method to check if it is returning true or false if (tryParse()) //addition of firstNumber with secondNumber string opr - txtOperand.Text double result-0; switch (opr) case "+" result-firstNumber secondNumber break; Case result-firstNumber - secondNumber break; Case "*. result-firstNumber*secondNumber break; case if (secondNumber) txtResult.TextImpossible to divide by Zero."; else result-firstNumber/ secondNumber break; default // If the operator is other than 1-5 error message is shown txtResult.Text"Invalid operator" break; // If the operator is other than 1-5 error message is shown txtResult.Text"Invalid operator"; break; txtResult . Text result . ToString("#"####"); catch (Exception e1) MessageBox.Show("Errore1.Message); txtResult.Text -"-"; private void btnExit_click(object sender, EventArgs e) Application.Exit( private void SimpleCalculator_KeyDown(object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close() private void txtOpr1_KeyDown (object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close(); txtResult.Text ""; Calculator SimpleCalculator.Form1 this.Close(); private void txtOpr1_KeyDown (object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close(); txtResult.Text" private void txtOperand_KeyDown (object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close( txtResult.Text private void txtOpr2_KeyDown (object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close( txtResult.Text In this assignment, you'll add exception handling and data validation to the SimpleCalculator application you developed in Chapter 6 lab assignments . Open the SimpleCalculator application you developed in Chapter 6 lab assignment in Visual Studio 2015. If you did not complete Chapter 6 lab assignment yet, you may complete it before you start this assignment. You may also request the solution of Chapter 6 lab assignment by sending me email, however, you will get 0 for that assignment 2. Add a try...catch statement to 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. Test the application again by entering a nonnumeric value for one of the operands. Then, enter 0 for the second operand as shown below to see what happens 4. Operand 2 Invalid numeric format. Please check all entries. Result Calculate Exit OK Simple Calculator Operand 1 Entry Error Operand 2 Divide-by-zero error. Please enter a non-zero value for operand 2. Result Calculate Exit OK Here is template of the current code file namespace SimpleCalculator public partial class Forml Form public Form1() InitializeComponent (); private void btnCalculate_Click(object sender, EventArgs e) try catch (FormatException) catch (OverflowException) catch (DivideByZeroException) catch (Exception ex) private decimal Calculate(decimal operandi, string operatorl, decimal operand2) private void btnExit_Click(object sender, EventArgs e) 5. Code methods named IsPresent, IsDecimal, and IsWithinRange that work like the methods 6. Code a method named IsOperator that checks that the text box that's passed to it contains a 7. Code a method named IsValidData that checks that the Operand 1 and Operand 2 text boxes described in chapter 7 of the book. value of +, -, *, or/. contain a decimal value between 0 and 1,000,000 (non-inclusive) and that the Operator text box contains a valid operator 8. Select all of the catch blocks from the try...catch statement in the btnCalculate Click event handler except for the one that catches any general exception, click the Comment Out button from the standard toolbar (see image below) to comment out those specific catch blocks. Then, add code to this event handler that performs the calculation, and displays the result only if the values in the text boxes are all valid. 9. Test the application to be sure that all the data is validated properly. Simple Calculator Entry Error Operand 1: Operator. Operand 2 Result 23 Operand2 is a required field. Calculate Exxt OK Simple Calculator Operand 1:2 Entry Error Operator Here is template of the completed code file namespace Simplecalculator public partial class Forml Form public Form1() InitializeComponent; private void btnCalculate_Click(object sender, EventArgs e) if (IsValidData()) try //catch (FormatException) //catch (OverflowException) //catch (DivideByZeroException catch (Exception ex) //catch (DivideByZeroException) IT catch (Exception ex) private decimal Calculate(decimal operandl, string operator1, decimal operand2) public bool IsValidData() public bool IsPresent (TextBox textBox, string name) public bool IsDecimal (TextBox textBox, string name) public bool IswithinRange (TextBox textBox, string name, decimal min, decimal max) public bool IsOperator(TextBox textBox, string name) private void btnExit_click (object sender, EventArgs e) 10. Compress the above two project folders into a .zip file (right-click the folder -> Send to -> Enamespace SimpleCalculator public partial class Form1 Form 13 14 public Form1() 16 InitializeComponent ); 18 double firstNumber; double secondNumber 0 public bool tryParse() try 26 if (I(double.TryParse(txtOpr1.Text, out firstNumber)) 11 (double.TryParse(txtopr2.Text, out secondNumber))) // if conversion fails display result using MessageBox.Show) MessageBox.Show( "Please enter a valid number."); //return false-that is invalid input return false 28 30 //if conversion is valid return true for input return true; 34 catch (Exception e1) MessageBox. Show( Error:e1.Message); return false; 10 private void btnCalculate_Click (object sender, EventArgs e) 14 try //calling tryParse( method to check if it is returning true or false if (tryParse()) //addition of firstNumber with secondNumber string opr - txtOperand.Text double result-0; switch (opr) case "+" result-firstNumber secondNumber break; Case result-firstNumber - secondNumber break; Case "*. result-firstNumber*secondNumber break; case if (secondNumber) txtResult.TextImpossible to divide by Zero."; else result-firstNumber/ secondNumber break; default // If the operator is other than 1-5 error message is shown txtResult.Text"Invalid operator" break; // If the operator is other than 1-5 error message is shown txtResult.Text"Invalid operator"; break; txtResult . Text result . ToString("#"####"); catch (Exception e1) MessageBox.Show("Errore1.Message); txtResult.Text -"-"; private void btnExit_click(object sender, EventArgs e) Application.Exit( private void SimpleCalculator_KeyDown(object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close() private void txtOpr1_KeyDown (object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close(); txtResult.Text ""; Calculator SimpleCalculator.Form1 this.Close(); private void txtOpr1_KeyDown (object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close(); txtResult.Text" private void txtOperand_KeyDown (object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close( txtResult.Text private void txtOpr2_KeyDown (object sender, KeyEventArgs e) if (e.KeyCodeKeys.Escape) this.Close( txtResult.Text In this assignment, you'll add exception handling and data validation to the SimpleCalculator application you developed in Chapter 6 lab assignments . Open the SimpleCalculator application you developed in Chapter 6 lab assignment in Visual Studio 2015. If you did not complete Chapter 6 lab assignment yet, you may complete it before you start this assignment. You may also request the solution of Chapter 6 lab assignment by sending me email, however, you will get 0 for that assignment 2. Add a try...catch statement to 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. Test the application again by entering a nonnumeric value for one of the operands. Then, enter 0 for the second operand as shown below to see what happens 4. Operand 2 Invalid numeric format. Please check all entries. Result Calculate Exit OK Simple Calculator Operand 1 Entry Error Operand 2 Divide-by-zero error. Please enter a non-zero value for operand 2. Result Calculate Exit OK Here is template of the current code file namespace SimpleCalculator public partial class Forml Form public Form1() InitializeComponent (); private void btnCalculate_Click(object sender, EventArgs e) try catch (FormatException) catch (OverflowException) catch (DivideByZeroException) catch (Exception ex) private decimal Calculate(decimal operandi, string operatorl, decimal operand2) private void btnExit_Click(object sender, EventArgs e) 5. Code methods named IsPresent, IsDecimal, and IsWithinRange that work like the methods 6. Code a method named IsOperator that checks that the text box that's passed to it contains a 7. Code a method named IsValidData that checks that the Operand 1 and Operand 2 text boxes described in chapter 7 of the book. value of +, -, *, or/. contain a decimal value between 0 and 1,000,000 (non-inclusive) and that the Operator text box contains a valid operator 8. Select all of the catch blocks from the try...catch statement in the btnCalculate Click event handler except for the one that catches any general exception, click the Comment Out button from the standard toolbar (see image below) to comment out those specific catch blocks. Then, add code to this event handler that performs the calculation, and displays the result only if the values in the text boxes are all valid. 9. Test the application to be sure that all the data is validated properly. Simple Calculator Entry Error Operand 1: Operator. Operand 2 Result 23 Operand2 is a required field. Calculate Exxt OK Simple Calculator Operand 1:2 Entry Error Operator Here is template of the completed code file namespace Simplecalculator public partial class Forml Form public Form1() InitializeComponent; private void btnCalculate_Click(object sender, EventArgs e) if (IsValidData()) try //catch (FormatException) //catch (OverflowException) //catch (DivideByZeroException catch (Exception ex) //catch (DivideByZeroException) IT catch (Exception ex) private decimal Calculate(decimal operandl, string operator1, decimal operand2) public bool IsValidData() public bool IsPresent (TextBox textBox, string name) public bool IsDecimal (TextBox textBox, string name) public bool IswithinRange (TextBox textBox, string name, decimal min, decimal max) public bool IsOperator(TextBox textBox, string name) private void btnExit_click (object sender, EventArgs e) 10. Compress the above two project folders into a .zip file (right-click the folder -> Send to ->

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions