using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RectangleClass
{
public class Rectangle
{
//fields
private int length = 0;
private int width = 0;
private Point corner = new Point(0,0);
//properties
public int Length
{
get { return length; }
set { length=value; }
}
public int Width
{
get { return width; }
set { width = value; }
}
public Point Corner
{
get { return corner; }
set { corner = value; }
}
//constructors
public Rectangle()
{
}
public Rectangle(int _length, int _width, Point _corner)
{
length = _length;
width = _width;
corner = _corner;
}
//methods
public int getArea()
{
return length * width;
}
public int getPerimeter()
{
return (length + width) * 2;
}
public void increaseLength(int _lengthIncrease)
{
length = length + _lengthIncrease;
}
public void increaseWidth(int _widthIncrease)
{
width = width + _widthIncrease;
}
public string getCenter()
{
corner.setPoint(length / 2, width / 2);
return corner.ToString();
}
public string getTopRight()
{
corner.setPoint(length, 0);
return corner.ToString();
}
public string getBottomLeft()
{
corner.setPoint(0, width);
return corner.ToString();
}
public string getBottomRight()
{
corner.setPoint(length, width);
return corner.ToString();
}
public string getAllPoints()
{
string sAllPoints = "";
sAllPoints = "Center = " + getCenter() + "\n" +
"Top Right = " + getTopRight() + "\n" +
"Bottom Left = " + getBottomLeft() + "\n" +
"Bottom Right = " + getBottomRight();
return sAllPoints;
}
}
}
Point Class (Used to override the ToString() function) - optional
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RectangleClass
{
public class Point
{
//fields
private int x, y;
// Default constructor:
public Point()
{
x = 0;
y = 0;
}
// A constructor with two arguments:
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
// Override the ToString method:
public override string ToString()
{
return (String.Format("({0},{1})", x, y));
}
public void setPoint(int _x, int _y)
{
this.x = _x;
this.y = _y;
}
}
}
Form Coding
Rectangle myRec1 = new Rectangle(0, 0, new Point());
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnGetLength_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show("Length is " + myRec1.Length);
}
private void btnGetWidth_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show("Width is " + myRec1.Width);
}
private void btnArea_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show("Area is " + myRec1.getArea());
}
private void btnPerimeter_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show("Area is " + myRec1.getPerimeter());
}
private void CheckFields()
{
myRec1.Length = 0;
myRec1.Width = 0;
if (txtLengthInc.Text != "")
myRec1.increaseLength(int.Parse(txtLengthInc.Text));
if (txtWidthInc.Text != "")
myRec1.increaseWidth(int.Parse(txtWidthInc.Text));
if (txtLength.Text != "")
myRec1.Length = myRec1.Length + int.Parse(txtLength.Text);
if (txtWidth.Text != "")
myRec1.Width = myRec1.Width + int.Parse(txtWidth.Text);
}
private void btnCenter_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show ("Center : " + myRec1.getCenter());
}
private void btnTopRight_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show("Top Right : " + myRec1.getTopRight());
}
private void btnBottomLeft_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show("Bottom Left : " + myRec1.getBottomLeft ());
}
private void btnButtonRight_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show("Bottom Right : " + myRec1.getBottomRight());
}
private void btnAllPoints_Click(object sender, EventArgs e)
{
CheckFields();
MessageBox.Show(myRec1.getAllPoints());
}
No comments:
Post a Comment