其他分享
首页 > 其他分享> > 第9次作业--接口及接口回调

第9次作业--接口及接口回调

作者:互联网

题目:

       利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。

Shape.java

1 package com;
2 //接口
3 public interface Shape {
4     double getArea();
5 
6 }

 

Rectangle.java

 1 package com;
 2 
 3 public class Rectangle implements Shape {
 4     double a;//矩形長
 5     double b;//矩形寬
 6 
 7     public Rectangle(double a, double b) {
 8         this.a = a;
 9         this.b = b;
10     }
11 
12     @Override
13     public double getArea() {
14 
15         return a * b;//返回面積
16     }
17 
18 }

 

Squera.java



 1 package com;
 2 
 3 public class Squera extends Rectangle {
 4     public Squera(double a) {//正方形的邊
 5         super(a, a);
 6 
 7     }
 8 
 9     public double getArea() {
10         return a * a;
11     }
12 
13 }

 

 
 1 package com;
 2 
 3 public class Triangle implements Shape {
 4 
 5     double a;//三角形三邊長
 6     double b;
 7     double c;
 8 
 9     public Triangle(double a, double b, double c) {
10         this.a = a;
11         this.b = b;
12         this.c = c;
13     }
14 
15     public double getArea() {
16         double p = (a + b + c) / 2;
17         return Math.sqrt(p * (p - a) * (p - b) * (p - c));//三角形面積
18     }
19 
20 }

 

Cricle.java

 1 package com;
 2 
 3 public class Cricle implements Shape {
 4 
 5     double r;//半径
 6     double PI = 3.14;
 7 
 8     public Cricle(double r) {
 9         this.r = r;
10     }
11 
12     public double getArea() {
13 
14         return r * r * PI;//
15     }
16 
17 }

 

Trapezoid.java

 1 package com;
 2 
 3 public class Trapezoid implements Shape {
 4 
 5     double a;//梯形的两边及高
 6     double b;//
 7     double h;//
 8     public Trapezoid(double a,double b,double h) {
 9         this.a=a;
10         this.b=b;
11         this.h=h;
12         // TODO Auto-generated constructor stub
13     }
14     public double getArea() {
15         
16         return (a+b)*h/2;//梯形面积
17     }
18 
19 }

 

Cone.java

package com;

public class Cone {
    Shape shape;//定義接口變量
    double h;//高

    public Cone(Shape shape, double h) {
        this.shape = shape;
        this.h = h;

    }

    public double getV() {//球體積
        return shape.getArea() * h;
    }

    public void changeD() {//換底
        this.shape = shape;
    }
}

 

Factory.java

 1 package com;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Factory {
 6 
 7     Shape getShape(char c) {
 8         Scanner reader = new Scanner(System.in);
 9         Shape shape = null;
10         switch (c) {//根据字符判断形状
11         case 'R':
12             System.out.println("请输入矩形的长和宽\n");
13             shape = new Rectangle(reader.nextInt(), reader.nextDouble());
14             break;
15         case 'Z':
16             System.out.println("请输入正方形的边长,柱体的高\n");
17             shape = new Squera(reader.nextDouble());
18             break;
19         case 'C':
20             System.out.println("请输入圆的半径\n");
21             shape = new Cricle(reader.nextDouble());
22             break;
23         case 't':
24             System.out.println("请输入三角形的三条边\n");
25             shape = new Triangle(reader.nextDouble(), reader.nextDouble(),
26                     reader.nextDouble());
27             break;
28 
29         case 'T':
30             System.out.println("请输入梯形的上底下底和高\n");
31             shape = new Trapezoid(reader.nextDouble(), reader.nextDouble(),
32                     reader.nextDouble());
33             break;
34 
35         }
36         return shape;//
37     }
38 }

 

Test.java

 1 package com;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test {
 6     public static void main(String[] args) {
 7 
 8         Scanner reader = new Scanner(System.in);
 9         System.out.print("请输入你选择的形状:矩形R,正方形Z,三角形t,圆C,梯形T:\n");
10         char c = reader.next().charAt(0);
11         Factory factory = new Factory();
12         Cone cone = new Cone(factory.getShape(c), reader.nextDouble());//获取底面积,高
13         System.out.print(cone.getV());
14 
15     }
16 }

 

标签:java,double,作业,接口,shape,reader,回调,public,nextDouble
来源: https://www.cnblogs.com/hzcxwz/p/11617220.html