计算长方体、四棱锥的表面积和体积

计算长方体、四棱锥的表面积和体积

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

计算如下立体图形的表面积和体积。

            

                         

从图中观察,可抽取其共同属性到父类Rect中:长度:l  宽度:h  高度:z

在父类Rect中,定义求底面周长的方法length( )和底面积的方法area( )。

 

定义父类Rect的子类立方体类Cubic,计算立方体的表面积和体积。其中表面积area( )重写父类的方法。

定义父类Rect的子类四棱锥类Pyramid,计算四棱锥的表面积和体积。其中表面积area( )重写父类的方法。

输入立体图形的长(l)、宽(h)、高(z)数据,分别输出长方体的表面积、体积、四棱锥的表面积和体积。

Input

 输入多行数值型数据(double);

每行三个数值,分别表示l h z

若输入数据中有非正数,则不表示任何图形,表面积和体积均为0。

Output

 行数与输入相对应,数值为长方体表面积 长方体体积 四棱锥表面积 四棱锥体积(中间有一个空格作为间隔,数值保留两位小数)

Example Input
1 2 3
0 2 3
-1 2 3
3 4 5
Example Output
22.00 6.00 11.25 2.00
0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00
94.00 60.00 49.04 20.00
Hint

 四棱锥体公式:V=1/3Sh,S——底面积 h——高


import java.util.*;
import java.math.*;
class Rect{
	public double l,h,z;
	
	Rect(){
		l = 0; 
		h = 0;
		z = 0;
	}
	Rect(double ll,double hh,double zz){
		l = ll;
		h = hh;
		z = zz;
	}
	void length(){
		System.out.print((l+h)*2);
	}
	void area(){
		System.out.print(l*h);
	}
}
class Cubic extends Rect{
	Cubic(){
		l = 0; 
		h = 0;
		z = 0;
	}
	Cubic(double ll,double hh,double zz){
		l = ll;
		h = hh;
		z = zz;
	}
	void area(){
		System.out.print(String.format("%.2f",(l*h+l*z+z*h)*2));
	}
	void volume(){
		System.out.print(String.format("%.2f",l*h*z));
	}
}

class Pyramid extends Rect{
	Pyramid(){
		l = 0; 
		h = 0;
		z = 0;
	}
	Pyramid(double ll,double hh,double zz){
		l = ll;
		h = hh;
		z = zz;
	}
	void area(){
		double h1 = Math.sqrt((h/2)*(h/2)+z*z);
		double h2 = Math.sqrt((l/2)*(l/2)+z*z);
		System.out.print(String.format("%.2f", l*h+h1*l+h2*h));
		
	}
	void volume(){
		System.out.print(String.format("%.2f",l*h*z/3));
	}
}


public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		while(input.hasNext()){
			double l,h,z;
			l = input.nextDouble();
			h = input.nextDouble();
			z = input.nextDouble();
			if(l<=0||h<=0||z<=0){
				Rect a = new Rect();
				Cubic b = new Cubic();
				b.area();
				System.out.print(" ");
				b.volume();
				System.out.print(" ");
				Pyramid c = new Pyramid();
				c.area();
				System.out.print(" ");
				c.volume();
				System.out.println();
			}else{
				Rect a = new Rect(l,h,z);
				Cubic b = new Cubic(l,h,z);
				b.area();
				System.out.print(" ");
				b.volume();
				System.out.print(" ");
				Pyramid c = new Pyramid(l,h,z);
				c.area();
				System.out.print(" ");
				c.volume();
				System.out.println();
				
			}
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值