当前位置:   article > 正文

123 - 长方形长方体类import java.util.Scanner; public class Main{ public static void main(String[] args)_import java.util.scanner; public class main { publ

import java.util.scanner; public class main { public static void main(string

123 - 长方形长方体类

Time Limit: 1000   Memory Limit: 65535
Submit: 288  Solved: 118

Description

定义一个长方形类Rectangle,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
定义一个子类长方体类,拥有长、宽、高属性,提供构造函数,getPerimeter函数计算所有边的周长,getArea函数计算表面积,新增getVolume函数,计算体积
在main函数中,分别构造长方形类和长方体类的对象,并输出他们的周长、面积、体积,保留两位小数

Input

长方形类的长、宽
长方体类的长、宽、高

Output

长方形的周长和面积
长方体的周长,表面积,体积

Sample Input

1 2
1 2 3

Sample Output

6.00 2.00
24.00 22.00 6.00

HINT

Pre Append Code

  1. import java.util.Scanner;
  2. public class Main{
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. double length = scan.nextDouble();
  6. double wide = scan.nextDouble();
  7. Rectangle r = new Rectangle(length,wide);
  8. System.out.printf("%.2f ",r.getPerimeter());
  9. System.out.printf("%.2f",r.getArea());
  10. System.out.println();
  11. length = scan.nextDouble();
  12. wide = scan.nextDouble();
  13. double height = scan.nextDouble();
  14. Cuboid c = new Cuboid (length, wide, height);
  15. System.out.printf("%.2f ",c.getPerimeter());
  16. System.out.printf("%.2f ",c.getArea());
  17. System.out.printf("%.2f",c.getVolume());
  18. scan.close();
  19. }
  20. }
  1. class Rectangle
  2. {
  3. double length;
  4. double width;
  5. Rectangle(double length, double width)
  6. {
  7. this.length = length;
  8. this.width = width;
  9. }
  10. public double getPerimeter()
  11. {
  12. return 2*(length+width);
  13. }
  14. public double getArea()
  15. {
  16. return length*width;
  17. }
  18. }
  19. class Cuboid extends Rectangle
  20. {
  21. double height;
  22. Cuboid(double length, double width,double height)
  23. {
  24. super(length, width);
  25. this.height = height;
  26. }
  27. public double getPerimeter()
  28. {
  29. return 4*(length+width+height);
  30. }
  31. public double getArea()
  32. {
  33. return 2*(length * width + length * height + height * width);
  34. }
  35. public double getVolume()
  36. {
  37. return length * height * width;
  38. }
  39. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/207369
推荐阅读
相关标签
  

闽ICP备14008679号