zoukankan      html  css  js  c++  java
  • Java实验项目三——平面图形和立体图形抽象类

    Program:按照下面要求完成类的设计

    (1)设计一个平面图形抽象类和一个立体图形抽象类,提供该类对象公共的方法和属性。

    (2)修改项目三中第2题中所设计的球类、圆柱类,圆锥类、矩形类、三角形类、圆类,分别继承平面图形抽象类和立体图形抽象类。

    (3)运行项目三中第2题中的测试方法,进行测试。

    Description:

    1、在abstractclass包下分别定义了平面图像抽象类Planum和立体图形抽象类Soild。

    2、在entity包中,创建了实体类:Triangle(三角形),Rectangle(矩形),Cylinder(圆柱体),Sphere(球体)。

    3、在main包中定义测试类TestDemo,进行测试。

        具体代码如下:

    抽象类

     1 /*
     2  * Description:定义平面图形的抽象类
     3  * 
     4  * Written By:Cai
     5  * 
     6  * Date Written:2017-10-16
     7  * 
     8  * */
     9 
    10 
    11 package abstractclass;
    12 
    13 public abstract class Planum {
    14 
    15     private double sideLength;        //边长
    16     private double height;            //17     
    18     //定义构造方法
    19     public Planum(double sideLength,double height) {
    20         
    21         this.sideLength = sideLength;
    22         this.height = height;
    23     }
    24 
    25     //定义setter()和getter()方法
    26     
    27     public double getSideLength() {
    28         return sideLength;
    29     }
    30 
    31     public void setSideLength(double sideLength) {
    32         this.sideLength = sideLength;
    33     }
    34 
    35     public double getHeight() {
    36         return height;
    37     }
    38 
    39     public void setHeight(double height) {
    40         this.height = height;
    41     }
    42     
    43     //抽象抽象方法,求得面积
    44     public abstract double getArea();
    45     
    46     
    47 }
     1 /*
     2  * Description:定义立体图形的抽象类
     3  * 
     4  * Written By:Cai
     5  * 
     6  * Date Written:2017-10-16
     7  * 
     8  * */
     9 
    10 package abstractclass;
    11 
    12 public abstract class Solid {
    13     
    14     private double r;            //声明半径
    15     private final double PI = 3.14;    //声明π
    16     
    17     //定义构造方法
    18     public Solid(double r) {
    19         
    20         this.r = r;
    21     }
    22 
    23     //定义sette()和getter()方法
    24     
    25     public double getR() {
    26         return r;
    27     }
    28 
    29     public void setR(double r) {
    30         this.r = r;
    31     }
    32     
    33     public double getPI() {
    34         
    35         return this.PI;
    36     }
    37     
    38     //声明抽象方法,取得表面积
    39     public abstract double getArea();
    40     //声明抽象方法,取得体积
    41     public abstract double getSolid();
    42     
    45 }

    实体类:

     1 /*
     2  * Description:定义圆柱的实体类
     3  * 
     4  * Written By:Cai
     5  * 
     6  * Date Written:2017-10-16
     7  * 
     8  * */
     9 
    10 package entity;
    11 
    12 
    13 import abstractclass.Solid;
    14 
    15 public class Cylinder extends Solid {
    16 
    17     private double height;        //声明圆柱体的高度
    18     
    19     //定义构造方法
    20     public Cylinder(double r,double height) {
    21         super(r);
    22         this.height = height;
    23     }
    24 
    25     //实现父类抽象方法,求得圆柱的表面积
    26     public double getArea() {
    27         
    28         return 4 * this.getPI() * Math.pow(this.getR(), 2);
    29     }
    30 
    31     //实现父类抽象方法,求得圆柱的体积
    32     public double getSolid() {
    33         
    34         double result =  this.getPI() * Math.pow(this.getR(), 2) * this.height;
    35         return Math.round(result*100.0) / 100.0;
    36     }
    37 }
     1 /*
     2  * Description:定义球的实体类
     3  * 
     4  * Written By:Cai
     5  * 
     6  * Date Written:2017-10-16
     7  * 
     8  * */
     9 
    10 package entity;
    11 
    12 import abstractclass.Solid;
    13 
    14 public class Sphere extends Solid {
    15 
    16     //定义构造方法
    17     public Sphere(double r) {
    18         super(r);
    19     }
    20 
    21     //实现父类抽象方法,求得球的表面积
    22     public double getArea() {
    23         
    24         return 4 * this.getPI() * Math.pow(this.getR(), 2);
    25     }
    26 
    27     //实现父类抽象方法,求得球的体积
    28     public double getSolid() {
    29         
    30         return this.getPI() * Math.pow(this.getR(), 3);
    31     }
    32     
    33 }
     1 /*
     2  * Description:定义矩形实体类
     3  * 
     4  * Written By:Cai
     5  * 
     6  * Date Written:2017-10-16
     7  * 
     8  * */
     9 
    10 package entity;
    11 
    12 import abstractclass.Planum;
    13 
    14 public class Rectangle extends Planum {
    15 
    16     //定义构造方法
    17     public Rectangle(double sideLength, double height) {
    18         
    19         super(sideLength, height);
    20     }
    21 
    22     //实现父类的抽象方法,求得矩形面积
    23     public double getArea() {
    24         
    25         return this.getHeight() * this.getSideLength();
    26     }
    27 
    28     
    29 }
     1 /*
     2  * Description:定义矩形实体类
     3  * 
     4  * Written By:Cai
     5  * 
     6  * Date Written:2017-10-16
     7  * 
     8  * */
     9 
    10 package entity;
    11 
    12 import abstractclass.Planum;
    13 
    14 public class Triangle extends Planum {
    15 
    16     //定义构造方法
    17     public Triangle(double sideLength, double height) {
    18         super(sideLength, height);
    19     }
    20 
    21     //实现父类的抽象方法,求得三角形面积
    22     public double getArea() {
    23         
    24         return this.getHeight() * this.getSideLength() / 2;
    25     }
    26 
    27     
    28 }

    测试类

     1 /*
     2  * Description:定义测试类
     3  * 
     4  * Written By:Cai
     5  * 
     6  * Date Written:2017-10-16
     7  * 
     8  * */
     9 
    10 package main;
    11 
    12 import entity.Cylinder;
    13 import entity.Sphere;
    14 import entity.Triangle;
    15 import abstractclass.Planum;
    16 import abstractclass.Solid;
    17 
    18 public class TestDemo {
    19     
    20     public static void main(String args[]) {
    21         
    22         //实例化平面图形的抽象类对象
    23         Planum tri = new Triangle(10, 10);           //三角形
    24         Planum rec = new entity.Rectangle(10,20);    //矩形
    25         
    26         //实例化立体图形的抽象类对象
    27         Solid sph = new Sphere(1);                   //球体
    28         Solid cyl = new Cylinder(1, 10);             //圆柱
    29         
    30         //打印平面图形的面积
    31         System.out.println( "三角形的面积:" + tri.getArea() );
    32         System.out.println( "矩形的面积:" + rec.getArea() );
    33         
    34         System.out.println( "---------------风骚的Java分割线-----------------" );
    35         
    36         //打印球体的表面积和体积
    37         System.out.println( "球体的表面积:" + sph.getArea() );
    38         System.out.println( "球体的体积:" + sph.getSolid() );
    39         
    40         System.out.println( "---------------风骚的Java分割线-----------------" );
    41         
    42         //打印圆柱体的表面积和体积
    43         System.out.println( "圆柱体的表面积:" + cyl.getArea() );
    44         System.out.println( "圆柱的体积:" + cyl.getSolid() );    
    45         
    46     }
    47 
    48 }
  • 相关阅读:
    清华大学2015年自主招生暨领军计划试题
    高斯取整函数专题
    国际上的数学比赛
    清华大学数学系本科用什么教材?
    数学人眼中的湖北
    北京十一学校潘国双:激发学习的内在动力
    数学家Erdos的故事
    CentOS7关于网络的设置
    MySQL表连接
    MySQL的sql解析
  • 原文地址:https://www.cnblogs.com/caizhen/p/7684235.html
Copyright © 2011-2022 走看看