第九次作业

一题目

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

二代码

    /**创建接口,并创建求面积的方法*/

package edu;
public interface Shape {
    double getArea();
}

    /**在主类中输出以各种其他图形为底的体积*/

package edu;
import java.util.*;
public class Test {
    public static void main(String[] args) {
        for(int j=0;j<5;j++){
        System.out.println("输入的形状为:");
        G g1=new G();
        Cone cone1=new Cone(g1.getShape(),6);
        cone1.setShape(g1.getShape());
        double i=cone1.getVolume();
        System.out.println(i);
                }
    }
}

   /** 创建工厂类,实现输入一个相应的字符,输出相应图形的体积  */

   

package edu;
import java.util.*;
import java.io.InputStreamReader;
public class G {
        Shape shape;
        Scanner s=new Scanner(System.in);
        char c=s.next().charAt(0);
        public Shape getShape(){
            switch(c){
            case 's':shape=new Triangle(4,5,6);break;//三角形
            case 'r':shape=new Rect(4,5);break;//矩形
            case 't':shape=new Tx(4,5,5);break;//梯形
            case 'z':shape=new Z(5);break;//正方形
            case 'c':shape=new Circle(6);break;//圆形
            }
            return shape;
        }
        public char getC(){
            return c;
            }
        public void setShape(Shape shape){
            this.shape=shape;
            }
        public void setC(char c){
            this.c=c;
        }
}

    /**创建矩形类,实现求矩形面积 */

     

package edu;
    public class Rect implements Shape{
    protected double c,w;
    public Rect(double c,double w){
            this.c=c;
            this.w=w;
            }
        public double getArea(){
            return c*w;
        }
        }

     /**创建正方形类,求正方形为底的面积*/

     

package edu;
public class Z implements Shape{
    double bian;
    Z(double bian){
        this.bian=bian;
        }
    public double getArea(){
        return bian*bian;
    }
}

     /**创建梯形类,求以梯形为底的面积*/

     

package edu;
public class Tx implements Shape{
    double t,h,d;
    public Tx(double t,double d,double h){
        this.t=t;
        this.d=d;
        this.h=h;
    }
    public double getArea(){
        return (t+d)*h/2;
    }
    }

     /**创建圆形类,计算圆的面积*/

     

package edu;
public class Circle implements Shape {
    double r;
    public Circle(double r){
        this.r=r;
    }
    public double getArea(){
        return Math.PI*r*r;
    }
    }

       /**创建三角形类,求三角形的面积*/

       

package edu;
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 p=(a+b+c)/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
    }

         /**创建柱体类,获得柱体体积和换底的方法*/

         

package edu;
public class Cone {
    Shape shape;
    double h;
    public Cone(Shape shape,double h){
        this.shape=shape;
        this.h=h;
    }
    public double getVolume(){
        return shape.getArea()*h;
    }
    public void setShape(Shape shape){
        this.shape=shape;
    }
}

 三运行结果

原文地址:https://www.cnblogs.com/12-abc/p/11620192.html