求大神帮忙写一个java作业要求如图
public static void main(String[] args) {
Circle circle = new Circle(10);
Rect rect = new Rect(20,10);
Zhuti zhuti1 = new Zhuti(circle,20);
Zhuti zhuti2 = new Zhuti(rect,25);
System.out.println("圆柱的体积:"+zhuti1.getZhutiArea());
System.out.println("立方体的体积:"+zhuti2.getZhutiArea());
}
}
interface Area{
public double getArea();
}
class Circle implements Area{
private double r;
public Circle(double r){
this.r = r;
}
public double getArea(){
return 3.14*r*r;
}
}
class Rect implements Area{
private double height;
private double width;
public Rect(double height,double width){
this.height = height;
this.width = width;
}
public double getArea(){
return height*width;
}
}
class Zhuti {
private Area bottom;
private double height;
public Zhuti(Area bottom, double height){
this.bottom = bottom;
this.height = height;
}
public double getZhutiArea(){
return bottom.getArea()*height;
}
}
package cn.zgn.test;
abstract class ZhuTi{
protected float height ;
//计算底面积的抽象方法,具体怎么计算在其具体的子类中实现
public abstract float calculationFloorArea();
//计算体积的方法,通用:底面积 x 高
protected float calculationVolume(){
return calculationFloorArea()* height;
}
}
/**
* 圆柱体
* @author ning
*
*/
class YuanZhu extends ZhuTi {
private float radius ;
public static final float PI = 3.1415926f ;
public YuanZhu(float radius , float height) {
this.height = height ;
this.radius = radius ;
}
@Override
public float calculationFloorArea() {
return PI*radius*radius;
}
}
//底面为三角形的柱体
class SanJiaoZhu extends ZhuTi{
private float bottom_length ;
private float bottom_height;
public SanJiaoZhu(float bottom_length ,float bottom_height , float height) {
this.bottom_length = bottom_length;
this.bottom_height = bottom_height ;
this.height = height ;
}
@Override
public float calculationFloorArea() {
return (bottom_length * bottom_height) / 2 ;
}
}
//底面为矩形的柱体
class ChangFangTi extends ZhuTi{
private float bottom_length ;
private float bottom_height;
public ChangFangTi(float bottom_length ,float bottom_height , float height) {
this.bottom_length = bottom_length;
this.bottom_height = bottom_height ;
this.height = height ;
}
@Override
public float calculationFloorArea() {
return bottom_length * bottom_height ;
}
}
public class Test {
public static void main(String[] args) {
ZhuTi yuanZhu = new YuanZhu(2.5f, 5.7f);
System.out.println(yuanZhu.calculationVolume());
SanJiaoZhu sanJiaoZhu = new SanJiaoZhu(56.4f, 85.2f , 56.5f);
System.out.println(sanJiaoZhu.calculationFloorArea());
ChangFangTi changFangTi = new ChangFangTi(26.6f, 23.5f, 45.3f);
System.out.println(changFangTi.calculationFloorArea());
}
}
输出如下:
太给力了