Java编程——求解几何图形的周长、面积的程序。

2020-06-02 教育 82阅读
//Dynamic.java
interface MyShape{
public double area();
public double circum();
}
class MyRectangle implements MyShape{
private double height;
private double width;
public MyRectangle(double height,double width){
this.height = height;
this.width = width;
}
public double area(){
return height * width;
}
public double circum(){
return 2 * (height + width);
}
}
class MyCircle implements MyShape{
private double radius;
public MyCircle(double radius){
this.radius = radius;
}
public double area(){
return Math.PI * radius * radius;
}
public double circum(){
return 2 * Math.PI * radius;
}
}
class MyTriangle implements MyShape{
private double a;
private double b;
private double c;
public MyTriangle(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
public double area(){
return Math.sqrt((a + b + c) * (a + b - c) * (a + c - b) *
(b + c - a)) / 4;
}
public double circum(){
return a + b + c;
}
}
public class Dynamic{
public static void main(String[] args){
MyShape myShape;
if(args.length == 1)
myShape = new MyCircle(Double.parseDouble(args[0]));
else if(args.length == 2)
myShape = new MyRectangle(Double.parseDouble(args[0]),
Double.parseDouble(args[1]));
else if(args.length == 3)
myShape = new MyTriangle(Double.parseDouble(args[0]),
Double.parseDouble(args[1]),Double.parseDouble(args[2]));
else{
System.out.println("运行出错,应该以1个或两个或三个的命令行参数" +
"来运行程序");
return;
}
System.out.println(myShape.area());
System.out.println(myShape.circum());
}
}
向楼上的很多set get 方法我都没写。
并且 我的三角形面积百分百准确,他那个不行
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com