Form1 為視窗主程式
ConstantTable 為抽象類別
定義圓周率.金屬密度.建立密度體積重量輸出方法.虛擬計算體積方法.虛擬形狀以及參數輸出方法
Shape 為父類別
產生靜態數量.形狀.材質
Ball.Cube.Cylinder.Pyramid 為子類別(繼承Shape)
定義各形狀之參數.建立體積計算方法.形狀以及參數輸出方法
Form1
using System;
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 Quiz5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cmbBoxMaterial.SelectedIndex = 0;
cmbBoxShape.SelectedIndex = 0;
txtBoxShapeMessage.Text += "形狀\t\t半徑or邊長\t高";
txtBoxShapeMessage.Text += Environment.NewLine;
txtBoxMessage.Text += "形狀\t\t半徑or邊長\t高\t密度\t體積\t重量";
txtBoxMessage.Text += Environment.NewLine;
}
Shape3D[] shapeArray = new Shape3D[66];
private void cmbBoxShape_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cmbBoxShape.SelectedIndex)
{
case 0:
lblParameter1.Text = "半徑";
CleanParameterInput();
ShowParameter2(false);
break;
case 1:
lblParameter1.Text = "邊長";
CleanParameterInput();
ShowParameter2(false);
break;
case 2:
lblParameter1.Text = "半徑";
lblParameter2.Text = "高";
CleanParameterInput();
ShowParameter2(true);
break;
case 3:
lblParameter1.Text = "邊長";
lblParameter2.Text = "高";
CleanParameterInput();
ShowParameter2(true);
break;
default:
break;
}
}
private void btnAddShape_Click(object sender, EventArgs e)
{
switch(cmbBoxShape.SelectedItem.ToString())
{
case "球":
shapeArray[Shape3D.Amount] = new Ball(cmbBoxMaterial.Text, Convert.ToDouble(txtBoxParameter1.Text));
break;
case "正方體":
shapeArray[Shape3D.Amount] = new Cube(cmbBoxMaterial.Text, Convert.ToDouble(txtBoxParameter1.Text));
break;
case "圓柱體":
shapeArray[Shape3D.Amount] = new Cylinder(cmbBoxMaterial.Text, Convert.ToDouble(txtBoxParameter1.Text), Convert.ToDouble(txtBoxParameter2.Text));
break;
case "金字塔":
shapeArray[Shape3D.Amount] = new Pyramid(cmbBoxMaterial.Text, Convert.ToDouble(txtBoxParameter1.Text), Convert.ToDouble(txtBoxParameter2.Text));
break;
default:
break;
}
txtBoxAmount.Text = Shape3D.Amount.ToString();
txtBoxShapeMessage.Text += shapeArray[Shape3D.Amount-1].ShowShapeAndParameters();
txtBoxShapeMessage.Text += Environment.NewLine;
}
private void btnCalculate_Click(object sender, EventArgs e)
{
for(int i = 0; i < Shape3D.Amount; i++)
{
txtBoxMessage.Text += shapeArray[i].ShowShapeAndParameters() + shapeArray[i].ShowDensityVolumeAndWeight();
}
}
public void CleanParameterInput()
{
txtBoxParameter1.Text = null;
txtBoxParameter2.Text = null;
}
public void ShowParameter2(bool check)
{
if(check)
{
lblParameter2.Visible = true;
txtBoxParameter2.Visible = true;
}
else
{
lblParameter2.Visible = false;
txtBoxParameter2.Visible = false;
}
}
}
}
ConstantTable
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz5
{
abstract class ConstantTable
{
public static double pi = 3.141592653589793238462433;
public static double GetDensity(string material)
{
double density;
switch (material)
{
case "鋁":
density = 2.7;
break;
case "鐵":
density = 7.87;
break;
case "鉛":
density = 11.3;
break;
default:
density = 0;
break;
}
return density;
}
}
}
Shape
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz5
{
class Shape3D
{
private static int amount = 0;
private string shape;
private string material;
public static int Amount
{
get { return amount; }
set
{
if (value < 0)
{
amount = 0;
}
else
{
amount = value;
}
}
}
public string Shape
{
get { return shape; }
set
{
shape = value;
}
}
public string Material
{
get { return material; }
set
{
material = value;
}
}
public Shape3D()
{
Material = "";
Amount++;
}
public Shape3D(string material)
{
Material = material;
Amount++;
}
public virtual double GetVolume()
{
return 0;
}
public double GetWeight()
{
return Math.Round(ConstantTable.GetDensity(Material) * GetVolume(), 2);
}
public string ShowDensityVolumeAndWeight()
{
string message = "";
message += ConstantTable.GetDensity(Material) + "\t" + GetVolume() + "\t" + GetWeight();
message += Environment.NewLine;
return message;
}
public virtual string ShowShapeAndParameters()
{
return "";
}
}
}
Ball
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz5
{
class Ball : Shape3D
{
private double radius;
public double Radius
{
get { return radius; }
set
{
if (value < 0)
{
radius = 0;
}
else
{
radius = value;
}
}
}
public Ball()
{
Shape = "Ball";
Radius = 0;
}
public Ball(string material, double radius)
: base(material)
{
Shape = "Ball";
Radius = radius;
}
public override double GetVolume()
{
return Math.Round(4.0 / 3 * ConstantTable.pi * Math.Pow(radius, 3), 2);
}
public override string ShowShapeAndParameters()
{
string message = "";
message += Shape + "\t\t" + Radius + "\t\t\t";
return message;
}
}
}
Cube
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz5
{
class Cube : Shape3D
{
private double sideLength;
public double SideLength
{
get { return sideLength; }
set
{
if (value < 0)
{
sideLength = 0;
}
else
{
sideLength = value;
}
}
}
public Cube()
{
Shape = "Cube";
SideLength = 0;
}
public Cube(string material, double sideLength)
: base(material)
{
Shape = "Cube";
SideLength = sideLength;
}
public override double GetVolume()
{
return Math.Round(Math.Pow(sideLength, 3), 2);
}
public override string ShowShapeAndParameters()
{
string message = "";
message += Shape + "\t\t" + SideLength + "\t\t\t";
return message;
}
}
}
Cylinder
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz5
{
class Cylinder : Shape3D
{
private double radius;
private double 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 Cylinder()
{
Shape = "Cylinder";
Radius = 0;
Height = 0;
}
public Cylinder(string material, double radius, double height)
: base(material)
{
Shape = "Cylinder";
Radius = radius;
Height = height;
}
public override double GetVolume()
{
return Math.Round(Height * ConstantTable.pi * Math.Pow(Radius, 2), 2);
}
public override string ShowShapeAndParameters()
{
string message = "";
message += Shape + "\t\t" + Radius + "\t\t" + Height + "\t";
return message;
}
}
}
Pyramid
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz5
{
class Pyramid : Shape3D
{
private double sideLength;
private double height;
public double SideLength
{
get { return sideLength; }
set
{
if (value < 0)
{
sideLength = 0;
}
else
{
sideLength = value;
}
}
}
public double Height
{
get { return height; }
set
{
if (value < 0)
{
height = 0;
}
else
{
height = value;
}
}
}
public Pyramid()
{
Shape = "Pyramid";
SideLength = 0;
Height = 0;
}
public Pyramid(string material, double sideLength, double height)
: base(material)
{
Shape = "Pyramid";
SideLength = sideLength;
Height = height;
}
public override double GetVolume()
{
return Math.Round(1.0 / 3 * height * Math.Pow(sideLength, 2), 2);
}
public override string ShowShapeAndParameters()
{
string message = "";
message += Shape + "\t\t" + SideLength + "\t\t" + Height + "\t";
return message;
}
}
}
沒有留言:
張貼留言