Thursday, May 1, 2014

Class With Method_Lab (Cylinder)


namespace Zau_Class_With_Method_Lab
{

    public partial class Form1 : Form
    {
        List<Cylinder> cylinderList = new List<Cylinder>();
        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreateCylinder_Click(object sender, EventArgs e)
        {
            //get a random height and radius
            double height = rand.Next(6, 12) + rand.NextDouble();
            double radius = rand.Next(2, 6) + rand.NextDouble();

            //create
            Cylinder cylinder1 = new Cylinder(height, radius);
            //---------------------------------------------
            height = rand.Next(6, 12) + rand.NextDouble();
            radius = rand.Next(2, 6) + rand.NextDouble();

            Cylinder cylinder2 = new Cylinder(height, radius);

            //Save Cylinders
            cylinderList.Add(cylinder1);
            cylinderList.Add(cylinder2);
            //Display both boxes
            DisplayCylinder(cylinder1);
            DisplayCylinder(cylinder2);

            DisplayCount();
        }
        private void DisplayCylinder(Cylinder cylinder)
        {
            richTextBox1.AppendText("Height: " + cylinder.Height.ToString("f2") + "\n" + "Radius: " + cylinder.Radius.ToString("f2") +
                "\n" + "Perimeter: " + cylinder.Perimeter().ToString("f2") + "\n" +
                "Volume: " + cylinder.Volume().ToString("f2") + "\n" + "Area: " + cylinder.Area().ToString("f2") + "\n\n");
        }
        private void DisplayCount()
        {
            txtCylinderCount.Text = String.Format("Count: {0}", cylinderList.Count);
        }

        private void btnDisplayAllCylinders_Click(object sender, EventArgs e)
        {
            foreach (Cylinder cylinder in cylinderList)
            {
                richTextBox2.AppendText("Height: " + cylinder.Height.ToString("f2") + "\n" + "Radius: " + cylinder.Radius.ToString("f2") +
                "\n" + "Perimeter: " + cylinder.Perimeter().ToString("f2") + "\n" +
                "Volume: " + cylinder.Volume().ToString("f2") + "\n" + "Area: " + cylinder.Area().ToString("f2") + "\n\n");
            }
        }

    }
    public class Cylinder
    {
        private double _height;
        private double _radius;

        //constructor
        public Cylinder(double height, double radius)
        {
            _height = height;
            _radius = radius;
        }
        //properties

        public double Height
        { get { return _height; } }

        public double Radius
        { get { return _radius; } }

        //Methods

        public double Perimeter()
        {
            return 2 * Math.PI * _radius;
        }
        public double Volume()
        {
            return Math.PI * _radius * _radius * _height;
        }

        public double Area()
        {
            return Math.PI * _radius * _radius;
        }
    }
}
///Lab: 
///Define a class Cylinder
///define its private fields, constructor and properties
///define methods Volume and Area
///in Form1: Create a List of Cylinder
///Add button to create new cylinder and add to list
///Add label at the bottom corner to display the number of 
///cylinders added to the list
///Add button to display all the cylinders in the list 

No comments:

Post a Comment