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

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

一、源代码

1、Shape.java

package cn.edu.ccut.jia;

public interface Shape {      //定义形状接口
abstract double getArea();
}

2、Rectangle.java

package cn.edu.ccut.jia;
//定义矩形类,创建长、宽两个成员变量,定义求面积方法
public class Rectangle implements Shape{
     double length;
     double width;
    public Rectangle(double length,double width){
        this.length=length;
        this.width=width;
    }
public double getArea(){
    
    return     length*width;
    }
}

3、Zheng.java

package cn.edu.ccut.jia;
//定义一个正方形类,并继承矩形类
public class Zheng extends Rectangle{
    Zheng(double side){
        super(side,side);//super调用父类方法
    }
    public double getArea(){//面积方法重写
        
    return    length*length;
    }

}

4、Yuan.java

package cn.edu.ccut.jia;
//定义一个圆类,创建成员变量r,定义求面积方法
public class Yuan implements Shape {
    double r;
   double PI=3.14;
    public Yuan(double r){
        this.r=r;
    }
    public double getArea(){
        
        return     r*r*PI;
        }
}

5、Tringle.java

package cn.edu.ccut.jia;
//定义一个三角形类,创建三个成员变量w,x,y,定义求面积方法
public class Tringle  implements Shape{
     double w;
     double x;
     double y;
    public Tringle(double w,double x,double y){
        this.w=w;
        this.x=x;
        this.y=y;
}
public double getArea(){
    double q=(w+x+y)/2;

    return  Math.sqrt(q*(q-w)*(q-x)*(q-y));
}
}

6、Tixing.java

package cn.edu.ccut.jia;
//定义一个梯形类,创建三个成员变量a,b,h,定义求面积方法
public class Tixing implements Shape{
     double a;
     double b;
     double h;
    public Tixing(double a,double b,double h){
        this.a=a;
        this.b=b;
        this.h=h;
    }
    public double getArea(){
        return (a+b)*h/2;
    }
}

7、Cone.java

package cn.edu.ccut.jia;
//定义一个柱体类,创建一个成员变量high和接口对象,定义求体积方法
public class Cone {
    double  high;
    Shape shape;
    public Cone(Shape shape,double high){
        this.shape=shape;
        this.high=high;
    }
      double getV(){
        return  high* shape.getArea();
     }
        
}

8、Foctory.java

package cn.edu.ccut.jia;
//创建工厂类
import java.util.*;
public class Foctory {
    Scanner reader=new Scanner(System.in);
    Shape getShape(char d){
        Shape shape=null;
        switch(d){
        case 'r':
            System.out.println("请输入矩形的长、宽和柱体的高");
            shape=new Rectangle(reader.nextDouble(),reader.nextDouble());
        break;
        case 'z':
            System.out.println("请输入正方形的边长和柱体的高");
            shape=new Zheng (reader.nextDouble());
        break;
        case 'y':
            System.out.println("请输入圆的半径和柱体的高");
            shape=new Yuan(reader.nextDouble());
        break;
        case's':
            System.out.println("请输入三角形的边长和柱体的高");
            shape=new Tringle(reader.nextDouble(),reader.nextDouble(),reader.nextDouble());
        break;
        case 't':
            System.out.println("请输入梯形的上底、下底、高和柱体的高");
            shape=new Tixing(reader.nextDouble(),reader.nextDouble(),reader.nextDouble());
        break;
        }
        return shape;
     }
}

9、Test.java

package cn.edu.ccut.jia;
import cn.edu.ccut.jia.*;
import java.util.*;
public class Test {
    public static void main(String[] args){
        while(true){
            Scanner reader=new Scanner(System.in);
        System.out.println("请输入您要选择的形状:矩形r,正方形z,三角形s,圆y,梯形t");
        char d=reader.next().charAt(0);
        Foctory f=new Foctory();
        Cone c=new Cone(f.getShape(d),reader.nextDouble());
        System.out.println(c.getV());
        
    }

}
}

二、运行截图

posted @ 2019-09-30 12:32  长安别故里  阅读(364)  评论(0编辑  收藏  举报