求大神帮忙写一个java作业要求如图

 我来答
912330776a
2015-11-19
知道答主
回答量:32
采纳率:0%
帮助的人:2.9万
展开全部
创建父类,属性高,底面积 方法 体积为高乘底面积 然后创建各种子类! 重写方法
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
化神sunny
2015-11-19 · TA获得超过132个赞
知道小有建树答主
回答量:224
采纳率:10%
帮助的人:91.4万
展开全部
public class TestDemo {
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;

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友26e19c3db
推荐于2016-11-05 · 超过16用户采纳过TA的回答
知道答主
回答量:22
采纳率:0%
帮助的人:15.5万
展开全部

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());

}


}


输出如下:

追问
太给力了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式