Monday, May 4, 2009

Shanti's Classwork - Rectangle Class

Code without the implementation for the Point object.
Class Definition for Rectangle:


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

namespace RectangleClass
{
class Rectangle
{
//fields
private int length = 0;
private int width = 0;

//properties
public int Length
{
get { return length; }
set { length=value; }
}
public int Width
{
get { return width; }
set { width = value; }
}

//constructors
public Rectangle()
{
}
public Rectangle(int _length, int _width)
{
length = _length;
width = _width;
}

//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;
}
}
}


UI Form



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RectangleClass
{
public partial class Form1 : Form
{
Rectangle myRec1 = new Rectangle(0, 0);

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)
{
MessageBox.Show("Width is " + myRec1.Width);
}

private void btnArea_Click(object sender, EventArgs e)
{
MessageBox.Show("Area is " + myRec1.getArea());
}

private void btnPerimeter_Click(object sender, EventArgs e)
{
MessageBox.Show("Area is " + myRec1.getPerimeter());
}

private void CheckFields()
{

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);

}


}
}

1 comment:

  1. I missed this class so this is a big help for me. Thanks!

    ReplyDelete