Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP Newbie

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Im just getting into "real" OOP, as i have been working with classes in PHP for a while now, so i have a basic knowledge of how things should go, but as some of you know, php isnt a "real" oop, but it has some elements. Anyway lets not go into that. My question is, what should my method structure be like? Can someone just give me some pointers, or whatever, on what i am doing wrong??

Here is one of the sources i have written (dont laugh too hard, i am just learning C#)

Ok, so say i want to take 2 input variables, multiply them together, and get the area and perimeter of a square (this is easy with no oop, and probably with oop too, i just dont get it yet). Ok, so here is what i have.. The syntax is C#, but its almost the same as Java.

using System;
using System.Text.RegularExpressions;
public class DoMath
{
int width;
int height;
int area;
int heightper;
int widthper;
int perim;
public void Width()
{
Console.WriteLine("Please Enter the Width of your Rectangle:");
width = Console.Read();
}
public void Height()
{
Console.WriteLine("Please Enter the Height of your Rectangle:");
height = Console.Read();
}
public void ExecMath(int height, int width)
{
if(height == width){
w = width;
w = height;
} else {
w = width;
h = height;
}
this.area = w * h;
this.perim = w + h;
Console.WriteLine("The Area of your Square is: {0}, and the perimeter is {1}", this.area, this.perim);
}
}
public class GetResults
{
static void Main()
{
DoMath.Width();
DoMath.Height();
DoMath.ExecMath(int height, int width);
}
}

I just have no idea where i am supposed to call what..... Should i read the vars in the methods, or in the Main()?
 
Hi!
I have checked yor code and done some adjustments and comments.
Feel free to ask about things that is unclear.

using System;
using System.Text.RegularExpressions;

public class DoMath
{
//It is good custom to declare variables inside classes as private.
//This means that only the class can access the variables.
//And I have removed heightper and widthper, becasue they are newer used.
private int width;
private int height;
private int area;
private int perim;

//This work fine.
public void Width()
{
Console.WriteLine("Please Enter the Width of your Rectangle:");
width = Console.Read();
}
//This to.
public void Height()
{
Console.WriteLine("Please Enter the Height of your Rectangle:");
height = Console.Read();
}
//Here I have done some changes.
//First I removed the if, and then simplified the calculation.
//Because the area is always height times width and therefore it is unnecessary
//to check if they are equal.
//The w and h you used wasn't declared and they would have caused a compile error.
public void ExecMath(int height, int width)
{
this.area = width * height;
this.perim = width + height;
Console.WriteLine("The Area of your Square is: {0}, and the perimeter is {1}",
this.area, this.perim);
}
}

public class GetResults
{
static void Main()
{
//To use a class you need to declare a variable of the class.
DoMath d;

d.Width();
d.Height();
d.ExecMath(10, 20);
}
}

Larsson
 
Thank you! So i guess i wasn't terribly horribly off, but I see what you did, and it makes sense.

I see that you put a 10,20 in the attr. list of the ExecMath function, and that would make the Height 10, and the Width 20, or vice versa, but how would i take the input that was recieved and use that as the values? Or is it already doing that?

I'm at school right now, so i don't have my compiler, but i'll try it when i get home. Thakns for the help!
 
Yes, the 10 and 20 is the values you want to calculate, but I just noticed that you don't need any input variables in the ExecMath-function because you use Width() and Height() for that.
Here is how it should look; I have also included an overloaded function of ExecMath() that takes two int variables.
An overloaded function is a way to use the same function name but with different input variables. This so you can handle different type of data.

using System;
using System.Text.RegularExpressions;

public class DoMath
{
//It is good custom to declare variables inside classes as private.
//This means that only the class can access the variables.
//And I have removed heightper and widthper, becasue they are newer used.
private int width;
private int height;
private int area;
private int perim;

//This work fine.
public void Width()
{
Console.WriteLine("Please Enter the Width of your Rectangle:");
width = Console.Read();
}
//This to.
public void Height()
{
Console.WriteLine("Please Enter the Height of your Rectangle:");
height = Console.Read();
}
//Here I have done some changes.
//First I removed the if, and then simplified the calculation.
//Because the area is always height times width and therefore it is unnecessary
//to check if they are equal.
//The w and h you used wasn't declared and they would have caused a compile error.
public void ExecMath(int h, int w)
{
this.area = w * h;
this.perim = w + h;
Console.WriteLine("The Area of your Square is: {0}, and the perimeter is {1}",
this.area, this.perim);
}

//This function uses the varibles that the user has decided. The above does not.
//I have also removed the this pointer because it's not needed in this function.
public void ExecMath()
{
area = width * heigth;
perim = width + heigth;
Console.WriteLine("The Area of your Square is: {0}, and the perimeter is {1}",
area, perim);
}
}

public class GetResults
{
static void Main()
{
//To use a class you need to declare a variable of the class.
DoMath d;

d.Width();
d.Height();
d.ExecMath(); //Uses the overloaded function without any input.
d.ExecMath(10, 20); //Uses the overloaded functiion with input.
}
}

Larsson
 
Small math point.....

2 widths and two heights on every rectangle so.....

Perim = 2*(width+height)

Craig
 
Something you may want to look at doing is separating your user input from the class that actually describes a rectangle. Try something like this:
Code:
using System;

namespace ClassLibrary1 {
	public class TT_Rectangle {
		bool _dirty;
		int _width;
		int _height;
		int _area;
		int _perimeter;

		public TT_Rectangle() {
			_dirty = false;
			_width = 0;
			_height = 0;
			_area = 0;
			_perimeter = 0;
		}

		public int Width {
			get {
				return _width;
			}
			set {
				_dirty = true;
				_width = value;
			}
		}

		public int Height {
			get {
				return _height;
			}
			set {
				_dirty = true;
				_height = value;
			}
		}

		public int Area {
			get {
				if (_dirty) {
					DoMath();
				}
				return _area;
			}
		}

		public int Perimeter {
			get {
				if (_dirty) {
					DoMath();
				}
				return _perimeter;
			}
		}

		private void DoMath() {
			_area = _height * _width;
			_perimeter = 2 * (_height + _width);
			_dirty = false;
		}
	}
	public class TT_Main 
	{
		static void Main() 
		{
			TT_Rectangle MyRect = new TT_Rectangle();

			Console.WriteLine("Please Enter the Width of your Rectangle:");
			MyRect.Width = Console.Read();

			Console.WriteLine("Please Enter the Height of your Rectangle:");
			MyRect.Height = Console.Read();

			Console.WriteLine("The Area of your Square is: {0}, and the perimeter is {1}", 
				MyRect.Area, MyRect.Perimeter);

		}

	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top