Thursday, April 10, 2014

Out Keyword with Method (Cylinder)

Q: Provide GUI to enter radius and height of a cylinder. Define a method "ProcessCylinder" that takes 2 input parameters: radius and height, both of type doubles and takes 3 output parameters: perimeter, area, and volume. In a button click, get user Input, call the method, then display the results.




namespace Out_Keyword_Ex
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void ProcessCylinder(double radius, double height, out double perimeter, out double area, out double volume)
        {
            perimeter = 2 * Math.PI * radius;
            area = Math.PI * radius * radius;
            volume = Math.PI * radius * radius * height;
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            double n1, n2;
            double P, A, V;
            if (double.TryParse(txtRadius.Text, out n1) && double.TryParse(txtHeight.Text, out n2))
            {
                ProcessCylinder(n1, n2, out P, out A, out V);
                txtDisplay.Text = "Perimeter = " + P.ToString("f2") + "\n" + "Area = " + A.ToString("f2") + "\n" + "Volume = " + V.ToString("f2");
            }
            else
            {
                MessageBox.Show("Format Exception: Invalid data");
            }
        }

    }
}

No comments:

Post a Comment