試題五
閱讀下列說明和Java代碼,將應(yīng)填入 (n)處的字寫在答題紙的對應(yīng)欄內(nèi)
【說明】
在某系統(tǒng)中,類interval代表由下界(lower bound)和上界(upper bound)定義的區(qū)間。要求采用不同的格式顯示區(qū)間范圍。如[lower bound.upper bound]:[lower bound...upper bound];[lower bound-upper bound]等現(xiàn)采用策略(strategy)模式實(shí)現(xiàn)該要求,得到如圖5-1所示的類圖。
圖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 pLower, double pUpper){
lowerBound = pLower;
upperBound = pUpper:
}
public void printInterval(PrintStrategy ps){
(2);
}
public double getLower(){
return lowerBound;
}
public double getUpper(){
return upperBound;
}
}
class PrintIntervalLine implements PrintStrategy {
public void doPrint(Interval val){
Svstem.out.println("["+ val.getLower()+"-"+val.getUppei
0+"]");
}
}
class PrintIntervalDots implements PrintStrategy {
public void doPrint(Interval val){
System.out.println("["+ val.getLower()+".."+ val.getUppe
r()+"]");
}
}
class PrintIntervalComma implements PrintStrategy {
public void doPrint(Interval val){
System.out.println("["+val.getLower()+","+ val.getUpper
0+"]");
}
}
ublic class Client {
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 interval = new Interval(1.7,2.1);
interval printInterval(getStrategy(TYPE.COMMA));
interval printInterval(getStrategy(TYPE.DOTS));
interval printInterval(getStrategy(TYPE.LINE));
}
}