共回答了21个问题采纳率:90.5%
/**
*柱体
*/
interface Column {
double PI = Math.PI;
double getArea();// 求表面积
double getVolume();// 求得柱体的体积
void display();// 将柱体的表面积和体积输出
}
/**
* @author Administrator
* 圆柱体
*/
class Cylinder implements Column {
private double radius;
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double getArea() {
return 2 * PI * radius * (radius + height);
}
@Override
public double getVolume() {
return PI * Math.pow(radius, 2) * height;
}
@Override
public void display() {
System.out.println("%%%圆柱体:n表面积= " + getArea() + ",体积=" + getVolume());
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
/**
* @author Administrator
* 立方体
*/
class Cube implements Column {
private double sideLength;
public Cube(double sideLength) {
this.sideLength = sideLength;
}
@Override
public double getArea() {
return 6*Math.pow(sideLength, 2);
}
@Override
public double getVolume() {
return Math.pow(sideLength, 3);
}
@Override
public void display() {
System.out.println("%%%立方体:n表面积= " + getArea() + ",体积=" + getVolume());
}
public double getSideLength() {
return sideLength;
}
public void setSideLength(double sideLength) {
this.sideLength = sideLength;
}
}
/**
* @author Administrator
* 球体
*/
class Ball implements Column {
private double radius;
public Ball(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return 4*PI*Math.pow(radius,2);
}
@Override
public double getVolume() {
return 4.0/3*PI*Math.pow(radius,3);
}
@Override
public void display() {
System.out.println("%%%球体:n表面积= " + getArea() + ",体积=" + getVolume());
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
/**
* @author Administrator
* 测试类
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Column columns[] = { new Cylinder(1, 3), new Cube(1), new Ball(2) };
for (Column column : columns) {
column.display();
}
}
}
1年前
2