2014年4月13日 星期日

[Code Review] Team 07 - Hw06

檔案下載




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {        
        private int amount = 0;
        private double density;
        private string[] str = null;
        Sphere[] S;
        Cube[] C;
        Pyramid[] P;
        Cylinder[] CL;
        
        public Form1()
        {
            InitializeComponent();
            str = new string[10];
            S = new Sphere[10];
            C = new Cube[10];
            P = new Pyramid[10];
            CL = new Cylinder[10];
            TxtBox_2.Visible = false;
            TxtBox_3.Visible = false;
            Lbl_1.Visible = false;
            Lbl_2.Visible = false;
        }

        private void Btn_1_Click(object sender, EventArgs e)
        {
            while (amount == 10)
            {
                MessageBox.Show("無法再新增囉!");
                return;
            }
            switch (CmbBox_1.SelectedIndex)
            {
                case 0:
                    density = 8.92;
                    str[amount] +="(銅)  ";
                    break;
                case 1:
                    density = 7.87;
                    str[amount] +="(鐵)  ";
                    break;
                case 2:
                    density = 11.34;
                    str[amount] +="(鉛)  ";
                    break;
            }
            //************將創造出來之不同外型物件,放至於所屬形狀之陣列*****************
            switch (CmbBox_2.SelectedIndex)
            {
                case 0:
                    S[amount] = Sphere.Create(double.Parse(TxtBox_2.Text), double.Parse(TxtBox_3.Text), density);
                    str[amount] += " 球 \t" + TxtBox_2.Text + "cm\t" + "\t";
                    break;
                case 1:
                    C[amount] = Cube.Create(double.Parse(TxtBox_2.Text), double.Parse(TxtBox_3.Text), density);
                    str[amount] += "正方體\t" + TxtBox_2.Text + "cm\t" + "\t";
                    break;
                case 2:
                    P[amount] = Pyramid.Create(double.Parse(TxtBox_2.Text), double.Parse(TxtBox_3.Text), density);
                    str[amount] += "金字塔" + "\t" + TxtBox_2.Text + "cm\t" + TxtBox_3.Text + "cm";
                    break;
                case 3:
                    CL[amount] = Cylinder.Create(double.Parse(TxtBox_2.Text), double.Parse(TxtBox_3.Text), density);
                    str[amount] += "圓柱體" + "\t" + TxtBox_2.Text + "cm\t" + TxtBox_3.Text + "cm";                    
                    break;
            }            
            TxtBox_1.AppendText(str[amount] + "\n");
            amount++;
            TxtBox_4.Text = amount.ToString();
        }

        private void Btn_2_Click(object sender, EventArgs e)
        {
            //***********各外型類別物件創造出來時,都已具有專屬屬性,再經由本身Mass()方法計算出質量;當掃描至某矩陣位置具有存放記憶體時,再輸出其資料***************
            for (int i = 0; i < amount; i++)
            {
                while (S[i] != null)
                {
                    TxtBox_5.AppendText(str[i]+ S[i].Density + "g/cm^3\t" + S[i].Mass().ToString("0.00") + "g\n");                    
                    S[i] = null;
                    break;
                }
                while (C[i] != null)
                {
                    TxtBox_5.AppendText(str[i]+ C[i].Density + "g/cm^3\t" + C[i].Mass().ToString("0.00") + "g\n");
                    C[i] = null;
                    break;
                }
                while (P[i] != null)
                {
                    TxtBox_5.AppendText(str[i] + "\t" + P[i].Density + "g/cm^3\t" + P[i].Mass().ToString("0.00") + "g\n");
                    P[i] = null;
                    break;
                }
                while (CL[i] != null)
                {
                    TxtBox_5.AppendText(str[i] + "\t" + CL[i].Density + "g/cm^3\t" + CL[i].Mass().ToString("0.00") + "g\n");
                    CL[i] = null;
                    break;
                }
                str[i] = "";
            }
            
        }

        private void CmbBox_2_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (CmbBox_2.SelectedIndex)
            {
                case 0:
                    Lbl_1.Text = "半徑:";
                    TxtBox_2.Visible = true;
                    TxtBox_3.Visible = false;
                    Lbl_1.Visible = true;
                    Lbl_2.Visible = false;
                    break;
                case 1:
                    Lbl_1.Text = "邊長:";
                    TxtBox_2.Visible = true;
                    TxtBox_3.Visible = false;
                    Lbl_1.Visible = true;
                    Lbl_2.Visible = false;
                    break;
                case 2:
                    Lbl_1.Text = "邊長:";
                    Lbl_2.Text = "高:";
                    TxtBox_2.Visible = true;
                    TxtBox_3.Visible = true;
                    Lbl_1.Visible = true;
                    Lbl_2.Visible = true;
                    break;
                case 3:
                    Lbl_1.Text = "邊長:";
                    Lbl_2.Text = "高:";
                    TxtBox_2.Visible = true;
                    TxtBox_3.Visible = true;
                    Lbl_1.Visible = true;
                    Lbl_2.Visible = true;
                    break;
            }
        }

        private void Btn_3_Click(object sender, EventArgs e)
        {
            TxtBox_1.Clear();
            TxtBox_5.Clear();
            amount = 0;
            TxtBox_4.Text = amount.ToString();
        }

        

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication6
{
   public class Modle
    {
        //**********************Modle建構子*******************
        public double pi = 3.14159; //圓周率
        protected double length_1;
        protected double length_2;
        protected double density;        

        public Modle(double L1, double L2, double density)
        {
            this.length_1 = L1;
            this.length_2 = L2;
            this.density = density;
        }
        //**********************Modle_Length_1屬性*******************
        public double Length_1
        {
            get
            { return length_1; }
            set
            {
                if (value < 0)
                    length_1 = 0;
                else
                {
                    length_1 = value;
                }
            }
        }
        //**********************Modle_Length_2屬性*******************
        public double Length_2
        {
            get
            { return length_2; }
            set
            {
                if (value > 0)
                    length_2 = value;
                else
                {
                    length_2 = 0;
                }
            }
        }
        //**********************Modle_Density屬性*******************
        public double Density
        {
            get
            { return density; }
            set
            {
                if (value > 0)
                    density = value;
                else
                {
                    density = 0;
                }
            }
        }            
        }
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication6
{
    //**********************繼承Modle建構子*******************
    public class Sphere : Modle
    {
        private double mass;
        public Sphere(double radius, double high, double density)
            : base(radius, high, density)
        {
        }
        //**********************建造Sphere實體********************
        public static Sphere Create(double radius, double high, double density)
        {
            Sphere S = null;
            if (radius <= 0 || density <= 0)
            {
                return S;
            }
            else
            {
                S = new Sphere(radius, high, density);
                return S;
            }
        }
        //**********************計算Sphere質量*******************
        public double Mass()
        {
            return mass = (Length_1 * Length_1 * Length_1 * pi * density * 4 / 3);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication6
{
    public class Cube : Modle
    {
        //**********************繼承Modle建構子*******************
        private double mass;
        public Cube(double radius, double high, double density)
            : base(radius, high, density)
        {
        }
        //**********************建造Cube實體********************
        public static Cube Create(double radius, double high, double density)
        {
            Cube C = null;
            if (radius <= 0 || density <= 0)
            {
                return C;
            }
            else
            {
                C = new Cube(radius, high, density);
                return C;
            }
        }
        //**********************計算Cube質量*******************
        public double Mass()
        {
            return mass = (Length_1 * Length_1 * Length_1 * density);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication6
{
    public class Pyramid : Modle
    {
        //**********************繼承Modle建構子*******************
        private double mass;
        public Pyramid(double radius, double high, double density)
            : base(radius, high, density)
        {
        }
        //**********************建造Pyramid實體********************
        public static Pyramid Create(double radius, double high, double density)
        {
            Pyramid P = null;
            if (radius <= 0 || density <= 0)
            {
                return P;
            }
            else
            {
                P = new Pyramid(radius, high, density);
                return P;
            }
        }
        //**********************計算Pyramid質量*******************
        public double Mass()
        {
            return mass = (Length_1 * Length_1 * Length_2 * density / 3);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication6
{
    public class Cylinder : Modle
    {
        //**********************繼承Modle建構子*******************
        private double mass;
        public Cylinder(double radius, double high, double density)
            : base(radius, high, density)
        {
        }
        //**********************建造Cylinder實體********************
        public static Cylinder Create(double radius, double high, double density)
        {
            Cylinder CL = null;
            if (radius <= 0 || density <= 0)
            {
                return CL;
            }
            else
            {
                CL = new Cylinder(radius, high, density);
                return CL;
            }
        }
        //**********************計算Cylinder質量*******************
        public double Mass()
        {
            return mass = (Length_1 * Length_1 * pi * density * Length_2);
        }
    }
}

5 則留言:

  1. 第8組:
    amount不是應該寫在class裡面嗎 ? XD
    因為這樣才會是創建的時候加1,而不是按按鈕的時候+1,雖然結果一樣但想法上不太一樣。
    然後老師課程有提到可以使用父類別的陣列去存取子類別的內容,這樣的話就不用用三個矩陣去存取各圖形了(WEEK8)

    回覆刪除
  2. 還是amount造成的bug,由於在amount++的時候沒有檢查物件到底有沒有成功建出來,所以就算建立失敗也會累加1,而導致與實際物件數量不符,只能說是按鈕按了幾次。

    回覆刪除
  3. 林高遠評論:

    優:
    1. 這邊對物件Visible的控制寫得比別組好,可以整塊移動

    缺:
    1. 這邊把物件數量限制都寫死,不能在runtime修改
    2. 每一種物件都可以用父類別的參考統一管理,不用各自為政,這樣才有發揮「繼承」和「多形」的價值
    3. Modle是你的基礎類別,所以把length_1和length_2放這裡不好,因為那不是每個繼承Modle的類別都有的性質
    4. 衍生類別建構子傳了很多參數進去,但是完全沒動到

    回覆刪除
  4. 體積和質量綁死了,這樣沒辦法直接得到體積資訊,建議分開寫。

    根據你的寫法 Modle的建構子可以以多型的方式處理,這樣就不用傳入用不到的參數了。

    回覆刪除
  5. ==第七組回覆==
    感謝大家的回覆,我依大家的建議重新撰寫程式,若還有問題我會再修改,謝謝!

    回覆刪除