首页 > 题库 > 职业考证 > 软考 > 软件设计师 > 案例题

阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
在某系统中,类Interval代表由下界(lower bound)和上界 (upper bound) 定义的区间,要求采用不同的格式显示区间范围,如[lower bound,upper bound]、[lower bound…upper bound]、[lower bound - upper bound]等。现采用策略 (strategy) 模式实现该要求,得到如图5-1所示的类图。


【Java代码】
import java.util.*;


enum TYPE { COMMA, DOTS, LINE};

interface PrintStrategy {
    public        (1)      ;

}

class Interval {
    private double lowerBound;
    private double upperBound;
    public Interval(double p_lower, double p_upper) {
            lowerBound = p_lower; upperBound = p_upper;

    }
    public void printInterval( PrintStrategy   ptr ) {
                 (2)      ;

    }
    public double getLower() {   retun lowerBound;    }
    public double getUpper() {   return upperBound;   }

}

class PrintIntervalsComma implements PrintStrategy {
    public void doPrint(Interval val) {
        System.out.println("[" + val.getLower() + "," + val.getUpper() + "]");

    }  

}

class PrintIntervalsDots implements PrintStrategy{
    public void doPrint(Interval val) {
        System.out.println("[" + val.getLower() +"…" + val.getUpper() + "]");

    } 

}

class PrintIntervalsLine implements PrintStrategy {
    public void doPrint(Interval val) {
           System.out.println("[" + val.getLower() +"-" + val.getUpper() + "]");

     }  

}

class Strategy{
     public static PrintStrategy getStrategy(TYPE type) {
          PrintStrategy st = null;
          switch(type) {
              case COMMA:
                        (3)     ;
                  break;
              case DOTS:
                        (4)     ;
                  break;
              case LINE:
                        (5)     ;
                  break;

            }
           return st;

      }

      public static void main(String[] args) {
          Interval a = new Interval(1.7,2.1);
          a.printInterval(getStrategy(TYPE.COMMA));
          a.printInterval(getStrategy(TYPE.DOTS));
          a.printInterval(getStrategy(TYPE.LINE));

       }   

}


参考答案: 查看答案 查看解析 查看视频解析 下载APP畅快刷题

相关知识点试题

相关试卷