zoukankan      html  css  js  c++  java
  • 于达——第九次作业

    题目:

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

    package example;
    
    
    interface Shape {//定义接口 接口方法
        abstract double getArea();
    
    }
    package example;
    
    class rectangle implements Shape {//长方形类
    double chang;
    double kuan;
    rectangle(double chang,double kuan){
        this.chang=chang;
        this.kuan=kuan;
    }
    
        public double getArea(){
        
            return chang*kuan;
        }
    
    }
    class zheng implements Shape{//正方形类
        double bian;
        double area;
        zheng (double bian){
            this.bian=bian;
        }
        public double getArea() {
            
            return area=bian*bian;
        }
    }
    class yuan implements Shape{//圆形类
        final double p=3.14;
        double banjing;
        yuan(double banjing){
            this.banjing=banjing;
        }
        public double getArea() {
            return p*banjing*banjing;
        }
    }
    class tixing implements Shape{//梯形类
        double shangdi;
        double xiadi;
        double gao;
        tixing(double shangdi,double xiadi,double gao){
            this.shangdi=shangdi;
            this.xiadi=xiadi;
            this.gao=gao;
        }
        public double getArea() {
            return (shangdi+xiadi)*gao/2;
        }
    }
    class sanjiao implements Shape{//三角形类
        double a;
        double b;
        double c;
        sanjiao(double a,double b,double c){
            this.a=a;
            this.b=b;
            this.c=c;
        }
        public double getArea() {
            double d =(a+b+c)/2;
            return Math.sqrt(d*(d-a)*(d-b)*(d-c));
        }
    }
    class zhuti{//柱体类
        Shape shape;
        double gao;
        zhuti(Shape shape,double gao){
            this.shape=shape;
            this.gao=gao;
        }
        void huandi(Shape shape) {//换底方法
            this.shape=shape;
        }
        double volume(){//求体积方法
            return shape.getArea()*gao;
        }
    }
    package example;
    import java.util.*;
    
    
    class works {//简单工厂类
        Shape shape=null;
        Scanner reader = new Scanner(System.in);
    public Shape shape() {
        char z=reader.next().charAt(0);
            switch(z) {
            case'j':System.out.println("矩形体积是:");shape=new rectangle(3,4);break;
            case'z':System.out.println("正方形体积是:");shape=new zheng(5);break;
            case'y':System.out.println("圆形体积是:");shape=new yuan(5);break;
            case's':System.out.println("三角形体积是:");shape=new sanjiao(5,6,7);break;
            case't':System.out.println("梯形体积是:");shape=new tixing(5,6,7);break;
            }
            return shape;
    }
    
    
    }
    package example;
    
    public class js {//主类
    
        public static void main(String[] args) {//主方法
    double gao=5;
    works worker=new works();
    System.out.println("请输入t/z/c/y/s");
    for(;;) {//执行for循环,执行简单工厂里的switch,输入一个字母并输出对应柱体的体积。
        zhuti zt=new zhuti(worker.shape(),gao);
    System.out.println(""+zt.volume());
    }
    
        }
    
    }

    运行结果:

  • 相关阅读:
    Creating fields using CSOM
    WCF Misconfiguration: Security Not Enabled
    WCF Misconfiguration: Insufficient Audit Failure Handling
    ASP.NET Misconfiguration: Excessive Session Timeout
    ASP.NET Misconfiguration: Missing Error Handling
    ASP.NET Misconfiguration: Request Validation Disabled
    ASP.NET Misconfiguration: Debug Information
    MIME Sniffing
    web.config中的HttpCookie.HttpOnly属性
    How to: Convert Between Various String Types
  • 原文地址:https://www.cnblogs.com/chen4635/p/11594965.html
Copyright © 2011-2022 走看看