I have tried to write basic calculator code which also calculate surface area of the circle.
I created classes needed for this task but struggle with Main method defining. Can someone help me to complete this coding?

``````class Calculator
{
private Double _num1;
private Double _num2;

public Double PI
{
get
{
return Math.PI;
}
}

public Calculator(Double num1, Double num2)
{
this._num1 = num1;
this._num2 = num2;
}

public void WriteNumber1()
{
Console.WriteLine(this._num1);
}

public void WriteNumber2()
{
Console.WriteLine(this._num2);
}

{
return this._num1 + this._num2;
}

public Double Subtract()
{
return this._num1 - this._num2;
}

public Double Multiply()
{
return this._num1 * this._num2;
}

public Double Divide()
{
return this._num1 / this._num2;
}

public static Double Add(Double num1,Double num2)
{
return num1 + num2;
}

public static Double Subtract(Double num1, Double num2)
{
return num1 - num2;
}

public static Double Multiply(Double num1, Double num2)
{
return num1 * num2;
}

public static Double Divide(Double num1, Double num2)
{
return num1 / num2;
}
public static Double cicrcle(Double r)
{
return Math.PI * 2 * r;
}
}
``````

Hi

Well some of your methods in your Calculator class are defined static (such as the cicrcle method) which means that you do not need to create an instance of the Calculator class in order to use it. This is highlighted in my example above. You can simply call `Calculator.cicrcle(your …`

Yes, you can do this. I assume you will be reading entries from a Console application? If so, you use the Console.WriteLine to ask for the entry for the first number and then Console.ReadLine to read the value entered. Repeat for the second number and then instantiate your Calculator.

You …

I tried with this but got error (CalculatorTest.Calculator doesn't contain a constructor that takes 1 argument).

Did you read my previous post? I explained how currently your Calculator class only supports instantiation with two arguments due to the fact that you only have one constructor in the Calculator …

## All 10 Replies

Hi

Not sure what you mean by Main method defining. Do you mean, how to use this class within your console application? If so, create a console application and you will find a Main method. Then if your class is part of the same file or you have added a reference to it you can simply call the Calculator.cicrcle method (mispelt by the way). For example:

``````class Program
{
static void Main(string[] args)
{
Console.WriteLine( Calculator.cicrcle(5));
}
}

class Calculator
{
......
}
``````

In the above, the Calculator class is part of the same console application.

If this is not what you meant, could you provide more info.

HTH

I mean how to call classes defined above and execute calculator operations and surfacee area of the circle.

Hi

Well some of your methods in your Calculator class are defined static (such as the cicrcle method) which means that you do not need to create an instance of the Calculator class in order to use it. This is highlighted in my example above. You can simply call `Calculator.cicrcle(your number)`.

Others however are not defined as static and therefore you need to create an instance of the Calculator class. So if you wanted to work with the Subtract method an example would be:

``````class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator(7, 5);
Console.WriteLine(calc.Subtract().ToString());
}
}
``````

Is that what you mean?

Yes this is what I meant. Can I put variables num1 and num2 instead of 7 and 5 in your code in order to enable a user to enter values:

``````Calculator calc = new Calculator(num1, num2);
``````

Yes, you can do this. I assume you will be reading entries from a Console application? If so, you use the Console.WriteLine to ask for the entry for the first number and then Console.ReadLine to read the value entered. Repeat for the second number and then instantiate your Calculator.

You will need to add some validation however to ensure that the values entered are of a type supported (in this case doubles). To do this, you can make use of the double.TryParse method which will attempt to take a string and convert it to a double.

I'll debug this code a bit later but anyway thank you for support and help. Very useful!

This is the complete code and it works.

Surface area of the circle is calculated by using a static method. How can I do this calculation in this code by using the class instantiate instead of the static method (for surface area of the circle is used operand 1 as a radius line)? I struggle with the math oparation in this case.

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculatorTest
{
class Calculator
{
public int operand1;
public int operand2;

public Calculator(int operand1, int operand2)
{
this.operand1 = operand1;
this.operand2 = operand2;
}
{
return this.operand1 + this.operand2;
}
public int Subtract()
{
return this.operand1 - this.operand2;
}
public int Multiply()
{
return this.operand1 * this.operand2;
}
public int Divide()
{
return this.operand1 / this.operand2;
}
}
class Program
{
//Constant PI for surface area of the circle
const double PI = 3.14;

static void Main(string[] args)
{
Console.WriteLine("Enter operand 1:");

Console.WriteLine("Enter operand 2:");

//Surface area of the circle
double o = Math.Pow(operand1, 2) * PI;
Console.WriteLine("Surface area of the circle:");
Console.WriteLine(o);

//Calculator (+, -, *, /)
Console.WriteLine("Choose an operation:");
Console.WriteLine("2 - Substruct");
Console.WriteLine("3 - Multiply");
Console.WriteLine("4 - Divide");

switch (z)
{
case 1:
Console.WriteLine("Result:");
Console.WriteLine(operand1 + operand2);
break;

case 2:
Console.WriteLine("Result:");
Console.WriteLine(operand1 - operand2);
break;

case 3:
Console.WriteLine("Result:");
Console.WriteLine(operand1 * operand2);
break;

case 4:
Console.WriteLine("Result:");
Console.WriteLine(operand1 / operand2);
break;
}

Calculator calc = new Calculator(operand1, operand2);

Calculator calc2 = new Calculator(operand1, operand2);
Console.WriteLine(calc2.Subtract().ToString());

Calculator calc3 = new Calculator(operand1, operand2);
Console.WriteLine(calc3.Multiply().ToString());

Calculator calc4 = new Calculator(operand1, operand2);
Console.WriteLine(calc4.Divide().ToString());

}
}
}
``````

Hi

Surface area of the circle is calculated by using a static method. How can I do this calculation in this code by using the class instantiate instead of the static method

Add the Circle method to your Calculator class in the same way as you have your Add, Multiply, Subtract methods (that is, without the use of the static keyword). This method would take one argument which I note from your class is provided via the constructor of the Calculator class. However, the constructor takes two arguments so you will need another constructor. If we follow the pattern that you have so far then this constructor would take one argument.

You can do this by adding the following additional constructor to the Calculator class:

``````    public Calculator(int operand1)
{

}
``````

Now your Calculator class has a new constructor that takes one argument and the PI constant so your Circle method should now work with these two values to return the value.

The above should be enough to get it working but I wanted to point out something else. I don't think the way that you have your Calculator class setup at the moment is very readable. What I mean is that when I look at your calling program (the console application) I read the instantiation and the fact that you pass two values to it then I read an action (the method, Add, Divide etc.) I would find it more readable if I saw something like calc.Add(1, 2). This would also negate the need to have additional constructors for methods that work on a number of operands greater or less than two.

I would also make use of static so that you did not have to instantiate the class each time. So the class would look like:

``````class Calculator
{
const double PI = 3.14;

public static int Add(int operand1, int operand2)
{
return operand1 + operand2;
}
public static int Subtract(int operand1, int operand2)
{
return operand1 - operand2;
}
public static int Multiply(int operand1, int operand2)
{
return operand1 * operand2;
}
public static int Divide(int operand1, int operand2)
{
return operand1 / operand2;
}
public static double circle(int operand1)
{
//your method here to calculate circle

}
}
``````

Then your calling code looks like:

``````    Console.WriteLine(Calculator.Add(1, 2).ToString());
Console.WriteLine(Calculator.circle(5).ToString());
``````

You don't have to follow this advice, your code is fine the way you have it but wanted to show you how you might improve it. Also, check out Static and Instance Members for more information.

I tried with this but got error (CalculatorTest.Calculator doesn't contain a constructor that takes 1 argument).

Constructor

``````public double circle()
{
return this.operand1 * this.operand1 * PI;
}
``````

Calling code

``````Calculator surface = new Calculator(operand1); //Error
Console.WriteLine(surface.circle().ToString);  //Error
``````

I tried with this but got error (CalculatorTest.Calculator doesn't contain a constructor that takes 1 argument).

Did you read my previous post? I explained how currently your Calculator class only supports instantiation with two arguments due to the fact that you only have one constructor in the Calculator class. You will need to add another constructor to the class that takes one argument, for example:

``````    public Calculator(int operand1)
{
this.operand1 = operand1;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.