Wednesday, October 22, 2014
Polymorphism With Circle Shapes
namespace Polymorphic_Shapes
{
public class Circle
{
private double _radius;
public Circle(double radius)
{
_radius = radius;
}
//default constructor
public Circle()
{
_radius = 1;
}
public virtual double Radius { get { return _radius; } }
public virtual double Height { get { return 0; } }
//the word virtual is added to override or Change circle to sphere)
public virtual double Area()
{
return Math.PI * _radius * _radius;
}
public virtual double Perimeter()
{
return 2 * Math.PI * _radius;
}
public virtual double Volume()
{
return 0;
}
}
}
//=========================================================
namespace Polymorphic_Shapes
{
public class Sphere : Circle
{
public Sphere(double radius):base(radius)
{
}
//override methods that are defined as virtual in the parent class
public override double Area()
{
return 4 * Math.PI * Radius * Radius;
}
public override double Volume()
{
return (4.0 / 3) * Math.PI * Radius * Radius * Radius;
}
}
}
//=========================================================
namespace Polymorphic_Shapes
{
//define a class Cylinder that inherits from Circle
public class Cylinder : Circle
{
private double _height;
public Cylinder(double radius, double height) : base (radius)
{
_height = height;
}
public override double Height
{
get { return _height; }
}
public override double Area()
{
return 2 * Math.PI * Radius * Height;
}
public override double Volume()
{
return Math.PI * Radius * Radius * Height;
}
}
}
//=========================================================
namespace Polymorphic_Shapes
{
public class Cone : Cylinder
{
public Cone(double radius, double height): base(radius, height)
{
//initialize any fields defined in this class, if any
}
public override double Area()
{
return Math.PI * Radius * (Radius + Math.Sqrt(Height * Height + Radius * Radius));
}
public override double Volume()
{
return Math.PI * Radius * Radius * Height / 3;
}
}
}
//=========================================================
namespace Polymorphic_Shapes
{
public partial class Form1 : Form
{
//Introduction to polymorphism (polymorphic inheritance)
//This is the 3rd principle of object-oriented programming
//the other 2 are: Encapsulation / Data hiding, and Inheritance
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void btnCreateRandomCircles_Click(object sender, EventArgs e)
{
double radius = rand.Next(10, 20) + rand.NextDouble();
Circle c = new Circle(radius);
Display(c);
}
private void btnCreateRandomSphere_Click(object sender, EventArgs e)
{
double radius = rand.Next(10, 20) + rand.NextDouble();
Sphere sphere = new Sphere(radius);
Display(sphere);
}
private void Display(Circle cshape)
{
double area = cshape.Area();
double perimeter = cshape.Perimeter();
double vol = cshape.Volume();
richTextBox1.AppendText(String.Format("Type: {0} Radius: {1:f} Area : {2:f} Peri: {3:f} Volume: {4:f} \n\n",
cshape.GetType().Name, cshape.Radius, area, perimeter, vol ));
}
private void btnCreateCylinder_Click(object sender, EventArgs e)
{
double radius = rand.Next(10, 20) + rand.NextDouble();
double h = rand.Next(15, 25) + rand.NextDouble();
Cylinder cy = new Cylinder(radius, h);
Display(cy);
}
private void btnCreateCone_Click(object sender, EventArgs e)
{
double radius = rand.Next(10, 20) + rand.NextDouble();
double h = rand.Next(15, 25) + rand.NextDouble();
Cone c = new Cone(radius, h);
Display(c);
}
}
}
Labels:
CSI-155
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment