Question

In: Computer Science

Hello, In C#, we are creating a simple calculator that takes two operands and one operator...

Hello,

In C#, we are creating a simple calculator that takes two operands and one operator and calculates a result. We need to include a private method to calculate the result as well as try-catch statements for exceptions. My code that i have so far is below, it executes and calculates, but there is an issue with the way i have done the method. Any help or info about my method errors would be great, Thank you!

**********************************

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 SimpleCalculator
{
/***********************************
* Displays simple calculations from the
* data the user enters.
* *********************************/

public partial class simpleCalculator : Form
{
public simpleCalculator()
{
InitializeComponent();
}

private void btnCalculate_Click(object sender, EventArgs e)
{
/*******************************
* This method calcultes the result from the
* user entry in the operands and operaator boxes.
* *****************************/
try
{
//converts the data frm the user entry frm txt to decimal and string.

decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
string operator1 = Convert.ToString(txtOperator1.Text);

  
//method to perform the calculations.
decimal result = Calculate(operand1, operand2, operator1);
  
}
//the catch exceptions, will try the above code and any exceptions will be caught by the ones below
//and throw uo a message box with an error message.
catch (FormatException)
{
MessageBox.Show("A format exception has occurred. Please check all entries." + "\n\n",
"Entry Error");
}
catch (OverflowException)
{
MessageBox.Show("An overflow exception has occurred. Please enter smaller values",
"Entry Error");
}
catch (DivideByZeroException)
{
MessageBox.Show("Divide-by-zero error. Please enter a non-zero value",
"Entry Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
}
//where the calculate method is called.
private decimal Calculate(decimal operand1, decimal operand2,
string operator1)
{
//initilized the result to decimal and zero.
decimal result = 0m;

//code to check the operator1 entry and compare it to either /, *, - or +
//then do the corresponding calculation.
if (operator1 == "/")
{
result = operand1 / operand2;
}
else if (operator1 == "*")
{
result = operand1 * operand2;
}
else if (operator1 == "+")
{
result = operand1 + operand2;
}
else if (operator1 == "-")
{
result = operand1 - operand2;
}
  
//code to display the reslt in the result box
txtResult.Text = result.ToString();
}
}

//code to exit form
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
//code to clear the result box after calculation has been performed and the operand1 box is cleared.
private void ClearResult(object sender, EventArgs e) => txtResult.Text = "";
}
}

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new Windows Forms Application in C# is created using Visual Studio 2017 with name "SimpleCalculator".This application contains a form with name "SimpleCalculator.cs".Below are the files associated with SimpleCalculator.

1.Form1.cs[Design]

2.Form1.cs

//namespaces
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;
//application namespace
namespace SimpleCalculator
{
/***********************************
* Displays simple calculations from the
* data the user enters.
* *********************************/
public partial class simpleCalculator : Form
{
public simpleCalculator()
{
InitializeComponent();
}

private void btnCalculate_Click(object sender, EventArgs e)
{
/*******************************
* This method calcultes the result from the
* user entry in the operands and operaator boxes.
* *****************************/
try
{
//converts the data frm the user entry frm txt to decimal and string.
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);//taking operand1
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);////taking operand2
string operator1 = Convert.ToString(txtOperator1.Text);//taking operator1
//method to perform the calculations,method call
decimal result = Calculate(operand1, operand2, operator1);
//code to display the reslt in the result box
txtResult.Text = result.ToString();
}
//the catch exceptions, will try the above code and any exceptions will be caught by the ones below
//and throw uo a message box with an error message.
catch (FormatException)
{
MessageBox.Show("A format exception has occurred. Please check all entries." + "\n\n",
"Entry Error");
}
catch (OverflowException)
{
MessageBox.Show("An overflow exception has occurred. Please enter smaller values",
"Entry Error");
}
catch (DivideByZeroException)
{
MessageBox.Show("Divide-by-zero error. Please enter a non-zero value",
"Entry Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
}
}
//where the calculate method is called.
private decimal Calculate(decimal operand1, decimal operand2, string operator1)
{
//initilized the result to decimal and zero.
decimal result = 0m;

//code to check the operator1 entry and compare it to either /, *, - or +
//then do the corresponding calculation.
if (operator1 == "/")
{
result = operand1 / operand2;//dividion
}
else if (operator1 == "*")
{
result = operand1 * operand2;//multiplication
}
else if (operator1 == "+")
{
result = operand1 + operand2;//addition
}
else if (operator1 == "-")
{
result = operand1 - operand2;//subtraction
}

return result;//return result
}
//code to clear the result box after calculation has been performed and the operand1 box is cleared.
private void ClearResult_Click(object sender, EventArgs e)
{
txtOperand1.Text = "";
txtOperand2.Text = "";
txtOperator1.Text = "";
txtResult.Text = "";//clear result from the textbox
}

//code to exit form
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

===============================================

Output : Run application using F5 and will get the screen as shown below

Screen 1 :

Screen 2:Screen when numbers are not entered

Screen 3:Screen showing addition

Screen 4:Screen showing subtraction

Screen 5:Screen showing multiplication

Screen 6:Screen showing division

Screen 7:Screen when second number is zero

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
c++ using class... define operator overloading and give simple example how we can use operator overloading...
c++ using class... define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.
Create a function that takes two numbers and a mathematical operator +, –, / , *...
Create a function that takes two numbers and a mathematical operator +, –, / , * and will perform a calculation with the given numbers. Examples: calculator(2, "+", 2) ➞ 4 calculator(2, "*", 2) ➞ 4 calculator(4, "/", 2) ➞ 2 Notes If the input tries to divide by 0, return: "Can't divide by 0!" Code in C++ language ...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
8. In MATLAB, the colon operator can be used in two ways: creating a matrix (or...
8. In MATLAB, the colon operator can be used in two ways: creating a matrix (or array) and selecting part(s) of a matrix (or array). Explain and give examples of each. 9. For a matrix C, that has 10 rows and 12 columns: - What is the MATLAB command to select all of row 5 from it? - What 2 MATLAB commands would ask the user for an integer, N, from 1 to 10 and then create an array D...
Design a simple calculator program using C++ which is able to: 1. ADD two decimal numbers...
Design a simple calculator program using C++ which is able to: 1. ADD two decimal numbers 2. MULTIPLY two decimal numbers. The following features must be incorporated in your program. 1. Must have an interface for the user to be able to either select the ADD option or MULTIPLY option or to EXIT the program. NOTE: If the user makes a wrong selection, a display must be shown to inform the user and the user must be given a choice...
For this assignment you will develop pseudocode and write a C++ program for a simple calculator....
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below. PSEUDOCODE FILE NAME: Calculator.txt C++ SOURCE CODE FILE NAME : Calculator.cpp DESCRIPTION: Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt...
Hello this is for C++ language. I am currently stuck on creating my api for Day...
Hello this is for C++ language. I am currently stuck on creating my api for Day Trading Stocks. as follows I need an api for *//Function Signature * * parameter: * * Return Value: ** *// Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument. Your function recommends the maximum profit an investor can make by placing AT MOST one buy and one sell order in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT