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

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

     二、代码

    /* 5个形状类分别调用Shape接口;

    柱体类中,定义求体积方法,换底求体积方法;

    工厂类中,通过输入对应形状的字符,创建对应形状类型的对象。矩形用 j表示,圆形用c表示,正方形用z表示,三角形用s表示,梯形用t表示。**/

    形状类

    package ccut;
    
    public interface Shape {
     double getArae();
    }

    5个形状

    package ccut;
    public class Zheng implements Shape {
    
        double bian;
         public Zheng(double bian){
                this.bian =bian;
            
            }
        public double getArae() {
            return bian*bian;
        }
    
    }
    package ccut;
    import java.math.*;
    public class Circle implements Shape {
    double r;
    public Circle(double r){
        this.r=r;
    }
        public double getArae() {
            return Math.PI*r*r;
        }
    
    }
    package ccut;
    import java.math.*;
    public class SanJiaoXing implements Shape {
    
        double a,b,c;
        double p;
        public SanJiaoXing(double a,double b,double c){
            this.a=a;
            this.b=b;
            this.c=c;
        }
        public double getArae() {
            p=(a+b+c)/2;
            
            return Math.sqrt(p*(p-a)*(p-b)*(p-c));
        }
    
    }
    package ccut;
    
    public class TiXing implements Shape {
    double a,b,h;
    public TiXing(double a,double b,double h){
        this.a=a;
        this.b=b;
        this.h=h;
    }
        public double getArae() {
            
            return (a+b)*h/2;
        }
    
    }
    package ccut;
    
    public class Rect implements Shape {
        double chang;
        double kuan;
        public Rect(double chang,double kuan){
            this.chang =chang;
            this.kuan =kuan;
        }
        public double getArae() {
             return chang*kuan;
        }
    
    }

    工厂类

    package ccut;
    import ccut.Shape;
    public class Factory {
        Shape getShape(char c){
            Shape shape=null;
            switch(c){
            case 'j':shape=new Rect(3,4);break;
            case 'z':shape=new Zheng(5);break;
            case 'c':shape=new Circle(4);break;
            case 's':shape=new SanJiaoXing(5,5,6);break;
            case 't':shape=new TiXing (2,3,4);break;
            }
            return shape;
        }
        
    }

    柱体类

    package ccut;
    import ccut.Shape;
    public class ZhuTi {
    double height;
    Shape shape;
     public ZhuTi(Shape shape,double height){
         this.height=height;
        this.shape=shape;
      
    }
      void setShape(Shape shape){
         this.shape=shape;
     }
     public double getV(){
         return shape.getArae()*height;
     }
    }

    主类

    package ccut;
    import ccut.ZhuTi;
    import ccut.Factory;
    import java.util.*;
    public class Test {
       public static void main(String[] args) {
        
      Scanner r =new Scanner(System.in);
      System.out.println("请输入底的类型:");
      char c=r.next().charAt(0);
      Factory factory=new Factory();
      factory.getShape(c);
      ZhuTi zhuti=new ZhuTi(factory.getShape(c),5);
    System.out.println("体积为:"+zhuti.getV());
      System.out.println("请输入新的底的类型:");
      c=r.next().charAt(0);
      zhuti.setShape(factory.getShape(c));
      System.out.println("体积为:"+zhuti.getV());
        }
    
    }

    三、运行结果

  • 相关阅读:
    docker容器安装使用
    hashMap学习
    spark运行方式及其常用参数
    java面试题基础
    大数据面试题
    java面试题
    Java四种线程池
    大数据
    pyspark 日期格式
    CMake error:System Error:No such file or directory CMake error:Could not open file for write in copy operation xxxx.ros_Config.cmake.tmp.
  • 原文地址:https://www.cnblogs.com/shanshan3/p/11656337.html
Copyright © 2011-2022 走看看