1.用JAVA语言写一个程序完成下面内容,并写出详细注释。 2.请创建一个抽象类RightPrism(构造方法为Shape类和double型高),以及抽象方法(体积公式calcVolume,侧面积公式calcLateralArea ),并运用面向对象的思想,实现圆柱体、三棱柱、正方体、长方体。 3.要求以 RightPrism xxx = new CircularCylinder ()这种多态的方式调用。
时间: 2023-06-03 09:05:36 浏览: 140
抽象类RightPrism的代码如下:
```java
public abstract class RightPrism extends Shape{
protected double height;
public RightPrism(double height){
this.height = height;
}
public abstract double calcVolume();
public abstract double calcLateralArea();
}
```
其中,构造方法有一个double类型的参数height,表示右棱柱的高度。还有两个抽象方法calcVolume用于计算体积,calcLateralArea用于计算侧面积。
接下来是圆柱体、三棱柱、正方体、长方体的代码。
CircularCylinder.java
```java
public class CircularCylinder extends RightPrism{
private double radius;
public CircularCylinder(double radius, double height){
super(height);
this.radius = radius;
}
@Override
public double calcVolume() {
return Math.PI * radius * radius * height;
}
@Override
public double calcLateralArea() {
return 2 * Math.PI * radius * height;
}
}
```
TriangularPrism.java
```java
public class TriangularPrism extends RightPrism{
private double a;
private double b;
private double c;
public TriangularPrism(double a, double b, double c, double height){
super(height);
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double calcVolume() {
double s = (a + b + c) / 2;
return height * Math.sqrt(s * (s-a) * (s-b) * (s-c));
}
@Override
public double calcLateralArea() {
return a * height + b * height + c * height;
}
}
```
Cube.java
```java
public class Cube extends RightPrism{
public Cube(double side){
super(side);
}
@Override
public double calcVolume() {
return height * height * height;
}
@Override
public double calcLateralArea() {
return 4 * height * height;
}
}
```
Cuboid.java
```java
public class Cuboid extends RightPrism{
private double a;
private double b;
public Cuboid(double a, double b, double height){
super(height);
this.a = a;
this.b = b;
}
@Override
public double calcVolume() {
return a * b * height;
}
@Override
public double calcLateralArea() {
return 2 * (a + b) * height;
}
}
```
以上是四个实现类的代码。可以看到,每个实现类都继承了抽象类RightPrism,并重写了calcVolume和calcLateralArea方法。
要求以 RightPrism xxx = new CircularCylinder()这种多态的方式调用,具体的代码如下:
```java
public static void main(String[] args) {
RightPrism p1 = new CircularCylinder(2, 4);
System.out.println("圆柱体的体积和侧面积:");
System.out.println("体积:" + p1.calcVolume());
System.out.println("侧面积:" + p1.calcLateralArea());
RightPrism p2 = new TriangularPrism(3, 4, 5, 6);
System.out.println("三棱柱的体积和侧面积:");
System.out.println("体积:" + p2.calcVolume());
System.out.println("侧面积:" + p2.calcLateralArea());
RightPrism p3 = new Cube(5);
System.out.println("正方体的体积和侧面积:");
System.out.println("体积:" + p3.calcVolume());
System.out.println("侧面积:" + p3.calcLateralArea());
RightPrism p4 = new Cuboid(3, 4, 5);
System.out.println("长方体的体积和侧面积:");
System.out.println("体积:" + p4.calcVolume());
System.out.println("侧面积:" + p4.calcLateralArea());
}
```
程序输出如下:
```
圆柱体的体积和侧面积:
体积:50.26548245743669
侧面积:25.132741228718345
三棱柱的体积和侧面积:
体积:32.863353450309965
侧面积:72.0
正方体的体积和侧面积:
体积:125.0
侧面积:100.0
长方体的体积和侧面积:
体积:60.0
侧面积:46.0
```
阅读全文