Java第九次作业--接口及接口回调

 ̄綄美尐妖づ 提交于 2020-03-12 03:24:35

一.题目

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

 

二.代码及注释

/**Shape类,创建接口,定义抽象类*/

package ccut.cn;

interface Shape {
    
     abstract double getArea();

}

/**矩形类,创建长宽,构造方法,计算矩形面积*/

package ccut.cn;

public class Rectangle implements Shape {
    
    double height;
    double width;
    Rectangle(double height,double width){
        this.height = height;
        this.width = width;
        
    }
    public double getArea(){
        double area=height*width;
        return area;
    }
    

}

 

/**正方形类,创建正方形边长,构造方法,并求正方形面积*/

 

package ccut.cn;

public class Square implements Shape {

    
    double side;
    Square(double side){
        this.side = side;
    }
    public double getArea(){
        return side*side;
    }

}

 

/**圆形类,创建圆的半径的成员变量,构造方法,及求圆的面积*/

 

package ccut.cn;

public class Circle implements Shape {


    double radius;
    Circle(double radius){
        this.radius=radius;
    }
    public double getArea(){
        return 3.14*radius*radius;
    }

}

 

/**三角形类,创建三角形三条边的成员变量,构造方法及求三角形面积*/

 

package ccut.cn;

public class Triangle implements Shape{

    
    
    double a;
    double b;
    double c;
    Triangle(double a,double b,double c){
        this.a = a;
        this.b = b;
        this.c = c;
        
    }
    public double getArea(){
        double s =(a+b+c)/2;
        double S = (float) Math.sqrt(s*(s-a)*(s-b)*(s-c));
        return S;
    }

}

 

/**梯形类,创建上底下底及高的成员变量,构造方法及求梯形的面积*/

 

package ccut.cn;

public class Trapezoid implements Shape{

    
    double s_height;
    double x_height;
    double tall;
    Trapezoid(double s_height,double x_height,double tall){
        this.s_height = s_height;
        this.x_height = x_height;
        this.tall = tall;
    }
    public double getArea(){
        return (s_height+x_height)*tall/2;
    
    }

}

 

/**柱体类,创建柱体高的成员变量,构造方法及计算柱体体积*/

 

package ccut.cn;

public class Cone {
    double length;
    Shape shape;
    Cone( Shape shape , double length){
        this.shape = shape;
        this.length = length;
    }
    double getVolume(){
        double v=shape.getArea()*length;
        return v;
    }
}

 

/**工厂类,对用户所需求的形状做判断,并返回相对应的形状的面积*/

package ccut.cn;
import java.util.*;
public class Factory {
    
    static Shape shape = null;
    static Scanner se=new Scanner(System.in);
    public static Shape a(char i){
        switch(i){
        case 'c' :System.out.println("请输入圆的半径:");
                  double c = se.nextDouble();
                  shape = new Circle(c);
                  break;
        case 't' :System.out.println("请输入正方形的边长:");
                  double t = se.nextDouble();
                  shape = new Square(t);
                  break;
        case 'r' :System.out.println("请输入矩形的长宽:");
                  double l = se.nextDouble();
                  double k = se.nextDouble();
                  shape = new Rectangle(l,k);
                  break;
        case 's' :System.out.println("请输入三角形三条边:");
                  double a = se.nextDouble();
                  double b = se.nextDouble();
                  double d = se.nextDouble();
                  shape = new Triangle(a,b,d);
                  break;
        case 'x' :System.out.println("请输入梯形的上底,下底和高:");
                  double s = se.nextDouble();
                  double x = se.nextDouble();
                  double h = se.nextDouble();
                  shape = new Trapezoid(s,x,h);
                  break;
          }
        
        return shape;
    }
    
    
}

 

/**主类,用户输入所需要求柱体的底的形状,调用工厂类,并求相对应的体积*/

 

package ccut.cn;
import java.util.*;
public class Zhulei {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("柱体高默认为10,请输入底面积图形:");
        System.out.println("圆形(c),正方形(t),矩形(r),三角形(s),梯形(x)");
        Scanner se = new Scanner(System.in);
        char i = se.next().charAt(0);
        
        Cone cone = new Cone(Factory.a(i),10);
        System.out.println(cone.getVolume());
        
        

    }

}

 

三.运行结果

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!