Results 1 to 3 of 3

Thread: calculator Class?

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    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
    Name:  picClsss.PNG
Views: 303
Size:  15.9 KB

    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 operand1decimal operand2)
            {
                return 
    operand1 operand2;
                }
            public 
    decimal Subtract(decimal operand1decimal operand2)
            {
                return 
    operand1 operand2;
            }
            public 
    decimal Multiply(decimal operand1decimal operand2)
            {
                return 
    operand1 operand2;
                }
            
            public 
    decimal Divide(decimal operand1decimal operand2)
            {
                return 
    operand1 operand2;
                }


        }

    Last edited by QuestionPlease; Nov 14th, 2012 at 11:02 PM.

  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: calculator Class?

    Why not use an automatic property?
    csharp Code:
    1. 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.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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:
    1. Calculator calculator = new Calculator();
    2. // calculator.CurrentValue == 0
    3. calculator.EnterValue(5);
    4. // calculator.CurrentValue == 5
    5. calculator.Add();
    6. // calculator.CurrentValue == 0 (? probably)
    7. calculator.EnterValue(3);
    8. // calculator.CurrentValue == 3
    9. calculator.Equals();
    10. // 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
  •  



Click Here to Expand Forum to Full Width