執行檔
04.15-PM09:50修正: 屬性內set部分的錯誤
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.Threading.Tasks;
using System.Windows.Forms;
namespace Hw6
{
public partial class Form1 : Form
{
private Shape3D[] shapeArr=new Shape3D[100];
private static double[] densityArr = { 2.7, 7.87, 11.3 };
public Form1()
{
InitializeComponent();
cbox_ShapeSelect.SelectedIndex = 0;
cbox_MaterialSelect.SelectedIndex = 0;
}
private void cbox_ShapeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cbox_ShapeSelect.SelectedIndex)
{
case 0:
lbl_Para1.Text = "半徑";
lbl_Para2.Visible = false;
txt_Para2.Visible = false;
break;
case 1:
lbl_Para1.Text = "邊長";
lbl_Para2.Visible = false;
txt_Para2.Visible = false;
break;
case 2:
lbl_Para1.Text = "半徑";
lbl_Para2.Text = "高";
lbl_Para2.Visible = true;
txt_Para2.Visible = true;
break;
case 3:
lbl_Para1.Text = "邊長";
lbl_Para2.Text = "高";
lbl_Para2.Visible = true;
txt_Para2.Visible = true;
break;
default:
break;
}
}
private void btn_Add_Click(object sender, EventArgs e)
{
int amount = Shape3D.Amount;
double density = densityArr[cbox_MaterialSelect.SelectedIndex];
switch (cbox_ShapeSelect.SelectedIndex)
{
case 0:
shapeArr[amount] = new Ball(double.Parse(txt_Para1.Text), density);
break;
case 1:
shapeArr[amount] = new Cube(double.Parse(txt_Para1.Text), density);
break;
case 2:
shapeArr[amount] = new Cylinder(double.Parse(txt_Para1.Text), double.Parse(txt_Para2.Text),density);
break;
case 3:
shapeArr[amount] = new Pyramid(double.Parse(txt_Para1.Text), double.Parse(txt_Para2.Text), density);
break;
default:
break;
}
txt_Message.AppendText(shapeArr[amount].ShapeProperty()+Environment.NewLine);
txt_AmountOfShape.Text = Shape3D.Amount.ToString();
}
private void btn_Calculate_Click(object sender, EventArgs e)
{
for (int i = 0; i < Shape3D.Amount;i++ )
{
string str = (shapeArr[i].ShowVolumeWeight() + Environment.NewLine);
txt_Display.AppendText(str);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Shape3D
{
protected double density;
private static int amount = 0;
public Shape3D()
{
density = 0;
amount++;
}
public Shape3D(double d)
{
Density = d;
amount++;
}
public double Density
{
get { return density; }
set
{
if (value< 0)
density = 0;
else
density = value;
}
}
public static int Amount
{
get { return amount; }
}
public double Weight()
{
return Density * Volume();
}
//Virtual Method
public virtual double Volume()
{
return 0;
}
public string ShowVolumeWeight()
{
string str = ShapeProperty();
str += '\t';
str += string.Format("{0,8:F2}", density);
str += '\t';
str += string.Format("{0,8:F2}", Volume());
str += '\t';
str += string.Format("{0,8:F2}", Weight());
return str;
}
public virtual string ShapeProperty()
{
return "";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Ball : Shape3D
{
private double radius;
// private static double pi = 3.1415926;
public Ball(double radius, double density)
: base(density)
{
Radius = radius;
}
public double Radius
{
get { return radius; }
set
{
if (value < 0)
radius = 0;
else
radius = value;
}
}
public override double Volume()
{
return 4.0/3*GeoConstant.pi*radius*radius*radius;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Ball");
str += '\t';
str += string.Format("{0,8:F2}", radius);
str += '\t';
str += string.Format("{0,8}", "");
return str;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Cube : Shape3D
{
private double side;
public Cube(double side, double density)
: base(density)
{
Side = side;
}
public double Side
{
get { return side; }
set
{
if (value < 0)
side = 0;
else
side = value;
}
}
public override double Volume()
{
return side*side*side;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Cube");
str += '\t';
str += string.Format("{0,8:F2}", side);
str += '\t';
str += string.Format("{0,8}", "");
return str;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Cylinder : Shape3D
{
private double radius;
private double height;
// private static double pi = 3.1415926;
public Cylinder(double radius, double height,double density)
: base(density)
{
Radius = radius;
Height = height;
}
public double Radius
{
get { return radius; }
set
{
if (value < 0)
radius = 0;
else
radius = value;
}
}
public double Height
{
get { return height; }
set
{
if (value < 0)
height = 0;
else
height = value;
}
}
public override double Volume()
{
return GeoConstant.pi * radius * radius * height;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Cylinder");
str += '\t';
str += string.Format("{0,8:F2}", radius);
str += '\t';
str += string.Format("{0,8:F2}", height);
return str;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Pyramid : Shape3D
{
private double side;
private double height;
public Pyramid(double side, double height,double density)
: base(density)
{
Side = side;
Height = height;
}
public double Side
{
get { return side; }
set
{
if (value < 0)
side = 0;
else
side = value;
}
}
public double Height
{
get { return height; }
set
{
if (value < 0)
height = 0;
else
height = value;
}
}
public override double Volume()
{
return 1.0/3*side*side*height;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Pyramid");
str += '\t';
str += string.Format("{0,8:F2}", side);
str += '\t';
str += string.Format("{0,8:F2}", height);
return str;
}
}
}
沒有留言:
張貼留言