Wednesday, July 29, 2009

C# UML Class Diagram




Click to enlarge...


Shape Class:


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

namespace ShapeInheritance
{
public abstract class Shape //enforce that abstract MUST BE OVERRIDEN
{
//fields
protected double xPos; //'protect' modifier used because we want all
protected double yPos; //subclasses will generally inherit those fields

//constructors
public Shape(double _xPos, double _yPos)
{
this.xPos = _xPos;
this.yPos = _yPos;
}

//methods
public abstract double Area(); //enforce that Area HAS TO BE OVERRIDEN BY SUB-CLASSES

public override string ToString()
{
return "Position : (" + xPos.ToString() + ", " + yPos.ToString() + ")\n";
}
}
}


Circle class:


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

namespace ShapeInheritance
{
public class Circle : Shape
{
//fields
private double radius;

//properties
public double Radius
{
get { return radius; }
set { radius = value; }
}

//constructors
public Circle() : base(0,0)
{
}

public Circle(double _xPos, double _yPos) : base(_xPos , _yPos)
{
}

public Circle(double _xPos, double _yPos, double _radius) : base(_xPos, _yPos)
{
this.radius = _radius;
}

//methods
public override double Area()
{
return Math.PI * Math.Pow(Radius, 2);
}

public override string ToString()
{
return base.ToString() + "\nradius : " + radius + "\nArea : " + Area().ToString("N2");
}
}
}


Cylinder class:


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

namespace ShapeInheritance
{
public class Cylinder : Circle
{
//fields
private double height;

//properties
public double Height
{
get { return height; }
set { height = value; }
}

//constructors
public Cylinder() : base(0,0)
{
}

public Cylinder(double _xPos, double _yPos, double _radius, double _height) : base(_xPos, _yPos, _radius)
{
height = _height;
}

//methods
public override double Area()
{
return (2 * base.Area()) + (2 * Math.PI * Radius * height);
}

public double Volume()
{
return Math.PI * Math.Pow(Radius,2) * height;
}

public override string ToString()
{
return base.ToString() + "\nHeight : " + height + "\nVolume : " + Volume().ToString("N2");
}

}
}


Form coding:


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 ShapeInheritance
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnCircle_Click(object sender, EventArgs e)
{
Circle circle = new Circle(0, 0, 10);
lblText.Text = circle.ToString();
}

private void btnCylinder_Click(object sender, EventArgs e)
{
Cylinder cylinder = new Cylinder(0, 0, 10,10);
lblText.Text = cylinder.ToString();
}
}
}


Note:


1. Chain constructors are not used. Sub-constructors are calling the base constructor only.


2. Rectangle class not implemented.

No comments:

Post a Comment