2020软件构造实验二

1实验目标概述
本次实验训练抽象数据类型(ADT)的设计、规约、测试,并使用面向对象编程(OOP)技术实现ADT。具体来说:
 针对给定的应用问题,从问题描述中识别所需的ADT;
 设计ADT规约(pre-condition、post-condition)并评估规约的质量;
 根据ADT的规约设计测试用例;
 ADT的泛型化;
 根据规约设计ADT的多种不同的实现;针对每种实现,设计其表示(representation)、表示不变性(rep invariant)、抽象过程(abstraction function)
 使用OOP实现ADT,并判定表示不变性是否违反、各实现是否存在表示泄露(rep exposure);
 测试ADT的实现并评估测试的覆盖度;
 使用ADT及其实现,为应用问题开发程序;
 在测试代码中,能够写出testing strategy并据此设计测试用例。
2实验环境配置
Eclipse IDE、EclEmma、JUnit测试
在 Eclipse IDE 中安装配置 EclEmma(一个用于统计 JUnit 测试用例的代码覆盖度的 plugin)直接从Eclipse Market下载安装即可。

在这里给出你的GitHub Lab2仓库的URL地址(Lab2-学号)。
https://github.com/ComputerScienceHIT/Lab2-1180300518

3实验过程
针对以下所有四个任务,请为每个你设计和实现的ADT撰写mutability/ immutability说明、AF、RI、safety from rep exposure。给出各ADT中每个方法的spec。为每个ADT编写测试用例,并写明testing strategy。
3.1Poetic Walks
实现的类型是Graph,带有标记顶点的可变加权有 向图的抽象数据类型。即需要实现添加新节点、添加新边、移除节点、获得所有的节点集合等
3.1.1Get the code and prepare Git repository
如何从GitHub获取该任务的代码、在本地创建git仓库、使用git管理本地开发。
从https://github.com/rainywang/Spring2019_HITCS_SC_Lab2/tree/master/P1中获取实验所需代码
git init
git remote add origin git@github.com:ComputerScienceHIT/Lab2-1180300518.git
git pull origin master
git add .
git commit -m “init”
git push origin master
3.1.2Problem 1: Test Graph
静态方法的测试都在GraphStaticTest.java中,实例方法的测试在GraphInstanceTest.java中。GraphInstanceTest是一个抽象类,而且不能直接运行它,需要首先实现emptyInstance()方法。

testadd() 测试边的添加
testset() 测试加边操作
testremove() 测试是否移除点,以及移除相应的边
testsourse()
testtarget()

3.1.3Problem 2: Implement Graph
按要求实现ConcreteEdgesGraph和ConcreteVerticesGraph
3.1.3.1Implement ConcreteEdgesGraph
实现Edge
Edge需要包含起点,终点和权重,并都用private权限,同时提供get方法和set方法,提供以起点、终点和权值为参数的构造方法。设置顶点集合和边数组存储图的信息。

Edge属性
private final String 起点
private final String 终点
private final int 权值

Edge方法
1、public Edge(String source, String target, int weight)构造方法
2、getSource() 得到起点
3、getTarget() 得到终点
4、getWeight() 得到权值
5、toString() 字符串连接起来

实现ConcreteEdgeGraph
1、add 调用vertices.add添加顶点(若vertices()中已包含vertex,返回false,否则新建一个顶点将其加入vertices)
2、set(String source, String target, int weight)设置一条边,若不存在则添加它(先判断顶点在不在集合中),若为0则删掉。
3、remove遍历所有的点若存在则删除点再删除边,不存在则返回false
4、Set vertices() 返回顶点
5、Map<String, Integer> sources 如果找到了目标的target点,就返回对应的source,如果没找到,返回一个空的map
6、Map<String, Integer> targets 如果找到了目标的sourse点,就返回对应的target,如果没找到,返回一个空的map

ConcreteEdgesGraphTest
只需添加
1、public void toStringtest() ,测试toString的返回值是否能正确规范地表示这张图。
2、public void EdgetoStringtest() 测试Edge中的toString方法,测试其返回值能否正确规范地表示这条边的信息
其他的测试继承父类

3.1.3.2Implement ConcreteVerticesGraph
实现Vertex
Vertex属性
private String vertex 点的名称
private Map<String,Integer> source 所有以vertex为起始节点的边,<起始节点vertex,边的权重>.
private Map<String,Integer> target 所有以vertex为目标节点的边,<目标节点vertex,边的权重>

Vertex方法
1、Vertex构造方法
2、String getName() 得到该点
3、Map<String,Integer> getSource() 存储所有以当前点为终点的边,key表示边的起点,value表示边权
4、Map<String,Integer> getTarget() 存储所有以当前点为起点的边,key表示边的终点,value表示边权
5、public void checkRep() 检查当前点的rep是否非空。
6、String toString() 以字符串的形式直观地展示有哪些边以当前点为终点\起点
7、int setSource(String s, int weight) Vertex V ,设置V的源点返回weight
8、int setTarget(String t, int weight) Vertex V ,设置V的终点返回weight

实现ConcreteVerticesGraph
1、public boolean add(String vertex) 遍历vertice若点不存在则添加返回true,存在则返回false
2、public int set(String source, String target, int weight) 遍历vertice若找到对应的source和target则修改权值否则加入这两点点
3、public boolean remove(String vertex) 遍历vertices中的每一个节点在targes和sources中删除该节点,在vertices中删除该节点.
4、public Set vertices() 拷贝一份vertices到新Set中,返回这个Set
5、public Map<String, Integer> sources(String target) 遍历vertices,如果某个vertex.target包含了target,就建立vertex->vertex.weight的映射并加入Map。
6、public Map<String, Integer> targets(String source) 遍历vertices,如果某个vertex.source包含了source,就建立vertex->vertex.weight的映射并加入Map。
7、 public String toString() 以字符串的形式直观地展示有哪些边以当前的图

实现ConcreteVerticesGraphTest
只需添加
3、public void toStringtest() ,测试toString的返回值是否能正确规范地表示这张图。
4、public void EdgetoStringtest() 测试Edge中的toString方法,测试其返回值能否正确规范地表示这条边的信息
其他的测试继承父类

3.1.4Problem 3: Implement generic Graph
3.1.4.1Make the implementations generic
将类声明改为(类的内部函数参数、返回值、域修改为L)

3.1.4.2Implement Graph.empty()

3.1.5Problem 4: Poetic walks
3.1.5.1Test GraphPoet

3.1.5.2Implement GraphPoet
1、public GraphPoet(File corpus) throws IOException 将文档中句子整行输入,用split分割,每个单词建立顶点,两个相邻单词间添加有向边,权重设为1,若边出现多次则权重为出现的次数
2、public void checkRep() 检查图不空
3、public String poem(String input) 输入部分诗句,获取其中单词通过其target找下一个单词,直到把诗句拼完整。
4、public String toString() 输出

3.1.5.3Graph poetry slam
main方法如下:

3.1.6Before you’re done
请按照http://web.mit.edu/6.031/www/sp17/psets/ps2/#before_youre_done的说明,检查你的程序。
如何通过Git提交当前版本到GitHub上你的Lab2仓库。
在这里给出你的项目的目录结构树状示意图。

3.2Re-implement the Social Network in Lab1
该任务主要是利用任务一中设计的图数据结构,改进lab-1中的社交网络图,由于任务一中已经完成了构建图所需要的工具,在这里主要修改getDistance方法。
3.2.1FriendshipGraph类
选择继承ConcreteEdgesGraph,实现FriendshipGraph中addVertex,addEdge和getDistance三个接口且不改变父类的rep

1、public void addVertex(Person person) 添加顶点
2、public void addEdge(Person a, Person b) 添加边
3、public int getDistance(Person a, Person b) 用BFS算法计算a,b两点间距离
3.2.2Person类
给出名字即可

3.2.3客户端main()
与lab1中一样即可。
3.2.4测试用例
1、正常输入下看返回值是否正确。
2、重名情况
3、无连通情况

3.2.5提交至Git仓库
如何通过Git提交当前版本到GitHub上你的Lab3仓库。
在这里给出你的项目的目录结构树状示意图。

3.3Playing Chess
3.3.1ADT设计/实现方案
设计了哪些ADT(接口、类),各自的rep和实现,各自的mutability/ immutability说明、AF、RI、safety from rep exposure。
必要时请使用UML class diagram(请自学)描述你设计的各ADT间的关系。
1、Position
rep private int x; 横坐标
private int y; 纵坐标
构造方法 public Position(int x, int y)
方法 public void setPosition(int x, int y) 修改横纵坐标
public int getPositionX() 获得横坐标
public int getPositionY() 获得纵坐标
public boolean equal(Position a, Position b) 判断两个position是否相等

2、Piece
rep final private String belong; 棋子是哪个玩家的
final private String type; 棋子的种类围棋有black和white
private int pieceState; 棋子状态 0未放置 1已放置
private Position piecePosition; 棋子的位置
构造方法 public Piece(String belong, String type)
方法 public int getPieceState() 获得棋子的状态
public Position getPiecePosition() 获得棋子的位置
public String getType() 获得棋子的类型
public String getBelong() 获得棋子属于哪个玩家
public void setPieceState(int pieceState) 设置棋子的状态
public void setPiecePosition(int x, int y) 设置棋子的位置

3、Player
rep final private String name; 玩家名字
private Set pieces = new HashSet<>(); 存储玩家的棋子
private List playerList = new LinkedList<>();
存储玩家历史的走棋
构造方法 public Player(String name)
方法 public String getName() 获得玩家名字
public Set getPieces() 获得玩家的棋子
public List getPlayerList() 获得玩家历史棋子步
public boolean addPiece(Piece newPiece) 添加一枚棋子,已经拥有返回false,否则添加并返回true
public boolean removePiece(Piece removePiece) 移除一枚棋子,不存在返回false,否则移除并返回true
public int numberOfpieces() 获取棋盘上属于自己的棋子数
public void addStep(String step) 向playerList域中添加一个走棋步骤

4、Board
rep final private String boardType; 棋盘种类
final private int size; 棋盘大小
private Piece[][] pieces = new Piece[19][19]; 存储棋盘上的棋子
private boolean[][] placed = new boolean[19][19]; 判断棋盘某个位置是否有棋子
构造方法 public Board(String boardType,int size)
方法 public String getBoardType()
public int getSize()
public Piece[][] getBoardPieces()
public Piece getPieces(int x, int y)
public boolean getPlace(int x, int y)
public void setPiece(Piece p, int x, int y)
public void setPlaced(int x, int y)
public void setNotPlaced(int x, int y)

5、Action
rep private Board chessBoard; 棋盘
private Player player1; 玩家1
private Player player2; 玩家2
构造方法 public Action(Board chessBoard, Player player1, Player player2)
方法 public Board getChessBoard()
public Player getPlayer1()
public Player getPlayer2()
public boolean putPiece(Player player, Piece piece, int x, int y)
落子
public boolean movePiece(Player player, int x1, int y1, int x2, int y2)
将棋子从原来位置移到指定位置
public boolean removePiece(Player player, int x, int y) 将棋子从棋盘中移除
public boolean eatPiece(Player player, int x1, int y1, int x2, int y2)
将棋盘(x1,y1)位置的棋子吃掉到对手的(x2,y2)位置的棋子

6、Game
rep private Action gameAction;
private Player player1;
private Player player2;
private Board gameBoard;
private String gameType;
构造方法 public Game(String gameType)
方法 public Player getPlayer1()
public Player getPlayer2()
public Board getGameBoard()
public void initGame(String gameType, String player1, String player2) 初始化
public boolean putPiece(Player player, Piece piece, int x, int y)
public boolean movePiece(Player player, int x1, int y1, int x2, int y2)
public boolean removePiece(Player player, int x, int y)
public boolean eatPiece(Player player, int x1, int y1, int x2, int y2)
public Player getOwner(int x, int y) 给出位置找棋子所属的棋手
public Piece getPiece(int x, int y) 给出位置找棋子
public int getNumberOfPieces(Player player) 获取棋手的在键盘上的棋子数
public String getHistorySteps(Player player) 获得棋手的走棋历史

3.3.2主程序MyChessAndGoGame设计/实现方案
辅之以执行过程的截图,介绍主程序的设计和实现方案,特别是如何将用户在命令行输入的指令映射到各ADT的具体方法的执行。

public void printmenu() 菜单
public void gameMain() 具体操作 switch来划分用户的输入
public static void main(String[] args)

3.3.3ADT和主程序的测试方案
介绍针对各ADT的各方法的测试方案和testing strategy。
介绍你如何对该应用进行测试用例的设计,以及具体的测试过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值