class triangle {
double a;
double b;
double c;
public triangle(double a, double b, double c) {
if ((a + b > c) && (a + c > b) && (b + c > a) && (a - c < b)
&& (a - b < c) && (b - c < a)) {
this.a = a;
this.b = b;
this.c = c;
} else {
System.out.println("输入边长有错");
}
}
public double GetCircle() {
return (a + b + c);
}
public double GetArea() {
double p;
p = (a + b + c) / 2.0;
return (Math.sqrt(p * (p - a) * (p - b) * (p - c)));
}
class Tprism extends triangle {
double height;
public Tprism(double a, double b, double c, double height) {
super(a, b, c);
if ((a + b > c) && (a + c > b) && (b + c > a) && (a - c < b)
&& (a - b < c) && (b - c < a)) {
this.a = a;
this.b = b;
this.c = c;
this.height = height;
} else {
System.out.println("输入边长有错");
}
}
}
public static void main(String[] args) {
triangle r = new triangle(3.0, 4.0, 5.0);
triangle.Tprism c=r.new Tprism(3.0,4.0,5.0,6.0);
System.out.println("三角形的周长" + r.GetCircle());
System.out.println("三角形的面积" + r.GetArea());
System.out.println("三棱柱的底面积" + c.GetCircle());
System.out.println("三棱柱的底周长" + c.GetArea());
System.out.println("三棱柱的体积" + c.GetArea() * c.height);
}
}
第一道题的代码可以用内部类调用完成