EclEmma的安装:
-
启动eclipse—点击Help菜单—Install New Software,在弹出的对话框中,点击Add
-
输入Name,例如EclEmma。输入Location:http://update.eclemma.org/
-
安装后重新启动eclipse,如果成功安装,工具栏上会出现一个新的按钮
-
安装成功则出现下图标志:
Poetic Walks
-
首先,这个问题是要我们自己做一个抽象数据类型“图”,完成他的功能并写出测试用例
-
mutable graph 点和边可以被加到图中
directed edges 边可以从一个与源点连接到目标点
weighted edges 设置边的权重
labeled vertices,将点用一些不可变的变量来标记,如他们可能有String names or Integer IDs
GraphPoet,功能:写诗
Test Graph <String>
方法 | 实现 |
---|---|
boolean add(L vertex) | 返回是否加入点,若以前存在返回false,否则向点集里加这个点,同时返回true |
int set(L source, L target, int weight) | 若权值为0,同时两点之间有有向边,则删除这条边;若为非0权值,则根据有向边是否存在实现加边和修改权值,返回的是原来两点间边的权值 |
boolean remove(L vertex) | 将点删除,若点集存在该点,删除,返回true,否则返回false |
Set<L>vertices() | 返回图的点集 |
Map<L,Integer> sources(L target) | 返回以传入参数为终点的边,并将他们写进map中,key为起点,value为权值 |
Map<L,Integer> targets(L source) | 返回以传入参数为起点的边,并将他们写进map中,key为终点,value为权值 |
public String toString() | 将图用字符串描述 |
所以,我们可以根据方法的不同功能写出测试用例,结果如下:
Implement Graph <String>
Implement ConcreteEdgesGraph
- 要求:使用private final Set vertices = new HashSet<>()、private final List edges = new ArrayList<>(),edge为不可变的
- 我的checkrep要求每两个点之间最多只有两条有向边,同时,权值为整数,边的起点和终点不重合
- 用题中给的域来写方法
- 因为是实现抽象类,所以大部分函数需要重写
- Edge 类
域 | 作用 |
---|---|
private final L source | 边的起点 |
private final L target | 边的终点 |
private final int weight | 边的权值 |
方法 | 作用 |
---|---|
public L getsource() | Getter |
public L gettarget() | Getter |
public int getweight() | Getter |
- ConcreteEdgesGraph
方法 | 作用 |
---|---|
boolean add(L vertex) | 遍历点集 |
int set(L source, L target, int weight) | 首先遍历边,如果两点之间有边,同时判断权值大小,进行删除(权值变为0),删除原来的边,加上新的边,若两点间没边,判断两个点是否在点集中,没有要在点集中加上,然后向边集加上边,最后我们要调用checkrep函数,最后返回之前两点间的权值 |
boolean remove(L vertex) | 遍历点集,删除(迭代器删除) |
Set<L> vertices() | 通过遍历边集,向点集中加点 |
Map<L,Integer> sources(L target) | 遍历边集,找到以传入参数为终点的边,记录起点和边的权值 |
Map<L,Integer> targets(L source) | 遍历边集,找到以传入参数为起点的边,记录终点和边的权值 |
public String toString() | 将图用字符串描述 |
- 测试
Implement ConcreteVerticesGraph
-
同上面一样,只是通过点来实现Graph抽象类
-
域
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190616001250196.png -
Vertex类
域 | 作用 |
---|---|
private L source | 边的起点 |
private Map<L, Integer> map | Key:边的终点 Value:边的权值 |
方法 | 作用 |
---|---|
L getsource() | Getter |
Map<L,Integer>getmap() | Getter |
putmap(L target,int weight) | 将边加到map去 |
removemap(L target) | 通过删除map来删除边 |
replacemap(L target,int weight) | 将边的权值改变 |
toString | 用字符串表示 |
- ConcreteVerticesGraph
方法 | 作用 |
---|---|
boolean add(L vertex) | 遍历点集 |
int set(L source, L target, int weight) | 首先遍历边,如果两点之间有边,同时判断权值大小,进行删除(权值变为0),删除原来的边,加上新的边,若两点间没边,判断两个点是否在点集中,没有要在点集中加上,然后向边集加上边,最后我们要调用checkrep函数,最后返回之前两点间的权值 |
boolean remove(L vertex) | 遍历点集,删除(迭代器删除) |
Set<L>vertices() | 通过遍历边集,向点集中加点 |
Map<L,Integer> sources(L target) | 遍历边集,找到以传入参数为终点的边,记录起点和边的权值 |
Map<L,Integer> targets(L source) | 遍历边集,找到以传入参数为起点的边,记录终点和边的权值 |
Implement ConcreteVerticesGraph
同上面一样,只是通过点来实现Graph抽象类
Vertex 类
域 | 作用 |
---|---|
private L source | 边的起点 |
private Map<L, Integer> map | Key:边的终点 Value:边的权值 |
方法 | 作用 |
– | – |
L getsource() | Getter |
Map<L,Integer>getmap() | Getter |
putmap(L target,int weight) | 将边加到map去 |
removemap(L target) | 通过删除map来删除边 |
replacemap(L target,int weight) | 将边的权值改变 |
toString | 用字符串表示 |
ConcreteVerticesGraph类
方法 | 作用 |
---|---|
boolean add(L vertex) | 遍历点集 |
int set(L source, L target, int weight) | 首先遍历map,如果两点之间有边,同时判断权值大小,进行删除(权值变为0),删除原来的边,加上新的边,若两点间没边,判断两个点是否在点集中,没有要在点集中加上,然后向map加上边,最后我们要调用checkrep函数,最后返回之前两点间的权值 |
boolean remove(L vertex) | 遍历点集,删除(迭代器删除) |
Setvertices() | 通过遍历点集,加集合加点 |
Map<L,Integer> sources(L target) | 遍历map,找到以传入参数为终点的边,记录起点和边的权值 |
Map<L,Integer> targets(L source) | 遍历边集,找到以传入参数为起点的边,记录终点和边的权值 |
public String toString() | 将图用字符串描述 |
Implement generic Graph
Make the implementations generic
使graph泛型化:
- 声明:
public class ConcreteEdgesGraph implements Graph { … }
class Edge { … }
public class ConcreteVerticesGraph implements Graph { … }
class Vertex { … } - 修改标签,用占位符L替代String
Implement Graph.empty()
Poetic walks
- 用上述图来写诗
- 要求:
从文件读入,作为一个图,计算词和词的有向关系,相邻的词之间表示有边,我在这里设置权值为1
从控制台输入一句话,找到一个bridge,与这句话中的相邻两个词的权值最大,并把bridge插入到这两个词中间,输出这句话,要求输入的话按照原来的大小写输出,bridge按小写输出
Test GraphPoet
Implement GraphPoet
- 建立图:从文件读入,然后通过split 拆分成不同的词,将词转换成小写加入到图的点集里,在扫描文集,建立点与点之间的邻接关系
- 从控制台读入,找到可以建立最长路径的两个点和Bridge,修改字符串,输出
Re-implement the Social Network in Lab1
FriendshipGraph类
- 首先,我们完成addVertex,这里我把图设定为Graph型,现在代码如下
- 然后,我们完成addEdge,这个函数是增加点之间的边,代码如下:
- 最后,我们完成getDistance,其实这块就是将图的BSF的算法用Java实现,(先广搜索:从一个点出发,遍历与此点邻接的所有点,再从邻接的第一个点搜下去),所以我们可以得到两个人所在的数的高度差;
代码如下:
Person类
我们针对person类的对象就只用了姓名这个方法,所以这块我们person的实现就比较简单。
客户端main()
Playing Chess
ADT设计/实现方案
- Action
域 | 作用 |
---|---|
private Board board | 棋盘 |
private Player player1 | 玩家1 |
private Player player2 | 玩家2 |
方法 | 作用 |
---|---|
Setter | 设置棋盘,玩家1,玩家2 |
Getter | 得到棋盘,玩家1,玩家2的信息 |
setpiece | 向棋盘中添加新棋子 |
movepiece | 将棋子从一个坐标移到另一个坐标 |
removepiece | 提子 |
eatpiece | 吃子 |
- Board:
域 | 作用 |
---|---|
private int boardtype | 棋盘类型0:国际象棋 1:围棋 |
private int boardsize | 棋盘大小 |
private Piece board[][] | 放棋子的二维组 |
方法 | 作用 |
---|---|
Setter | 设置域 |
Getter | 得到域的信息 |
boardavailable | 判断棋盘某个位子是否为空 |
pieceinboard | 判断棋子是否在棋盘上 |
getnumber | 计算某一个玩家在棋盘上的棋子数 |
empty | 将棋盘某个位子清空 |
- Game
- MyChessAndGoGame
游戏主体
5. Piece
6. Player