赞
踩
1 2 3 0 2 3 -1 2 3 3 4 5
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
zhouxq
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- 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);
- }
- sc.close();
- }
-
- }
-
- /**
- * 父类
- *
- * @author student
- *
- */
- 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 Rect(int l) {
- this(l, l, l);
- }
-
- public Rect() {
- }
-
- /**
- * 计算长方体底面周长
- */
- public double length() {
- return 2 * (l + h);
- }
-
- /**
- * 长方体底面面积
- */
- public double area() {
- return l * 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;
- }
- }
-
- /**
- * 四椎体类
- *
- * @author student
- *
- */
- 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;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。