2015年5月7日 星期四

[2015][Homework]Team04 - Hw07

Form1.cs為視窗設計
Shape3D.cs為基礎類別
Ball.cs、Cube.cs、Cylinder.cs、Pyramid.cs為衍生類別
Constant.cs為紀錄運算需要用到的常數,例如pi
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace homework7
{
    public partial class Form1 : Form
    {
        private Shape3D[] shapeArr = new Shape3D[100];
        private static double[] densityArr = { 2.7, 7.87, 11.3 };      //density

        public Form1()      //initialize
        {
            InitializeComponent();
            cboMaterial.SelectedIndex = 0;
            cboShape.SelectedIndex = 0;
            txtNumbers.Text = Shape3D.Amount.ToString();
            txtParameter1.Text = "0";
        }

        private void cboShape_SelectedIndexChanged(object sender, EventArgs e)      //when shape chosen, show labels and text boxes
        {
            switch (cboShape.SelectedIndex)
            {
                case 0:      //ball
                    lblParameter1.Text = "半徑";
                    lblParameter2.Visible = false;
                    txtParameter2.Visible = false;
                    break;
                case 1:      //cube
                    lblParameter1.Text = "邊長";
                    lblParameter2.Visible = false;
                    txtParameter2.Visible = false;
                    break;
                case 2:      //cylinder
                    lblParameter1.Text = "半徑";
                    lblParameter2.Text = "高";
                    lblParameter2.Visible = true;
                    txtParameter2.Visible = true;
                    break;
                case 3:      //pyramid
                    lblParameter1.Text = "邊長";
                    lblParameter2.Text = "高";
                    lblParameter2.Visible = true;
                    txtParameter2.Visible = true;
                    break;
                default:
                    break;
            }
        }

        private void btnInput_Click(object sender, EventArgs e)      //new items
        {
            int amount = Shape3D.Amount;
            double density = densityArr[cboMaterial.SelectedIndex];      //density of chosen material

            switch (cboShape.SelectedIndex)
            {
                case 0:      //ball
                    shapeArr[amount] = new Ball(double.Parse(txtParameter1.Text), density);
                    break;
                case 1:      //cube
                    shapeArr[amount] = new Cube(double.Parse(txtParameter1.Text), density);
                    break;
                case 2:      //cylinder
                    shapeArr[amount] = new Cylinder(double.Parse(txtParameter1.Text), double.Parse(txtParameter2.Text), density);
                    break;
                case 3:      //pyramid
                    shapeArr[amount] = new Pyramid(double.Parse(txtParameter1.Text), double.Parse(txtParameter2.Text), density);
                    break;
            }
            txtDetails.AppendText(shapeArr[amount].ShapeProperty() + Environment.NewLine);      //show properties of shapes
            txtNumbers.Text = Shape3D.Amount.ToString();      //show amount
        }

        private void btnCalculate_Click(object sender, EventArgs e)      //calculate and show results
        {
            txtResults.Clear();
            for (int i = 0; i < Shape3D.Amount; i++)
            {
                string display = (shapeArr[i].ShowResults() + Environment.NewLine);
                txtResults.AppendText(display);
            }
        }
    }
}

Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework7
{
    class Shape3D
    {
        protected double density;      //fields
        private static int _amount = 0;

        public Shape3D()      //constructors
        {
            density = 0;
            _amount++;
        }

        public Shape3D(double d)
        {
            Density = d;
            _amount++;
        }

        public double Density      //attributes
        {
            get { return density; }
            set
            {
                if (density < 0)
                {
                    density = 0;
                }
                else
                {
                    density = value;
                }
            }
        }

        public static int Amount
        {
            get { return _amount; }
        }

        public double Weight()      //calculate weight
        {
            return Density * Volume();
        }

        public virtual double Volume()      //virtual method
        {
            return 0;
        }

        public string ShowResults()      //show results message, including properties of shapes, density, volume, and weight
        {
            string results = ShapeProperty();
            results += '\t';
            results += string.Format("{0,8:F2}", density);
            results += '\t';
            results += string.Format("{0,8:F2}", Volume());
            results += '\t';
            results += string.Format("{0,8:F2}", Weight());
            return results;
        }

        public virtual string ShapeProperty()      //virtual method
        {
            return "";
        }
    }
}

Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework7
{
    class Ball:Shape3D
    {
        private double _radius;      //fields

        public Ball(double _radius, double density)      //constructors
            :base(density)
        {
            Radius = _radius;
        }

        public double Radius      //attributes
        {
            get { return _radius; }
            set
            {
                if (_radius < 0)
                {
                    _radius = 0;
                }
                else
                {
                    _radius = value;
                }
            }
        }

        public override double Volume()      //calculate volume
        {
            return 4.0 / 3 * Constant.pi * _radius * _radius * _radius;
        }

        public override string ShapeProperty()      //show details of chosen shape, including shape and radius
        {
            string property = "Ball";
            property += '\t';
            property += string.Format("{0,8:F2}", _radius);
            property += '\t';
            property += "";
            return property;
        }
    }
}

Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework7
{
    class Cube:Shape3D
    {
        private double _length;      //fields

        public Cube(double _length, double density)      //constructors
            :base(density)
        {
            Length = _length;
        }

        public double Length      //attributes
        {
            get { return _length; }
            set
            {
                if (_length < 0)
                {
                    _length = 0;
                }
                else
                {
                    _length = value;
                }
            }
        }

        public override double Volume()      //calculate volume
        {
            return _length * _length * _length;
        }

        public override string ShapeProperty()      //show details of chosen shape, including shape and length
        {
            string property = "Cube";
            property += '\t';
            property += string.Format("{0,8:F2}", _length);
            property += '\t';
            property += "";
            return property;
        }
    }
}

Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework7
{
    class Cylinder:Shape3D
    {
        private double _radius;      //fields
        private double _height;

        public Cylinder(double _radius, double _height, double density)      //constructors
            : base(density)
        {
            Radius = _radius;
            Height = _height;
        }

        public double Radius      //attributes
        {
            get { return _radius; }
            set
            {
                if (_radius < 0)
                {
                    _radius = 0;
                }
                else
                {
                    _radius = value;
                }
            }
        }

        public double Height
        {
            get { return _height; }
            set
            {
                if (_height < 0)
                {
                    _height = 0;
                }
                else
                {
                    _height = value;
                }
            }
        }

        public override double Volume()      //calculate volume
        {
            return _radius * _radius * Constant.pi * _height;
        }

        public override string ShapeProperty()      //show details of chosen shape, including shape, radius, and height
        {
            string property = "Cylinder";
            property += '\t';
            property += string.Format("{0,8:F2}", _radius);
            property += '\t';
            property += string.Format("{0,8:F2}", _height);
            return property;
        }
    }
}

Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework7
{
    class Pyramid:Shape3D
    {
        private double _length;      //fields
        private double _height;

        public Pyramid(double _length, double _height, double density)      //constructors
            : base(density)
        {
            Length = _length;
            Height = _height;
        }

        public double Length      //attributes
        {
            get { return _length; }
            set
            {
                if (_length < 0)
                {
                    _length = 0;
                }
                else
                {
                    _length = value;
                }
            }
        }

        public double Height
        {
            get { return _height; }
            set
            {
                if (_height < 0)
                {
                    _height = 0;
                }
                else
                {
                    _height = value;
                }
            }
        }

        public override double Volume()      //calculate volume
        {
            return 1.0 / 3 * _length * _length * _height;
        }

        public override string ShapeProperty()      //show details of chosen shape, including shape, length, and height
        {
            string property = "Pyramid";
            property += '\t';
            property += string.Format("{0,8:F2}", _length);
            property += '\t';
            property += string.Format("{0,8:F2}", _height);
            return property;
        }
    }
}

Constant.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework7
{
    class Constant
    {
        public static double pi = 3.1415926;
    }
}

沒有留言:

張貼留言