-
Nov 14th, 2012, 10:38 PM
#1
Thread Starter
Junior Member
calculator Class?
I need to create a calculator class for my basic calculator application that will implement the functions of the Calculator?
The design of the calculator class should be in this
This is what I have so far, am so confused?
PHP Code:
public class Calculator
{
private decimal currentValue;
public Calculator()
{
}
public Calculator(decimal currentValue)
{
this.CurrentValue = currentValue;
}
public decimal CurrnetValue
{
get
{
return currentValue;
}
set
{
currentValue = value;
}
}
public decimal Add(decimal operand1, decimal operand2)
{
return operand1 + operand2;
}
public decimal Subtract(decimal operand1, decimal operand2)
{
return operand1 - operand2;
}
public decimal Multiply(decimal operand1, decimal operand2)
{
return operand1 * operand2;
}
public decimal Divide(decimal operand1, decimal operand2)
{
return operand1 / operand2;
}
}
}
Last edited by QuestionPlease; Nov 14th, 2012 at 11:02 PM.
-
Nov 17th, 2012, 03:09 PM
#2
Re: calculator Class?
Why not use an automatic property?
csharp Code:
public decimal CurrentValue { get; set; }
Also, instead of 2 parameters for what you've got, you should be utilizing the CurrentValue and having the first and only param for each of these functions deal with the mathematical operations with CurrentValue and that param instead. And setting CurrentValue in the process. The way you have it there's no way to keep track of the CurrentValue for this instance.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Nov 19th, 2012, 10:45 AM
#3
Re: calculator Class?
I think you have misunderstood. From my reading of your assignment, you need to maintain state inside the class, so that it would be used something like this:
csharp Code:
Calculator calculator = new Calculator(); // calculator.CurrentValue == 0 calculator.EnterValue(5); // calculator.CurrentValue == 5 calculator.Add(); // calculator.CurrentValue == 0 (? probably) calculator.EnterValue(3); // calculator.CurrentValue == 3 calculator.Equals(); // calculator.CurrentValue == 8
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|