赞
踩
试题五(共1 5 分)
阅读下列说明和Java码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
在某系统中,类interval代表由下界(lower bound)和上界(upper bound)定义的区间。要求采用不同的格式显示区间范围。如[lower bound,upper bound];[lower bound…upper bound];[lower bound-upper bound]等。现采用策略(strategy)模式实现该要求
【Java代码】
import Java.util.enum
TYPE{COMMA, Dots, LINE}
interface PrintStrategy{
public (1);
(1):void doPrint(interval val)
}
class Intervals{
//两个类成员变量=上下边界值
public void Intervals(double lower, double upper){
//初始化上下边界值
}
public double getLower {return lower...}
public double getUpper {return upper...}
public void printInterval (PrintStrategy pr){
(2);
}
}
class PrintIntervalsComma implements PrintStrategy{
public void doPrint (interval Val){
system.out.printin("["+val.getLower()+","+val.getUpper()+"]"}
}
class PrintIntervalsDots implements PrintStrategy{
public void do print (interval val){
system.out.printin("["+val(.getLower()+"..."+val.getUpper()+"]";}
}
class PrintIntervalsLine implements PrintStrategy{
public void do print (interval val){
system.out.print("["+val.getLower()+"-"+val.getUpper()+"]";}
}
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){
Intervals a = new Intervals(1.7, 2.1);
a.printInterval (getStrategy(TYPE.COMMA));
a.printInterval (getStrategy(TYPE.Dots));
a.printInterval (getStrategy(TYPE.LINE));
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。