Monday, November 3, 2014

AbstractClass_Lab_10_24_14


namespace Zau_AbstractClass_Lab_10_24_14
{

    public abstract class BaseShape
    {
        public abstract double Length { get; }
        public abstract double Width { get; }
        public abstract double Height { get; }

        public abstract double Area();
        public abstract double Perimeter();
        public abstract double Volume();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Zau_AbstractClass_Lab_10_24_14
{
    public class Sphere : BaseShape
    {
        internal protected double _radius;
        //Constructor
        public Sphere(double radius)
        {
            _radius = radius;
        }

        //Property
        public virtual double Radius { get { return _radius; } }
   
        public override double Length
        {get { return 2 * Radius; }}
        public override double Width
        {get { return 2 * Radius; }}
        public override double Height
        {get { return 2 * Radius; }}

        public override double Area()
        {
            return 4 * Math.PI * Radius * Radius;
        }

        public override double Perimeter()
        {
            return 4 * Math.PI * Radius * Radius;
        }

        public override double Volume()
        {
            return 4D / 3D * Math.PI * Math.Pow(Radius, 3);
        }

        public override string ToString()
        {
            return String.Format("Type: {0}  Radius: {1:f}  Area : {2:f}  Peri: {3:f}  Volume: {4:f} \n\n",
                GetType().Name, Radius, Area(), Perimeter(), Volume());
        }
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Zau_AbstractClass_Lab_10_24_14
{
    public class Cone : Sphere
    {
        internal protected double _height;

        public Cone(double radius, double height)
            : base(radius)
        {
            //initialize any fields defined in this class, if any
            this._height = height;
        }
        public override double Height
        { get { return _height; } }

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

        public override string ToString()
        {
            return String.Format("Type: {0}  Radius: {1:f}  Height: {2:f}  Area : {3:f}  Peri: {4:f}  Volume: {5:f} \n\n",
                GetType().Name, Radius, Height, Area(), Perimeter(), Volume());
        }
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Zau_AbstractClass_Lab_10_24_14
{
    public class RectBox : BaseShape
    {
        internal protected double _length, _width, _height;

        //Constructor
        public RectBox(double length, double width, double height)
        {
            this._length = length;
            this._width = width;
            this._height = height;
        }

        public override double Length
        {get { return _length; }}
        public override double Width
        { get { return _width; } }
        public override double Height
        {get { return _height; }}


        //methods override
        public override double Perimeter()
        {
            // P = 4L+4W+4H
            return (4 * Length) + (4 * Width) + (4 * Height);
        }

        public override double Area()
        {
            return 2 * (Length * Height) + 2 * (Length * Width) + 2 * (Width * Height);
        }

        public override double Volume()
        {
            return Length * Width * Height;
        }

        public override string ToString()
        {
            return String.Format("Type: {0}  Length: {1:f}  Width: {2:f}  Height: {3:f} Area : {4:f}  Peri: {5:f}  Volume: {6:f} \n\n",
                GetType().Name, Length, Width, Height, Area(), Perimeter(), Volume());
        }
    }

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Zau_AbstractClass_Lab_10_24_14
{
    public class RectPyramid : RectBox
    {
        public RectPyramid(double length, double width, double height)
            : base(length, width, height)
        {

        }

        public override double Area()
        {

            return Length * Width + (Length * Math.Sqrt(Math.Pow((Width / 2), 2) +
                Math.Pow(Height, 2)) + Width * Math.Sqrt(Math.Pow((Length / 2), 2) + Math.Pow(Height, 2)));
        }

        public override double Volume()
        {
            return 1f / 3f * (Length * Width) * Height;
        }

        public override string ToString()
        {
            return String.Format("Type: {0}  Length: {1:f}  Width: {2:f}  Height: {3:f} Area : {4:f}  Volume: {5:f} \n\n",
                GetType().Name, Length, Width, Height, Area(), Volume());
        }

    }

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Zau_AbstractClass_Lab_10_24_14
{
    public partial class Form1 : Form
    {
        List<BaseShape> shapeList = new List<BaseShape>();
        Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreateSphere_Click(object sender, EventArgs e)
        {
            double radius = rand.Next(10, 20) + rand.NextDouble();
            Sphere sphere = new Sphere(radius);
            shapeList.Add(sphere);
            MessageBox.Show("A sphere has been added");
            Display(sphere);
        }

        private void btnCreateRectBox_Click(object sender, EventArgs e)
        {
            double length = rand.Next(10, 100);
            double width = rand.Next(5, 50);
            double height = rand.Next(5, 50);
            RectBox b = new RectBox(length, width, height);
            shapeList.Add(b);
            MessageBox.Show("A Rectangle box has been added");
            Display(b);
        }

        private void btnCreateRectPyramid_Click(object sender, EventArgs e)
        {
            double length = rand.Next(10, 100);
            double width = rand.Next(5, 50);
            double height = rand.Next(5, 50);
            RectPyramid rp = new RectPyramid(length, width, height);
            shapeList.Add(rp);
            MessageBox.Show("A Rectangle Pyramid has been added");
            Display(rp);
        }

        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);
            shapeList.Add(c);
            MessageBox.Show("A Cone has been added");
            Display(c);
        }

        private void Display(BaseShape bshape)
        {
            richTextBox1.AppendText(bshape.ToString());
        }

    }

}

No comments:

Post a Comment