Monday, May 5, 2014

Class With Methods (Cylinder) Update


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)
        {
            if (cylinderList.Count > 0)
            {
            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");
            }
            }
            else
                MessageBox.Show("Sorry. You didn't create a cylinder yet.");
        }

        //Cylinder with lowest volume
        private void btnCylinderLowestVolume_Click(object sender, EventArgs e)
        {
            if (cylinderList.Count > 0)
            {
                Cylinder cylinder = GetMinVolumeCylinder();
                //display it
                richTextBox3.Text = "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";
            }
            else
                MessageBox.Show("Sorry. You didn't create a cylinder yet.");
        }
        private Cylinder GetMinVolumeCylinder()
        {
            Cylinder minVolCylinder = cylinderList[0];
            foreach (Cylinder cylinder in cylinderList)
            {
                if (cylinder.Volume() < minVolCylinder.Volume())
                    minVolCylinder = cylinder;
            }
            return minVolCylinder;
        }

        //Cylinder with lowest area
        private void btnCylinderLowestArea_Click(object sender, EventArgs e)
        {
            if (cylinderList.Count > 0)
            {
            Cylinder cylinder = GetMinAreaCylinder();
            //display it
            richTextBox3.Text = "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";
            }
            else
                MessageBox.Show("Sorry. You didn't create a cylinder yet.");
        }
        private Cylinder GetMinAreaCylinder()
        {
            Cylinder minAreaCylinder = cylinderList[0];
            foreach (Cylinder cylinder in cylinderList)
            {
                if (cylinder.Area() < minAreaCylinder.Area())
                    minAreaCylinder = cylinder;
            }
            return minAreaCylinder;
        }

        private void btnCylinderLowestRadius_Click(object sender, EventArgs e)
        {
            if (cylinderList.Count > 0)
            {
            Cylinder cylinder = GetMinRadiusCylinder();
            richTextBox3.Text = "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";
            }
            else
                MessageBox.Show("Sorry. You didn't create a cylinder yet.");
        }
        private Cylinder GetMinRadiusCylinder()
        {
            Cylinder minRadiusCylinder = cylinderList[0];
            foreach (Cylinder cylinder in cylinderList)
            {
                if (cylinder.Radius < minRadiusCylinder.Radius)
                    minRadiusCylinder = cylinder;
            }
            return minRadiusCylinder;
        }

        private void btnGetBiggerCylinders_Click(object sender, EventArgs e)
        {
             if (cylinderList.Count > 0)
            {
                List<Cylinder> BiggerCylinders = GetBigCylinders();
                richTextBox4.Clear();
                 richTextBox4.AppendText("Average Volume: " + GetAverageVolume().ToString("f2") + "\n\n");
                foreach (Cylinder cylinder in BiggerCylinders)
                {
                    richTextBox4.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");
                }
            }
             else
                 MessageBox.Show("Sorry. You didn't create a cylinder yet.");
        }

        //method to return the average volume of all the cylinders
        private double GetAverageVolume()
        {
            double sum = 0;
            foreach (Cylinder cylinder in cylinderList)
            {
                sum += cylinder.Volume();
            }
            double avg = sum / cylinderList.Count;
            return avg;
        }
     
        //method to return cylinders bigger than average
        private List<Cylinder> GetBigCylinders()
        {

            double avg = GetAverageVolume();
            List<Cylinder> temCylinderList = new List<Cylinder>();
            foreach (Cylinder cylinder in cylinderList)
            {
                if (cylinder.Volume() > avg)
                    temCylinderList.Add(cylinder);
            }
            return temCylinderList;
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
    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 
///
///May 2nd, Lab Assignment
///Add the following to your Cylinder class project:
///Methods to:
///         Get the cylinder with the lowest volume
///         Get the cylinder with the lowest area
///         Get the cylinder with the lowest radius
///         Add buttons to call these methods and display their results
///
///Methods to:
///         - Compute the average volume of all cylinders
///         - return a list of all the cylinders whose volume
///          is more than the average volume
///          hint: syntax:
///          private List<Cylinder> GetBigCylinders()
///          {
///           ....need to create a temp list, populate it
///               with big cylinders
///           }
///         -Add button to call the method (GetBigCylinders) and
///          displays all big cylinders

No comments:

Post a Comment