计算长方体、四棱锥的表面积和体积(继承)

计算长方体、四棱锥的表面积和体积(继承)_第1张图片

import java.util.*;
class Rect{
	double l;
	double h;
	double z;
	public Rect(double l,double h,double z) {
		this.l=l;
		this.h=h;
		this.z=z;
	}
	double length() {
		return 2*(l+h);
	}
	double area() {
		return l*h;
	}
}
class Cubic extends Rect{
	public Cubic(double l,double h,double z) {
		super(l, h, z);
	}
	double s() {
		return 2*(l*h+l*z+h*z);
	}
	double v() {
		return l*h*z;
	}
}
class Pyramid extends Rect{
	public Pyramid(double l,double h,double z) {
		super(l, h, z);
	}
	double s() {
		double x=Math.sqrt(z*z+l*l/4);
		double y=Math.sqrt(z*z+h*h/4);
		double sum=x*h+y*l+l*h;
		return sum;
	}
	double v() {
		return l*h*z/3;
	}
}

public class 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();
			
			if(l>0&&h>0&&z>0) {
				Cubic cu=new Cubic(l, h, z);
				double s1=cu.s();
				double v1=cu.v();
				System.out.printf("%.2f %.2f ",s1,v1);
				Pyramid py=new Pyramid(l, h, z);
				double s2=py.s();
				double v2=py.v();
				System.out.printf("%.2f %.2f\n",s2,v2);
			}
			else
				System.out.println("0.00 0.00 0.00 0.00");
		}
		sc.close();
	}
}

你可能感兴趣的:(类与对象)