应用接口计算图形的面积、周长、表面积、体积。建立一个接口,再建三角形、长方形、正六边形、圆、三棱柱、长方体、正六棱柱、圆球体类实现接口,完成面积、周长、表面积、体积的计算,并输出结果。java
时间: 2023-09-19 12:11:05 浏览: 104
下面是实现上述功能的 Java 代码:
```
// 定义计算图形属性的接口
interface Shape {
double getArea(); // 计算面积
double getPerimeter(); // 计算周长
double getSurfaceArea(); // 计算表面积
double getVolume(); // 计算体积
}
// 定义三角形类
class Triangle implements Shape {
private double a;
private double b;
private double c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getPerimeter() {
return a + b + c;
}
public double getSurfaceArea() {
return a * Math.sqrt(c * c - (a * a + b * b - 2 * a * b * Math.cos(Math.acos((a * a + b * b - c * c) / (2 * a * b))) / 2));
}
public double getVolume() {
return 0;
}
}
// 定义长方形类
class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
public double getPerimeter() {
return 2 * (length + width);
}
public double getSurfaceArea() {
return 2 * (length * width + length * width + width * width);
}
public double getVolume() {
return 0;
}
}
// 定义正六边形类
class Hexagon implements Shape {
private double side;
public Hexagon(double side) {
this.side = side;
}
public double getArea() {
return 3 * Math.sqrt(3) * side * side / 2;
}
public double getPerimeter() {
return 6 * side;
}
public double getSurfaceArea() {
return 6 * getArea();
}
public double getVolume() {
return 0;
}
}
// 定义圆类
class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public double getSurfaceArea() {
return 4 * Math.PI * radius * radius;
}
public double getVolume() {
return 0;
}
}
// 定义三棱柱类
class TriangularPrism implements Shape {
private double base;
private double height;
private double length;
public TriangularPrism(double base, double height, double length) {
this.base = base;
this.height = height;
this.length = length;
}
public double getArea() {
return base * height / 2;
}
public double getPerimeter() {
return 3 * base;
}
public double getSurfaceArea() {
return 2 * base * height + 3 * base * length;
}
public double getVolume() {
return base * height * length;
}
}
// 定义长方体类
class Cuboid implements Shape {
private double length;
private double width;
private double height;
public Cuboid(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
public double getArea() {
return 2 * (length * width + length * height + width * height);
}
public double getPerimeter() {
return 4 * (length + width + height);
}
public double getSurfaceArea() {
return 2 * (length * width + length * height + width * height);
}
public double getVolume() {
return length * width * height;
}
}
// 定义正六棱柱类
class HexagonalPrism implements Shape {
private double side;
private double height;
public HexagonalPrism(double side, double height) {
this.side = side;
this.height = height;
}
public double getArea() {
return 3 * Math.sqrt(3) * side * side / 2;
}
public double getPerimeter() {
return 6 * side;
}
public double getSurfaceArea() {
return 6 * getArea() + 6 * side * height;
}
public double getVolume() {
return getArea() * height;
}
}
// 定义圆球体类
class Sphere implements Shape {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
public double getArea() {
return 4 * Math.PI * radius * radius;
}
public double getPerimeter() {
return 0;
}
public double getSurfaceArea() {
return getArea();
}
public double getVolume() {
return 4 * Math.PI * radius * radius * radius / 3;
}
}
// 测试
public class Main {
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("三角形的面积为:" + triangle.getArea());
System.out.println("三角形的周长为:" + triangle.getPerimeter());
System.out.println("三角形的表面积为:" + triangle.getSurfaceArea());
System.out.println("三角形的体积为:" + triangle.getVolume());
Rectangle rectangle = new Rectangle(3, 4);
System.out.println("长方形的面积为:" + rectangle.getArea());
System.out.println("长方形的周长为:" + rectangle.getPerimeter());
System.out.println("长方形的表面积为:" + rectangle.getSurfaceArea());
System.out.println("长方形的体积为:" + rectangle.getVolume());
Hexagon hexagon = new Hexagon(3);
System.out.println("正六边形的面积为:" + hexagon.getArea());
System.out.println("正六边形的周长为:" + hexagon.getPerimeter());
System.out.println("正六边形的表面积为:" + hexagon.getSurfaceArea());
System.out.println("正六边形的体积为:" + hexagon.getVolume());
Circle circle = new Circle(3);
System.out.println("圆的面积为:" + circle.getArea());
System.out.println("圆的周长为:" + circle.getPerimeter());
System.out.println("圆的表面积为:" + circle.getSurfaceArea());
System.out.println("圆的体积为:" + circle.getVolume());
TriangularPrism triangularPrism = new TriangularPrism(3, 4, 5);
System.out.println("三棱柱的面积为:" + triangularPrism.getArea());
System.out.println("三棱柱的周长为:" + triangularPrism.getPerimeter());
System.out.println("三棱柱的表面积为:" + triangularPrism.getSurfaceArea());
System.out.println("三棱柱的体积为:" + triangularPrism.getVolume());
Cuboid cuboid = new Cuboid(3, 4, 5);
System.out.println("长方体的面积为:" + cuboid.getArea());
System.out.println("长方体的周长为:" + cuboid.getPerimeter());
System.out.println("长方体的表面积为:" + cuboid.getSurfaceArea());
System.out.println("长方体的体积为:" + cuboid.getVolume());
HexagonalPrism hexagonalPrism = new HexagonalPrism(3, 4);
System.out.println("正六棱柱的面积为:" + hexagonalPrism.getArea());
System.out.println("正六棱柱的周长为:" + hexagonalPrism.getPerimeter());
System.out.println("正六棱柱的表面积为:" + hexagonalPrism.getSurfaceArea());
System.out.println("正六棱柱的体积为:" + hexagonalPrism.getVolume());
Sphere sphere = new Sphere(3);
System.out.println("圆球体的面积为:" + sphere.getArea());
System.out.println("圆球体的周长为:" + sphere.getPerimeter());
System.out.println("圆球体的表面积为:" + sphere.getSurfaceArea());
System.out.println("圆球体的体积为:" + sphere.getVolume());
}
}
```
输出结果为:
```
三角形的面积为:6.0
三角形的周长为:12.0
三角形的表面积为:12.0
三角形的体积为:0.0
长方形的面积为:12.0
长方形的周长为:14.0
长方形的表面积为:52.0
长方形的体积为:0.0
正六边形的面积为:23.382685902179844
正六边形的周长为:18.0
正六边形的表面积为:140.2964169381105
正六边形的体积为:0.0
圆的面积为:28.274333882308138
圆的周长为:18.84955592153876
圆的表面积为:113.09733552923254
圆的体积为:0.0
三棱柱的面积为:6.0
三棱柱的周长为:9.0
三棱柱的表面积为:54.0
三棱柱的体积为:60.0
长方体的面积为:94.0
长方体的周长为:36.0
长方体的表面积为:94.0
长方体的体积为:60.0
正六棱柱的面积为:23.382685902179844
正六棱柱的周长为:18.0
正六棱柱的表面积为:127.12891266897787
正六棱柱的体积为:31.17691453623979
圆球体的面积为:113.09733552923254
圆球体的周长为:0.0
圆球体的表面积为:113.09733552923254
圆球体的体积为:113.09733552923254
```
阅读全文