home / study / engineering / computer science / questions and answers / i need h
ID: 3836898 • Letter: H
Question
home / study / engineering / computer science / questions and answers / i need help with this c# program i have most of ...
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: I need help with this C# program I have most of it...
Bookmark
I need help with this C# program
I have most of it done I just need to know how to Validate this simple program.
It must be done in C# and must be a form. Also I am a beginner so please make it super easy to understand. I would also love if you could explain what you did as you do this.
Directions below
--------------------------------------------------------------------------------------------------
In this exercise, you’ll add data validation to the Simple Calculator form of extra exercise A5-E1.
1.Open the SimpleCalculator project in the Assignment5SimpleCalculatorValidation directory.
2. Code methods named
IsPresent,
IsDecimal, and
IsWithinRange
that work like the methods described in Murach’s chapter 7 of the book.
3. Code a method named IsOperator that checks that the text box that’s passed to it contains a value of +, -, *, or /.
4. Code a method named IsValidData that checks that the Operand 1 and Operand 2 text boxes contain a decimal value between 0 and 1,000,000 (non-inclusive) and that the Operator text box contains a valid operator.
5. Delete all of the catch blocks from the try-catch statement in the btnCalculate_Click event handler except for the one that catches any exception. Then, add code to this event handler that performs the calculation and displays the result only if the values of the text boxes are valid.
6. Test the application to be sure that all the data is validated properly.
------------------------------------------------------------------------------------------------------------------------
The code that needs to be modified below
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();
}
}
}
Explanation / Answer
namespace Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
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 Boolean IsPresent() {
if (txtOperand1.Text != "" && txtOperand2.Text != null && txtOperator.Text != null)
return true;
return false;
}
private Boolean IsDecimal() {
//here we are trying to parse and assign the textbox contents into the decimal variables.So that we can verify the operands are decimal if it succeeds.
decimal a,b;
if ((decimal.TryParse(txtOperand1.Text, out a))&& (decimal.TryParse(txtOperand2.Text, out b)))
return true;
return false;
}
private Boolean IsWithinRange(decimal operand1,decimal operand2)
{
if (operand1>=0 && operand1<=1000000 && operand2 >= 0 && operand2 <= 1000000)
return true;
return false;
}
private Boolean IsOperator(string operator1) {
if(operator1 == "*" || operator1 == "/" || operator1 == "-" || operator1 == "+")
return true;
return false;
}
private Boolean IsValidData(decimal operand1, decimal operand2)
{
if (operand1 >= 0 && operand1 <= 1000000 && operand2 >= 0 && operand2 <= 1000000)
return true;
return false;
}
private void btnCalculate_Click_1(object sender, EventArgs e)
{
//IsPresent method will verify whether all the text boxes are not empty and stop the calculation if it's empty.
if (!IsPresent())
{
MessageBox.Show("Text boxes are empty.", "Error!");
txtOperand1.Focus();
return;
}
//IsDecimal method will try to check whether operands in textbox1 and textbox3 are decimals.
if (!IsDecimal())
{
MessageBox.Show("Operands are not decimal.", "Error!");
txtOperand1.Focus();
return;
}
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
//IsWithinRange will check whether operands are within a range or not and will return boolean values according to that.
if (!IsWithinRange(operand1,operand2))
{
MessageBox.Show("Operands are not within range.", "Error!");
txtOperand1.Focus();
return;
}
string operator1 = txtOperator.Text;
//IsOperator will check whether the operator passed in operator1 text box is valid or not.
if (!IsOperator(operator1))
{
MessageBox.Show("Not a valid operator.", "Error!");
txtOperand1.Focus();
return;
}
//IsValidData will check Operand 1 and Operand 2 text boxes contain a decimal value between 0 and 1,000,000
if (!IsValidData(operand1, operand2))
{
MessageBox.Show("Operands are not within range.", "Error!");
txtOperand1.Focus();
return;
}
decimal result = Calculate(operand1, operator1, operand2);
result = Math.Round(result, 4);
this.txtResult.Text = result.ToString();
txtOperator.Focus();
}
private void btnExit_Click_1(object sender, EventArgs e)
{
this.Close();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.