# Console Calculator

Page 1 of 1

## 14 Replies - 18001 Views - Last Post: 04 April 2012 - 10:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=273837&amp;s=e150502877b6a31fa30dccadd80772ed&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Becha

Reputation: 0
• Posts: 5
• Joined: 04-April 12

# Console Calculator

Posted 04 April 2012 - 07:54 AM

Hi,

I am trying to learn C#. There is one example of console calculator I am trying to do and I did something but not everything.....

Question is:

Create class Calculator. This class must contain the following features:

constant PI
constructor that accepts two integers, operand1 and operand2
method for writing values ​​and operand2 operand1
methods for calculating the four operations - addition, subtraction, division and multiplication that will return the result of operand1 and operand2 operation on values
static methods for these operations, which accepts parameters as operands
method that calculates the surface area of the circle diameter circle operand1.
Create a console application that instantiates object of class Calculator, call all the methods and write the result of calculation.

I did something but I got error message of course - could you please tell me what I did wrong and is there any other way to do this?

My solution is :
```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;
}
}

```

This post has been edited by macosxnerd101: 04 April 2012 - 07:55 AM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

## Replies To: Console Calculator

### #2 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Console Calculator

Posted 04 April 2012 - 07:56 AM

Care to tell us what the errors are?

As a side note your brief tells you to accept 'two integers' and not two double's. Not sure if that was by design or not.

This post has been edited by Ryano121: 04 April 2012 - 07:59 AM

### #3 modi123_1

• Suitor #2

Reputation: 13851
• Posts: 55,302
• Joined: 12-June 08

## Re: Console Calculator

Posted 04 April 2012 - 07:57 AM

Quote

I did something but I got error message of course

What's the error message there sport? How about the line it is triggering on?

### #4 Becha

Reputation: 0
• Posts: 5
• Joined: 04-April 12

## Re: Console Calculator

Posted 04 April 2012 - 08:15 AM

Ryano121, on 04 April 2012 - 07:56 AM, said:

Care to tell us what the errors are?

As a side note your brief tells you to accept 'two integers' and not two double's. Not sure if that was by design or not.

Error message states that Main method is missing

....Do you have any idea how to write this from begin to the end?

Because I am beginner...

### #5 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Console Calculator

Posted 04 April 2012 - 08:20 AM

Oh so you haven't actually defined a main method yet then?

Your main method is the entry point of your application. When you program is run, the code inside this method is called (which in turn can call loads of other methods).

```public static void Main()
{
// Use your Calculator class here
}
```

But to be honest if you haven't learnt about the main method yet, and are already starting to build a calculator, I would step back for a minute. Normally the main method is the very FIRST thing you learn about when starting a new language.

### #6 eclipsed4utoo

Reputation: 1533
• Posts: 5,972
• Joined: 21-March 08

## Re: Console Calculator

Posted 04 April 2012 - 08:20 AM

Well, you don't have a Main method. Do you have a Program.cs file in the project?

### #7 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6527
• Posts: 14,422
• Joined: 02-June 10

## Re: Console Calculator

Posted 04 April 2012 - 08:22 AM

Quote

My solution is :

This is one class, not an entire solution. If that really is your entire solution then you threw away everything that Visual Studio stubbed out for you. Your best bet in that case is to make a new solution so VS can build a working skeleton for you again, then integrate your class into it.

Quote

Error message states that Main method is missing

Well, there isn't a main() in your calculator class. So yeah, its missing. Do you know why the main() method is important? If you don't, then you need to stop and go back to page 1 of your book.

Calculator tutorial: See FAQ # 25. (Click the SHOW button below)

What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated Mar 2012
Spoiler

### #8 Becha

Reputation: 0
• Posts: 5
• Joined: 04-April 12

## Re: Console Calculator

Posted 04 April 2012 - 08:33 AM

Yes you are right, I think I need to go back to the first page....but....

I will like to have result or solution for this one....how you will write this as experienced
programer?

I know I need to learn a lot...and your help in this case is more than welcome....I will be gratefull if you can tell me (maybe you know) where I can find more examples with sollution...sometimes is very usefull to read why someone choose for seciefic solution...

### #9 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6527
• Posts: 14,422
• Joined: 02-June 10

## Re: Console Calculator

Posted 04 April 2012 - 08:36 AM

Nobody here is going to just give you a finished working application to turn in as your homework.

You're asking for an example after I already pointed you to a tutorial for building a calculator.

There are numerous threads of console calculators here on DIC. All you have to do is search or look at the bottom of the page:

### #10 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Console Calculator

Posted 04 April 2012 - 08:37 AM

I still fail to believe that you are able to write that Calculator class (if you really did) which entails fields, properties, constructors, static method, parameters, and you don't know what a main method is...

### #11 Sergio Tapia

• D.I.C Lover

Reputation: 1258
• Posts: 4,168
• Joined: 27-January 10

## Re: Console Calculator

Posted 04 April 2012 - 08:45 AM

On the contrary, I believe him.

It's not unheard of for students to just learn things mechanically and copy paste code from examples given by his teacher and mold it into something he needs. Hell I've done this personally when I was in my first year in University. Not that strange really.

OP, can I suggest you buy Head First C#? Buy it, and give a couple of days worth of reading and you'll see how easy C#. You'll benefit greatly and the book is very light reading.

### #12 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6527
• Posts: 14,422
• Joined: 02-June 10

## Re: Console Calculator

Posted 04 April 2012 - 08:54 AM

Quote

Where do I start?

You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.

Spoiler

If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
Spoiler

Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.

Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler

### #13 Becha

Reputation: 0
• Posts: 5
• Joined: 04-April 12

## Re: Console Calculator

Posted 04 April 2012 - 09:06 AM

Look I am not in school, I am trying to do ple and I have nobody to ask :-)

Thank you for your help and examples of calculator, but, my question is if you have something what you know it is usefull when you have noone to ask....to learn C#.....something with questions and answers....or you think it is wrong way to learn C#?

For the example I wrote before...yes I did it but...maybe it was to big step to start with something like it....i was just curious what can be another result and is this one correct....
nothing more, nothing less

### #14 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Console Calculator

Posted 04 April 2012 - 09:11 AM

The trouble with learning by yourself (with no book) is that you end up with huge gaps in your knowledge.

When you first start out you want to go really fast, do what you want to do because you think it will be a good idea. But that way you end up missing very important things that will cause you problems in the future (such as not knowing what a main method is).

A lot of people like to learn by seeing how other people would do things, but to be honest you don't learn that way. You just think 'Yeah I would do it that way next time', but don't end up writing any code at all. Yes the best way to learn is to do, but only do the things that you have a firm grasp on beforehand.

As has been said a couple of times. Get a good book on C#. Start from page 1 and go through it all. After you have a good grip on the language, then you start doing other projects.

### #15 Becha

Reputation: 0
• Posts: 5
• Joined: 04-April 12

## Re: Console Calculator

Posted 04 April 2012 - 10:17 AM

Ryano121, on 04 April 2012 - 09:11 AM, said:

The trouble with learning by yourself (with no book) is that you end up with huge gaps in your knowledge.

When you first start out you want to go really fast, do what you want to do because you think it will be a good idea. But that way you end up missing very important things that will cause you problems in the future (such as not knowing what a main method is).

A lot of people like to learn by seeing how other people would do things, but to be honest you don't learn that way. You just think 'Yeah I would do it that way next time', but don't end up writing any code at all. Yes the best way to learn is to do, but only do the things that you have a firm grasp on beforehand.

As has been said a couple of times. Get a good book on C#. Start from page 1 and go through it all. After you have a good grip on the language, then you start doing other projects.

Ryano you are right, I just play and try to find if it will work....some things were but this one not :-)