ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-09-30 16:00:10  阅读:201  来源: 互联网

标签:double 作业 Shape 接口 shape reader 回调 public nextDouble


题目:

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

代码:

1 package y;
2 // 图形接口 
3 public interface Shape {
4     double getArea();
5 }
 1 package y;
 2 //圆类重写图形接口,重写面积方法
 3 public class Circle implements Shape {
 4         double r;     
 5         public Circle(double r){
 6             this.r=r;
 7         }
 8     @Override
 9     public double getArea() {
10         // TODO Auto-generated method stub
11           return Math.PI*r*r;
12     }
13 
14 }
 1 package y;
 2 //矩形类重写图形接口,重写面积方法
 3 public class Rectangle implements Shape {
 4         double width;
 5         double length;
 6         public Rectangle(double width, double length) {
 7             this.width = width;
 8             this.length = length;
 9         }      
10     @Override
11     public double getArea() {
12         // TODO Auto-generated method stub        
13         return  width * length;
14     }
15     
16 
17 }
 1 package y;
 2 //圆类重写图形接口,重写面积方法
 3 public class Trapezoid implements Shape{
 4     double s;
 5     double  x;
 6     double  h;
 7     public Trapezoid(double s, double x,double h) {
 8     this.s =s;
 9     this.x =x;
10     this.h = h;
11         }      
12     @Override
13     public double getArea() {
14         // TODO Auto-generated method stub        
15         return  (s+x)*h/2.0;
16     }
17 
18 }
 1 package y;
 2 //三角形类重写图形接口,重写面积方法
 3 public class Triangle implements Shape {
 4     double a;
 5     double b;
 6     double c;
 7     public Triangle(double a,double b,double c){
 8         this.a=a;
 9         this.b=b;
10         this.c=c;
11     }
12     @Override
13     public double getArea() {
14         // TODO Auto-generated method stub
15         double p=(a+b+c)/2;
16         return Math.sqrt(p*(p-a)*(p-b)*(p-c));
17     }
18 
19 }
 1 package y;
 2 //正方形类继承矩形类
 3 public class Zheng extends Rectangle {
 4      public Zheng(double side) {
 5             super(side, side);
 6         }
 7     @Override
 8     public double getArea() {
 9         // TODO Auto-generated method stub
10         return width * width;
11     }
12 
13 }
 1 package y;
 2 //柱类,包括构造方法,求体积方法,和换底方法
 3 public class Cone {
 4      Shape shape;
 5         double high;
 6         
 7         public Cone(Shape shape, double high) {
 8             this.shape = shape;
 9             this.high = high;
10         }
11         public double getVolume() {
12             return shape.getArea()*high;
13         }
14         public void setShape(Shape shape)
15         {
16             this.shape=shape;
17         }
18 }
 1 package y;
 2 
 3 import java.util.Scanner;
 4 //工厂类
 5 public class Foctory {
 6      public Shape getShape(char c) {
 7          Scanner reader = new Scanner(System.in);
 8          Shape shape = null;
 9          switch (c) {
10          case 'R':
11              System.out.println("请输入矩形的长和宽、柱体的高");
12              shape = new Rectangle(reader.nextInt(), reader.nextDouble());
13              break;
14          case 'S':
15              System.out.println("请输入三角形的边柱体的高");
16              shape = new Triangle(reader.nextDouble(), reader.nextDouble(), reader.nextDouble());
17              break;
18          case 'C':
19              System.out.println("请输入圆半径,柱体的高");
20              shape = new Circle(reader.nextDouble());
21              break;
22          case 'T':
23              System.out.println("请输入梯形的上底下底和高");
24              shape = new Trapezoid(reader.nextDouble(), reader.nextDouble(), reader.nextDouble());
25              break;
26          case 'Z':
27              System.out.println("请输入正方形的边长,柱体的高");
28              shape = new Zheng(reader.nextDouble());
29              break;
30          }
31          return shape;
32  }
33 }
package h;

import java.util.Scanner;

import y.Cone;
import y.Foctory;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         while(true){
                Scanner reader = new Scanner(System.in);
                System.out.println("矩形R,三角形S,圆C,梯形T,正方形Z");
                char c = reader.next().charAt(0);
                Foctory f = new Foctory();
                Cone cone = new Cone(f.getShape(c), reader.nextDouble());
                System.out.println(cone.getVolume());
            }

    }

}

运行

 

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有