JAVA设计一个立方体类Cube,只有边长属性,具有设置边长,获取边长,计算表面积,计算体积的方法。创建Cube对象,为其设置新边长,显示其边长,计算并显示其表面积和体积。;
时间: 2024-05-14 13:14:27 浏览: 151
JAVA考试填空题
```java
public class Cube {
private double edgeLength;
public void setEdgeLength(double edgeLength) {
this.edgeLength = edgeLength;
}
public double getEdgeLength() {
return edgeLength;
}
public double getSurfaceArea() {
return 6 * edgeLength * edgeLength;
}
public double getVolume() {
return edgeLength * edgeLength * edgeLength;
}
}
public class Main {
public static void main(String[] args) {
Cube cube = new Cube();
cube.setEdgeLength(5.0);
System.out.println("Edge Length: " + cube.getEdgeLength());
System.out.println("Surface Area: " + cube.getSurfaceArea());
System.out.println("Volume: " + cube.getVolume());
}
}
```
阅读全文