• 努力打卡 每天学习 不浪费每一天 Day48


    题目:

    2.3.2编程题
    1.(基本题)本题目要求实现的各个类,按照其继承的顺序,分别是:
    抽象Geometry类:calculateArea()抽象方法。
    Circle类:用来描述圆的状态和行为,为Geometry的子类
    Ellipse类:描述椭圆的状态和行为,为Geometry的子类;
    Cylinder类:描述圆柱体,该类的属性bottom是Circle类对象;
    Test类:实例化以上各个类。
    各个类的具体说明如下:
    (1).编写描述圆的Circle类
    Circle类只有一个属性值radius,记录圆的半径。该类的方法包括:
    构造方法Circle(doubleradius),设置圆的半径;
    get和set方法(设置和获取属性值radius);
    calculateArea方法:计算圆的面积并返回结果。
    (2).编写描述椭圆的Ellipse类
    Ellipse,包含两个属性a和b,表示长短轴,实现的方法包括:
    构造方法Ellipse(doublea,doubleb);
    get和set方法(设置和获取属性值a,b);
    calculateArea方法:覆盖父类中的calculateArea方法,计算椭圆的面积。
    (3.14*(a/2)*(b/2))
    (3).编写描述圆柱体的Cylinder类,
    Cylinder类,包含两个属性,该属性bottom是Geometry类引用;属性h表示圆柱体的
    高。实现的方法包括:
    构造方法Cylinder(Geometrya,doubleh);
    get和set方法;
    calculateArea方法,计算圆柱体的表面积;
    (4).编写类Test,该类内有方法voiddraw(Geometryg),该方法输出“目前正在画一个
    xxx”,xxx为圆形或椭圆形。
    该类有main方法,方法内实例化以上各个类,过程如下:
    a.初始化一个Cylinder类型的数组,长度为3(元素中有bottom为Circle的也有bottom为
    Eclipse的),并用按对象面积对该数组排序,打印出排序后的结果;
    b.初始化一个Geometry类型的数组,长度为3,该数组中元素有Circle,也有Eclipse,并利
    用Test类的draw方法将数组元素逐个画出。
    (5).将Geometry抽象类改为interface接口的定义,修改以上完成的程序,使程序功能正
    常执行。

    1. mport java.util.Arrays;
    2. public class Test {
    3. public static void main(String[] args) {
    4. Cylinder[] cylinder={
    5. new Cylinder(new Circle(3),5),
    6. new Cylinder(new Ellipse(7,3),15),
    7. new Cylinder(new Ellipse(6,3),10)};
    8. System.out.println("===========厡式=================");
    9. for (Cylinder cylinder1 : cylinder) {
    10. System.out.println(cylinder1);
    11. }
    12. System.out.println("===========面积从小到大===========");
    13. Arrays.sort(cylinder,(o1, o2) -> Double.compare(o1.calculateArea(), o2.calculateArea()));
    14. for (Cylinder cylinder1 : cylinder) {
    15. System.out.println(cylinder1);
    16. }
    17. System.out.println("===========面积从大到小===========");
    18. Arrays.sort(cylinder,(o1, o2) -> Double.compare(o2.calculateArea(), o1.calculateArea()));
    19. for (Cylinder cylinder1 : cylinder) {
    20. System.out.println(cylinder1);
    21. }
    22. System.out.println("============测试draw功能===========");
    23. Geometry[] geometries={
    24. new Circle(3),
    25. new Ellipse(4,3),
    26. new Circle(5)};
    27. for (int i = 0; i < geometries.length; i++) {
    28. draw(geometries[i]);
    29. }
    30. }
    31. public static void draw(Geometry g){
    32. if(g instanceof Circle)
    33. {
    34. System.out.println("目前正在画一个柱体,底面为圆形");
    35. }
    36. if (g instanceof Ellipse){
    37. System.out.println("目前正在画一个柱体,底面为椭圆形");
    38. }
    39. }
    40. }
    1. import java.math.BigDecimal;
    2. import java.math.RoundingMode;
    3. public class Ellipse extends Geometry{
    4. private double a;//长
    5. private double b;//短
    6. public Ellipse(double a, double b) {
    7. this.a = a;
    8. this.b = b;
    9. }
    10. public double getA() {
    11. return a;
    12. }
    13. public void setA(double a) {
    14. this.a = a;
    15. }
    16. public double getB() {
    17. return b;
    18. }
    19. public void setB(double b) {
    20. this.b = b;
    21. }
    22. @Override
    23. public double calculateArea() {
    24. return new BigDecimal(Math.PI*(a/2.0)*(b/2.0)).setScale(3, RoundingMode.HALF_UP).doubleValue();
    25. }
    26. }
    1. mport java.math.BigDecimal;
    2. import java.math.RoundingMode;
    3. public class Cylinder extends Geometry{
    4. private Geometry bottom;
    5. private double h;
    6. public Cylinder(Geometry a, double h) {
    7. //多态
    8. bottom=a;
    9. this.h=h;
    10. }
    11. public Geometry getBottom() {
    12. return bottom;
    13. }
    14. public void setBottom(Circle bottom) {
    15. this.bottom = bottom;
    16. }
    17. public double getH() {
    18. return h;
    19. }
    20. public void setH(double h) {
    21. this.h = h;
    22. }
    23. @Override
    24. public double calculateArea() {
    25. if(bottom instanceof Circle)
    26. {
    27. return
    28. new BigDecimal(2*bottom.calculateArea()+2.0*Math.PI*((Circle) bottom).getRadius()*h)
    29. .setScale(3, RoundingMode.HALF_UP).doubleValue();
    30. }
    31. else if (bottom instanceof Ellipse){
    32. return new BigDecimal((2*bottom.calculateArea()+2.0*Math.PI* (((Ellipse) bottom).getB()/2.0)
    33. +4.0*((((Ellipse) bottom).getA())/2.0-((Ellipse) bottom).getB()/2.0))*h).setScale(3,RoundingMode.HALF_UP)
    34. .doubleValue();
    35. }
    36. return -1;
    37. }
    38. @Override
    39. public String toString() {
    40. if(bottom instanceof Circle){
    41. return "底面半径为"+((Circle) bottom).getRadius()+",高为:"+h+"的柱体表面积为"+this.calculateArea();
    42. }
    43. else if (bottom instanceof Ellipse){
    44. return "底面长轴为:"+((Ellipse) bottom).getA()+",短轴为:"+((Ellipse) bottom).getB()+",高为:"+h+"的柱体表面积为"+this.calculateArea();
    45. }
    46. else
    47. return null;
    48. }
    49. }
    1. import java.math.BigDecimal;
    2. import java.math.RoundingMode;
    3. public class Circle extends Geometry{
    4. private double radius;
    5. public Circle(double radius) {
    6. this.radius = radius;
    7. }
    8. public double getRadius() {
    9. return radius;
    10. }
    11. public void setRadius(double radius) {
    12. this.radius = radius;
    13. }
    14. @Override
    15. public double calculateArea() {
    16. return new BigDecimal(Math.PI*Math.pow(radius,2)).setScale(3, RoundingMode.HALF_UP).doubleValue();
    17. }
    18. }

  • 相关阅读:
    我与JAVA过七夕
    组件安全概述
    反射是什么
    分享5款同类软件中的翘楚,属于是WIN10必备良品
    基于Django开发的图书管理推荐、电影推荐、课程推荐系统、大众点评店铺推荐系统、健身课程管理系统
    Metronic 管理仪表板,高级引导仪表板主题
    【无标题】
    javascript 基础知识点整理
    微信小程序 uCharts的使用方法
    【Python&BA】商业分析:Business Analytics 的理解
  • 原文地址:https://blog.csdn.net/Haenu0317/article/details/127130078