2023.4.17更新:
首先这个是近一年前自己复习时候做的期末答案,最后也考得不错九十多分排名第十好像。
参考了20级计算机群佬们的代码,学长们的火炬答案
---------------------------------
根据具体思路,自己尝试编写了具体的代码仅供参考
(完全不保真哈)
目录
软件构造2018期末
4.
public class NoEnoughPaperException extends Exception
{
public NoEnoughParereECspox(String s)
{
super(s);
}
}
调整方法签名
Paperlist(XXXXXXXXX) throws NoEnoughPaperException
调整17,18行
if(..................)
throw new NoEnoughPaperException("学生希望.................")
修改client,14行
try
{
XXXXXXXXXXx
}
catch(NoEnoughPaperException e)
{
soutv(e.getmessage());
}
5.1
interface Strategy
{
void selectPaperByStrategy(List<Paper> availiblePaper,Stu stu)
}
class RandomStrategy implements Strategy
{
@ovveride
void selectPaperByStrategy(List<Paper> availiblePaper,Stu stu){
}
}
将原selectPaperByRandonm处的15行
改为:selectPaper(Strategy strategy,Stu stu)
19行变为:List<Paper> papers=strategy.selectPaperByStrategy(availablepaper,stu);
大概:
public list<Paper> selectPaperByStrategy(Student student,Strategy strategy)
{
...........
........................
List<papers> = strategy.selectPaperByStrategy(availiblePaper,stu) ;
............
.........
}
原来listOfRainy.selectPaperByRandom(stu);
现在listOfRainy.selecyPaper(new XXXStrategy(),stu);
在client,14行改为:
listOfRainy.selectPaper(new RandomStrategy(),stu);
可使用随机策略
改为listOfRainy.selectPaper(new XXXXXXXXtrategy(),stu);
可改为XXX策略
5.2
interface Visitor
{
int visit()
}
public class ConcreteVisitor1 implements Visitro
{
@override
XXXXXXXXXXx
}
public class SelectedOneVisitor implements
{
@override
}
interface Element
{
int accept(Visitor visitor)
}
PaperSelection implements Element
{
@override
int accept(Visitor visitor)
{
return visitor.visit(this)
}
}
//接口可省略直接写出
PaperSelection
{
int accept(Visitor visitor)
{
return visitor.visit(this)
}
}
在Client中
int num =listOfrainy.getselectionResult.accept(new SelectedOneVisitor());
软件构造2019期末
*param
*param
*
public parkingFildFactory(Stinrg name,int lotsNumber,Set<lot> lots,Map<Lot,Car> status) throws IllegalArgumentException
{
if(name.length==0||lotsNumber<5)
throw new IllegalArgumentException("参数不正确");
else
{
return new concreteParkingField(name,lotsNumber,lots,status);
}
}
static ParkingField create(String name, int[] lotnos, int[] width) {
if ( width.length < 5 || lotnos.length<5)
throw new IllegalArgumentException("输入数组长度应大于等于5!");
if (width.length!=lotnos.length) {
throw new IllegalArgumentException("停车位编号数目应同宽度数目一致!");
}
Set<Integer> testDuplicationLotNo = new HashSet<>();
for(int lotno:lotnos)
testDuplicationLotNo.add(lotno);
if (testDuplicationLotNo.size()!=lotnos.length)
throw new IllegalArgumentException("停车位编号中存在重复!");
return new ConcreteParkingField(name, lotnos, width);
//RI:
name长度大于0,lotsNumber车位至少为5,lots不同停车位对象的编号不能相同,status且 Key 和 Value 的值均不能为 null,
private void checkRep()
{
assert name.length>0;
assert lotsNumber>=5;
for(Lot lot:lots)
{
for(Lot lot1:lots)
{
assert(lot1==lot ||lot1.getLotNumber()!=lot.getLotNumber())
}
}
assert lots.size==lotsNumber;
assert status.containsKey(null)==false;
assert status.containsValue(null)==false;
}
第四题装饰者
ParkingFiledDecorator用抽象类实现ParkingField然后再concreteParkingFieldDecorator继承了ParkingFiledDecorator,oncreteParkingFieldDecorator重写了方法,增加属性修改方法
abstract Class ParkingFiledDecorator implements ParkingField
{
ParkingField parkingfield
public ParkingFiledDecorator(ParkingField parkingfield)
{
this.parkingfield = parkingfield;
}
public
.........
}
class concreteParkingFieldDecorator extends ParkingFiledDecorator
{
Stirng company;
public concreteParkingFieldDecorator(parkingfield,String company)
{
super();
this.company = company;
}
@Override
public void parking(..)
{
super.parking();
soutv("欢迎停车");
}
@Override
public void departure(..)
{
super.parking();
soutv("欢迎下次光临");
}
}
ParkingField pf = ...; //此处无需补充,假设 pf 已成功创建
ParkingField pfWithCompany = concreteParkingFieldDecorator(pf,company);
4-2
1.在ParkingField里面和ConcreteParkingField添加accept()方法
public double accept(Visitor visitor)
{
visitor.visit(this);
}
2.在abstract clsss ParkingFieldVisitor抽象类,方法有visit
{
double visit(ConcreteParkingField cpf);
}
3.CaclulateVisitor extends ParkingFieldVisitor具体实现类然后实现了方法visit,visit内部加入计算方法
{
public double visit(ConcreteParkingField cpf)
{
return cpf.getstatus.keySet.size()/cpf.getlotsNumber();
}
}
(2)
ParkingField pf = ...; //此处无需补充,假设 pf 已成功创建
//并且进行了一系列 parking 和 departure 操作
double fullRatio = pf.accept(new CaclulateVisitor());
5-1
PF::='<' ParkingName ',' lotsNumber',' companyName
ParkingName::=word (' ' word)*
lotsNumber::=[5-9] | [1-9] [0-9]+
word::=[a-zA-z0-9]+
companyName::='' | word (' 'word)*
正则:<[a-zA-Z0-9]+(' '[a-zA-Z0-9]+)*,([5-9]|[1-9]\d+),(''|\w(''\w)*)
5-2
ConcreteParkingField要implements Iterable<Car>
{
public Iterator<Car>()
{
return new ConcreteParkingFieldIterator();
}
class ConcreteParkingFieldIterator impelemnts Iterator<Car>
{
private int size = status.size();
private int index;
private int curNumber=Integer.MINVAlUE
priavet INT nextNumber;
public boolean hasNext()
{
return index<size-1;
}
public Car next()
{
for(Lot lot:status.keySet())
{
if(lot.number>curNumber)
nextNumber = Min(nextNumber,lot.number);
}
index++;
curNumber=nextNumber
return sttus.get(curNumber);
}
}
}
6-1
public void parking(Car c, int num) throws LotOccupiedException, NoSuchLotException,
LotTooNarrowException, CarAlreadyParkingException
{
if(status.keySet().contain(num))
throw LotOccupiedException;
for(Car car:status.keyValue())
{
if(car.getwidth)
}
}
软件构造2021期末
4.
([A-Z][a-z]*\((-1|0|1)\))+
5.
ConcretePoll
RI:
candidates.size()>=1
candidates内部的候选人名字满足由字母构成,首字母大写,
不含空格
maxSupportNUm>=1,<=cadidates.size
votes覆盖了所有候选人,不能包含不在本次候选的人,只有-1,0,1,
值为1的数目不能超过允许支持的数目
Vote:RI
votes覆盖了所有候选人,不能包含不在本次候选的人,只有-1,0,1,
值为1的数目不能超过允许支持的数目
candidates.size()>=1
candidates内部的候选人名字满足由字母构成,首字母大写,
不含空格
score只能是-1,0,1
6.
可能存在表示泄露
//Safety from rep exposure
//所有字段均为private
//采用防御性拷贝,对于getVotesByCandidate返回一个拷贝,对于list
也返回一个拷贝
7.利用策略方法:
interface Strategy()
{
void getVoteResult()
}
public class Strategy1 implements Starategy
{
//情况1
@ovverride
void getVoteResult()
}
public class Strategy2 implements Starategy
{
//情况2
}
直接修改元方法
Map<STring,DOuble> getVoteResult(Strategy strategy)
{
strategy.getVoteResult()
}
//
public class Context {
private Strategy strategy;
public Context(Strategy strategy){
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2){
return strategy.doOperation(num1, num2);
}
}
关于装饰模式
装饰模式(Decorator Pattern):动态地给一个对象增加一些额外的职责,就增加对象功能来说,装饰模式比生成子类实现更为灵活。装饰模式是一种对象结构型模式。
//在原方法上多出一些代码***
//这里使用new ConcreteDecorator(concreteElement).operation(),访问者是concreteElment.accept(concreteVisitor);
装饰模式是装饰某个具体对象出一个新对象
//利用委托,然后调用比如额外多print一些*****
//没有改变源代码,而是要“扩展”
//抽象类 Decorator implements Elments装饰类要算作原来Shape(ELment)一类
对每一个特性构造子类,通过委派机制增加到对
象上
//可以理解ParkingFieldWithCompany(Decorator)是原来ParkingField的子类,自行多出了一个属性
ParkingField pf = ParkingField.create(lots);
ParkingField pfwc = new ParkingFieldWithCompany(pf, "HIT_CS");