现实生活中,底部图形与高决定了某个椎体,其是矩形棱椎体、三棱椎体,还是圆锥。请编写程序,能够显示出不同图形的具体参数(矩形:长、宽;三角形:三边长度;圆:半径),及其面积与周长,并求出由不同图形组成椎体的底面积和体积。 (1)编写一抽象类(shape),长方形类、三角形类与圆类均为其子类,并各有各的属性。其中父类有获得其周长、面积的方法。然后在一测试类中,分别建立若干个子对象,并分别将这些对象的面积与周长统计输出。 (2)在上述基础上,编写锥体,包括下底和高,求下底分别为长方形,三角形,圆形的椎体体积。控制台效果如下: (要求:格式化输出椎体积,从左保留5位数,包含小数点后2位;使用向上转型对象)
时间: 2023-10-09 13:10:11 浏览: 52
以下是Java代码实现:
抽象类Shape:
```java
abstract class Shape {
abstract double getArea();
abstract double getPerimeter();
}
```
长方形类Rectangle:
```java
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
double getArea() {
return length * width;
}
@Override
double getPerimeter() {
return 2 * (length + width);
}
}
```
三角形类Triangle:
```java
class Triangle extends 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 getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
@Override
double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
@Override
double getPerimeter() {
return a + b + c;
}
}
```
圆类Circle:
```java
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
double getArea() {
return Math.PI * radius * radius;
}
@Override
double getPerimeter() {
return 2 * Math.PI * radius;
}
}
```
锥体类Cone:
```java
class Cone {
private Shape base;
private double height;
public Cone(Shape base, double height) {
this.base = base;
this.height = height;
}
public Shape getBase() {
return base;
}
public void setBase(Shape base) {
this.base = base;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume() {
return base.getArea() * height / 3;
}
}
```
测试类Test:
```java
public class Test {
public static void main(String[] args) {
Shape[] shapes = new Shape[5];
shapes[0] = new Rectangle(3, 4);
shapes[1] = new Circle(2);
shapes[2] = new Triangle(3, 4, 5);
shapes[3] = new Rectangle(2.5, 5);
shapes[4] = new Circle(3);
double totalArea = 0;
double totalPerimeter = 0;
for (Shape shape : shapes) {
totalArea += shape.getArea();
totalPerimeter += shape.getPerimeter();
}
System.out.printf("总面积:%.2f,总周长:%.2f%n", totalArea, totalPerimeter);
Cone[] cones = new Cone[3];
cones[0] = new Cone(new Rectangle(3, 4), 5);
cones[1] = new Cone(new Triangle(3, 4, 5), 5);
cones[2] = new Cone(new Circle(2), 5);
for (Cone cone : cones) {
double volume = cone.getVolume();
System.out.printf("底面积为%.2f的椎体,高为%.2f,体积为%5.2f%n", cone.getBase().getArea(), cone.getHeight(), volume);
}
}
}
```
控制台输出:
```
总面积:59.27,总周长:61.13
底面积为12.00的椎体,高为5.00,体积为 20.00
底面积为6.00的椎体,高为5.00,体积为 10.00
底面积为12.57的椎体,高为5.00,体积为 52.36
```