题目:
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接口的定义,修改以上完成的程序,使程序功能正
常执行。
- mport java.util.Arrays;
-
- public class Test {
- public static void main(String[] args) {
- Cylinder[] cylinder={
- new Cylinder(new Circle(3),5),
- new Cylinder(new Ellipse(7,3),15),
- new Cylinder(new Ellipse(6,3),10)};
-
- System.out.println("===========厡式=================");
- for (Cylinder cylinder1 : cylinder) {
- System.out.println(cylinder1);
- }
- System.out.println("===========面积从小到大===========");
- Arrays.sort(cylinder,(o1, o2) -> Double.compare(o1.calculateArea(), o2.calculateArea()));
- for (Cylinder cylinder1 : cylinder) {
- System.out.println(cylinder1);
- }
- System.out.println("===========面积从大到小===========");
- Arrays.sort(cylinder,(o1, o2) -> Double.compare(o2.calculateArea(), o1.calculateArea()));
- for (Cylinder cylinder1 : cylinder) {
- System.out.println(cylinder1);
- }
- System.out.println("============测试draw功能===========");
- Geometry[] geometries={
- new Circle(3),
- new Ellipse(4,3),
- new Circle(5)};
- for (int i = 0; i < geometries.length; i++) {
- draw(geometries[i]);
- }
-
- }
- public static void draw(Geometry g){
- if(g instanceof Circle)
- {
- System.out.println("目前正在画一个柱体,底面为圆形");
- }
- if (g instanceof Ellipse){
- System.out.println("目前正在画一个柱体,底面为椭圆形");
- }
- }
- }
- import java.math.BigDecimal;
- import java.math.RoundingMode;
-
- public class Ellipse extends Geometry{
- private double a;//长
- private double b;//短
-
- public Ellipse(double a, double b) {
- this.a = a;
- this.b = b;
- }
-
- public double getA() {
- return a;
- }
-
- public void setA(double a) {
- this.a = a;
- }
-
- public double getB() {
- return b;
- }
-
- public void setB(double b) {
- this.b = b;
- }
-
- @Override
- public double calculateArea() {
- return new BigDecimal(Math.PI*(a/2.0)*(b/2.0)).setScale(3, RoundingMode.HALF_UP).doubleValue();
- }
- }
- mport java.math.BigDecimal;
- import java.math.RoundingMode;
-
- public class Cylinder extends Geometry{
- private Geometry bottom;
- private double h;
-
- public Cylinder(Geometry a, double h) {
- //多态
- bottom=a;
- this.h=h;
- }
-
- public Geometry getBottom() {
- return bottom;
- }
-
- public void setBottom(Circle bottom) {
- this.bottom = bottom;
- }
-
- public double getH() {
- return h;
- }
-
- public void setH(double h) {
- this.h = h;
- }
-
- @Override
- public double calculateArea() {
- if(bottom instanceof Circle)
- {
- return
- new BigDecimal(2*bottom.calculateArea()+2.0*Math.PI*((Circle) bottom).getRadius()*h)
- .setScale(3, RoundingMode.HALF_UP).doubleValue();
- }
- else if (bottom instanceof Ellipse){
- return new BigDecimal((2*bottom.calculateArea()+2.0*Math.PI* (((Ellipse) bottom).getB()/2.0)
- +4.0*((((Ellipse) bottom).getA())/2.0-((Ellipse) bottom).getB()/2.0))*h).setScale(3,RoundingMode.HALF_UP)
- .doubleValue();
- }
- return -1;
- }
-
- @Override
- public String toString() {
- if(bottom instanceof Circle){
- return "底面半径为"+((Circle) bottom).getRadius()+",高为:"+h+"的柱体表面积为"+this.calculateArea();
- }
- else if (bottom instanceof Ellipse){
- return "底面长轴为:"+((Ellipse) bottom).getA()+",短轴为:"+((Ellipse) bottom).getB()+",高为:"+h+"的柱体表面积为"+this.calculateArea();
- }
- else
- return null;
-
- }
- }
- import java.math.BigDecimal;
- import java.math.RoundingMode;
-
- public class Circle extends Geometry{
- private double radius;
-
- public Circle(double radius) {
- this.radius = radius;
- }
-
- public double getRadius() {
- return radius;
- }
-
- public void setRadius(double radius) {
- this.radius = radius;
- }
-
- @Override
- public double calculateArea() {
- return new BigDecimal(Math.PI*Math.pow(radius,2)).setScale(3, RoundingMode.HALF_UP).doubleValue();
- }
- }