用java编写圆锥_geometric 1)编写一个Java应用程序, 1)该 中有3个类:Triangle、Circle和cone,分别用来 Develop 238万源代码下载- www.pudn....
时间: 2023-06-25 20:02:14 浏览: 193
包含圆类的程序(含几种构造方法)java语言实现
以下是一个简单的Java应用程序,包括三个类Triangle、Circle和Cone,用于计算圆锥的体积。
Triangle.java
```
public class Triangle {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
public double getArea() {
return 0.5 * base * height;
}
}
```
Circle.java
```
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
```
Cone.java
```
public class Cone {
private Circle base;
private Triangle side;
public Cone(Circle base, Triangle side) {
this.base = base;
this.side = side;
}
public double getVolume() {
return (1.0/3.0) * base.getArea() * side.getArea();
}
}
```
使用这些类来计算圆锥的体积,您可以按照下面的代码进行操作:
```
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5.0);
Triangle triangle = new Triangle(10.0, 8.0);
Cone cone = new Cone(circle, triangle);
double volume = cone.getVolume();
System.out.println("The volume of the cone is: " + volume);
}
}
```
在这个例子中,我们创建了一个半径为5的圆和一个底部为这个圆的三角形,然后将它们传递给圆锥类来计算体积。输出将是:
```
The volume of the cone is: 261.7993877991494
```
阅读全文