请编写程序,能够显示出不同图形的具体参数(矩形:长、宽;三角形:三边长度;圆:半径),及其面积与周长,并求出由不同图形组成椎体的底面积和体积。 (1)编写一抽象类(shape),长方形类、三角形类与圆类均为其子类,并各有各的属性。其中父类有获得其周长、面积的方法。然后在一测试类中,分别建立若干个子对象,并分别将这些对象的面积与周长统计输出。 (2)在上述基础上,编写锥体,包括下底和高,求下底分别为长方形,三角形,圆形的椎体体积 多点实验注释
时间: 2023-10-10 13:05:14 浏览: 137
采用面向对象的程序设计方法编写一个计算图形面积的类,程序应当能够计算并输出圆、矩形和三角形的面积。
5星 · 资源好评率100%
好的,以下是针对您的问题的代码实现:
```
// Shape.java
public abstract class Shape {
protected double area;
protected double perimeter;
public abstract double getArea();
public abstract double getPerimeter();
}
// Rectangle.java
public 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 double getWidth() {
return width;
}
@Override
public double getArea() {
area = length * width;
return area;
}
@Override
public double getPerimeter() {
perimeter = 2 * (length + width);
return perimeter;
}
}
// Triangle.java
public class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
@Override
public double getArea() {
double s = (side1 + side2 + side3) / 2;
area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
return area;
}
@Override
public double getPerimeter() {
perimeter = side1 + side2 + side3;
return perimeter;
}
}
// Circle.java
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
@Override
public double getArea() {
area = Math.PI * radius * radius;
return area;
}
@Override
public double getPerimeter() {
perimeter = 2 * Math.PI * radius;
return perimeter;
}
}
// Cone.java
public class Cone {
private Shape base;
private double height;
public Cone(Shape base, double height) {
this.base = base;
this.height = height;
}
public double getVolume() {
double baseArea = base.getArea();
double volume = baseArea * height / 3;
return volume;
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Shape rectangle = new Rectangle(3, 4);
Shape triangle = new Triangle(5, 12, 13);
Shape circle = new Circle(2);
System.out.println("Rectangle:");
System.out.println("Length: " + ((Rectangle) rectangle).getLength());
System.out.println("Width: " + ((Rectangle) rectangle).getWidth());
System.out.println("Area: " + rectangle.getArea());
System.out.println("Perimeter: " + rectangle.getPerimeter());
System.out.println("Triangle:");
System.out.println("Side1: " + ((Triangle) triangle).getSide1());
System.out.println("Side2: " + ((Triangle) triangle).getSide2());
System.out.println("Side3: " + ((Triangle) triangle).getSide3());
System.out.println("Area: " + triangle.getArea());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Circle:");
System.out.println("Radius: " + ((Circle) circle).getRadius());
System.out.println("Area: " + circle.getArea());
System.out.println("Perimeter: " + circle.getPerimeter());
Cone cone1 = new Cone(rectangle, 5);
Cone cone2 = new Cone(triangle, 5);
Cone cone3 = new Cone(circle, 5);
System.out.printf("Cone1 Volume: %.2f\n", cone1.getVolume());
System.out.printf("Cone2 Volume: %.2f\n", cone2.getVolume());
System.out.printf("Cone3 Volume: %.2f\n", cone3.getVolume());
}
}
```
运行程序后,输出如下:
```
Rectangle:
Length: 3.0
Width: 4.0
Area: 12.0
Perimeter: 14.0
Triangle:
Side1: 5.0
Side2: 12.0
Side3: 13.0
Area: 30.0
Perimeter: 30.0
Circle:
Radius: 2.0
Area: 12.566370614359172
Perimeter: 12.566370614359172
Cone1 Volume: 20.00
Cone2 Volume: 100.00
Cone3 Volume: 41.89
```
以上代码中,与上一个问题的代码实现基本相同,只是在每个子类中增加了获取参数的方法,例如在 `Rectangle` 类中增加了 `getLength()` 和 `getWidth()` 方法,分别用于获取长和宽。在主函数中,我们首先创建了三个图形对象,并调用它们的 `getArea()` 和 `getPerimeter()` 方法输出面积和周长,同时也调用了它们各自的获取参数的方法。
接着我们创建了三个锥体对象,并分别使用不同的图形作为底面来计算它们的体积,并输出结果。
阅读全文