import java.util.Scanner;
//声明类Demo01
public class Demo01 {
//主方法,为程序的入口,运行程序时自动执行main方法
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
//每行读入三个数
double l = sc.nextDouble();
double h = sc.nextDouble();
double z = sc.nextDouble();
//构造长方体对象
Cubic cubic = new Cubic(l,h,z);
//构造四棱锥对象
Pyramid pyramid = new Pyramid(l,h,z);
//格式化数据
String area1 = String.format("%.2f",cubic.area());
String volumn1 = String.format("%.2f",cubic.volumn());
String area2 = String.format("%.2f",pyramid.area());
String volumn2 = String.format("%.2f",pyramid.volumn());
System.out.println(area1+" "+volumn1+" "+area2+" "+volumn2);
}
}
}
//父类
class Rect{
double l;
double h;
double z;
public Rect(double l,double h,double z){
if(l>0&&h>0&&z>0){
this.l = l;
this.h = h;
this.z = z;
}
}
//计算长方形底面积周长
public double length(){
return 2*(l+h);
}
//长方形底面面积
public double area(){
return 1*h;
}
}
/*
长方体类
*/
class Cubic extends Rect{
public Cubic(double l,double h,double z){
super(l,h,z);
}
//长方体表面积,重写父类方法
public double area(){
return 2*super.area()+length()*z;
}
//长方体体积,子类新增方法
public double volumn(){
return super.area()*z;
}
}
//四棱锥
class Pyramid extends Rect{
public Pyramid(double l,double h,double z){
super(l,h,z);
}
//四棱锥表面积,重写父类方法
public double area(){
return super.area()
+ l*Math.sqrt(z*z + h*h/4)
+ h*Math.sqrt(z*z + l*l/4);
}
//四棱锥体积,子类新增方法
public double volumn(){
return super.area()*z/3;
}
}
7-1 sdut-oop-5 计算长方体和四棱锥的表面积和体积(类的继承) (10分)
最新推荐文章于 2024-06-04 13:46:00 发布