469,646 Members | 1,473 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Good OOP practice?

I'm currently doing a course in C# programming fundamentals.
Please will you check and comment on the following assignment:

Assignment: Create a simple calculator prgram that illistrates good OOP
practice.

I have written the following console program that I hope answers this
question (and it does work), unfortunately I couldn't really find a place
to demonstrate the concept of Encapsulation (or maybe I just dont understand
it : )
Anyway here it is:

1. What is your opinion?
2. Are all my //comments correct?

----------------------------------------------------------------------------
----------------------

using System;

class NumericInput
{
// Declare Class variables
private double convertme;
private string input;
private double converted;

// Conversion Method
public double convertMethod(string inputstring)
{
convertme = Convert.ToDouble(inputstring);
return convertme;
}

// Get User Input Method
public double getinput(string prompt)
{
Console.Write(prompt);
input = Console.ReadLine();
converted = convertMethod(input);
return converted;
}
}

class Calculate
{
// Declare Class variables
private double result;

// Sum Method
public double addMethod(double num1, double num2)
{
result = (num1+num2);
return result;
}
}

class Calculator
{
// main Method runs on startup
public static void Main()
{
// Declare Variables
double myresult;
double input1, input2;

// Instantiate Object NumericInput
NumericInput userInput = new NumericInput();

input1 = userInput.getinput("Enter your first number:");
input2 = userInput.getinput("Enter your second number");

// Instantate Object Calculate
Calculate calc = new Calculate();

// Invoke Object Calculate's addMethod
myresult = calc.addMethod(input1,input2);
Console.WriteLine("The answer is: " + myresult);
}
}
Nov 16 '05 #1
5 5272
Okay, I'll make some comments on this.

1: where you have "// Declare Class variables", the more accurate
terminology is "fields" not variables.

2: Your convertMethod() function simply converts a string to a double. Why
not change the line:

converted = convertMethod(input);
to
converted = Convert.ToDouble(input);

That's exactly what convertMethod is doing and your simply adding a layer of
method calls, not to mention ambiguity to the code. convertMethod is a poor
name for the method because it doesn't accurately describe what it does.
Your method names should be descriptive of what the function does.
convertStringToDouble() would be a much better name for it, but in this
case, simply removing it and using Convert.ToDouble() is the best idea. I
simply mention the naming because it's important to name your class members
well to make the code readable by you and others.

As a further note on naming, it's a bad idea to use the word "Method" in
your method names unless it's in a sense that descibes a technique like,
MultiplyMatricesWithStraussenMethod where Straussen's Method is the
technique you're using to multiple matrices. Programmers will presumably
know already that your method is a method. Naming it somethingMethod is
redundant.

Finally, I would get rid of the Calculate class altogether. All it does is
adds two numbers. Creating a class to do something so simply is excessive
and in the real world, there's a huge performance penalty involved for
creating classes to do something so simple. The time spent instantiating the
class and initializing members is going to cost your more than actually
performing the addition that it exists to perform.

But, I will make one comment about your implementation of the Calculate
class. You have "result" as a field of the class but it's used as a if it
were a local variable by the addMethod method (see the redundancy in
naming?). It would be much better ot implement addMethod as follows:

public double addMethod(double num1, double num2)
{
double result = (num1+num2);
return result;
}

and not have result as a class field.

I hope I don't sound overly harsh. My intention is simply to provide
constructive criticism.

Pete
"altergothen" <ju******@webnet.za.net> wrote in message
news:mv********************@is.co.za...
I'm currently doing a course in C# programming fundamentals.
Please will you check and comment on the following assignment:

Assignment: Create a simple calculator prgram that illistrates good OOP
practice.

I have written the following console program that I hope answers this
question (and it does work), unfortunately I couldn't really find a place
to demonstrate the concept of Encapsulation (or maybe I just dont understand it : )
Anyway here it is:

1. What is your opinion?
2. Are all my //comments correct?

-------------------------------------------------------------------------- -- ----------------------

using System;

class NumericInput
{
// Declare Class variables
private double convertme;
private string input;
private double converted;

// Conversion Method
public double convertMethod(string inputstring)
{
convertme = Convert.ToDouble(inputstring);
return convertme;
}

// Get User Input Method
public double getinput(string prompt)
{
Console.Write(prompt);
input = Console.ReadLine();
converted = convertMethod(input);
return converted;
}
}

class Calculate
{
// Declare Class variables
private double result;

// Sum Method
public double addMethod(double num1, double num2)
{
result = (num1+num2);
return result;
}
}

class Calculator
{
// main Method runs on startup
public static void Main()
{
// Declare Variables
double myresult;
double input1, input2;

// Instantiate Object NumericInput
NumericInput userInput = new NumericInput();

input1 = userInput.getinput("Enter your first number:");
input2 = userInput.getinput("Enter your second number");

// Instantate Object Calculate
Calculate calc = new Calculate();

// Invoke Object Calculate's addMethod
myresult = calc.addMethod(input1,input2);
Console.WriteLine("The answer is: " + myresult);
}
}

Nov 16 '05 #2
B S
Thanks a Million Pete !!!
Now this is constructive criticism that a newbie like me can really use
and learn from. All your points make alot of sense. Your time and effort
to comment on my code is much appreciated >:)

- thanks again.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
You're welcome, and after re-reading my post, let me apologize for my
atrocious grammar and spelling. Made me wince to read it.

Pete

"B S" <an*********@devdex.com> wrote in message
news:e%****************@TK2MSFTNGP11.phx.gbl...
Thanks a Million Pete !!!
Now this is constructive criticism that a newbie like me can really use
and learn from. All your points make alot of sense. Your time and effort
to comment on my code is much appreciated >:)

- thanks again.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4
pd******@hotmail.com <pe**@petedavis.net> wrote:
Okay, I'll make some comments on this.

1: where you have "// Declare Class variables", the more accurate
terminology is "fields" not variables.
Not necessarily. The correct terminology (according to the C# spec) is
"instance variables" or "static variables" depending on declaration.
Both are described as fields in the C# spec.

<snip>
But, I will make one comment about your implementation of the Calculate
class. You have "result" as a field of the class but it's used as a if it
were a local variable by the addMethod method (see the redundancy in
naming?). It would be much better ot implement addMethod as follows:

public double addMethod(double num1, double num2)
{
double result = (num1+num2);
return result;
}


I would go further, and avoid the local variable which serves no real
purpose here:

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

Note the capitalisation of the method name to fit with MS guidelines.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon,

You are correct on both points. Thanks for the corrections.

Pete

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
pd******@hotmail.com <pe**@petedavis.net> wrote:
Okay, I'll make some comments on this.

1: where you have "// Declare Class variables", the more accurate
terminology is "fields" not variables.


Not necessarily. The correct terminology (according to the C# spec) is
"instance variables" or "static variables" depending on declaration.
Both are described as fields in the C# spec.

<snip>
But, I will make one comment about your implementation of the Calculate
class. You have "result" as a field of the class but it's used as a if it were a local variable by the addMethod method (see the redundancy in
naming?). It would be much better ot implement addMethod as follows:

public double addMethod(double num1, double num2)
{
double result = (num1+num2);
return result;
}


I would go further, and avoid the local variable which serves no real
purpose here:

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

Note the capitalisation of the method name to fit with MS guidelines.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Erik De Keyser | last post: by
13 posts views Thread by Mel | last post: by
3 posts views Thread by Andy Dingley | last post: by
3 posts views Thread by Andyza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.