软件构造lab1

目 录
1 实验目标概述 1
2 实验环境配置 1
3 实验过程 1
3.1 Magic Squares 1
3.1.1 isLegalMagicSquare() 1
3.1.2 generateMagicSquare() 1
3.2 Turtle Graphics 1
3.2.1 Problem 1: Clone and import 2
3.2.2 Problem 3: Turtle graphics and drawSquare 2
3.2.3 Problem 5: Drawing polygons 2
3.2.4 Problem 6: Calculating Bearings 2
3.2.5 Problem 7: Convex Hulls 2
3.2.6 Problem 8: Personal art 2
3.2.7 Submitting 2
3.3 Social Network 2
3.3.1 设计/实现FriendshipGraph类 2
3.3.2 设计/实现Person类 2
3.3.3 设计/实现客户端代码main() 2
3.3.4 设计/实现测试用例 3
3.4 Tweet Tweet 3
3.4.1 Problem 1: Extracting data from tweets 3
3.4.2 Problem 2: Filtering lists of tweets 3
3.4.3 Problem 3: Inferring a social network 3
3.4.4 Problem 4: Get smarter 3
4 实验进度记录 3
5 实验过程中遇到的困难与解决途径 3
6 实验过程中收获的经验、教训、感想 4
6.1 实验过程中收获的经验和教训 4
6.2 针对以下方面的感受 4

1实验目标概述
本次实验通过求解四个问题(其中一个可选),训练基本Java编程技能,能够利用Java OO开发基本的功能模块,能够阅读理解已有代码框架并根据功能需求补全代码,能够为所开发的代码编写基本的测试程序并完成测试,初步保证所开发代码的正确性。另一方面,利用Git作为代码配置管理的工具,学会Git的基本使用方法。
?基本的Java OO编程
?基于Eclipse IDE进行Java编程
?基于JUnit的测试
?基于Git的代码配置管理

2实验环境配置
简要陈述你配置本次实验所需开发、测试、运行环境的过程,必要时可以给出屏幕截图。
特别是要记录配置过程中遇到的问题和困难,以及如何解决的。
1.JDK配置
下载JDK,并解压到D:JDK ???
Win10 下环境变量Path的配置 ???
验证是否配置成功

在Eclipse中运行java 程序

3.GIT配置
下载安装,运行

3实验过程
请仔细对照实验手册,针对四个问题中的每一项任务,在下面各节中记录你的实验过程、阐述你的设计思路和问题求解思路,可辅之以示意图或关键源代码加以说明(但无需把你的源代码全部粘贴过来!)。
为了条理清晰,可根据需要在各节增加三级标题。
3.1Magic Squares
在这里简要概述你对该任务的理解。
任务:
n阶幻方的安排n×n数字,通常不同的整数,在广场,n在所有行,所有列,对角线和相同的常数,在main()函数中调用五次isLegalMagicSquare()函数,将5个文本文件名分别作为参数输入进去,看其是否得到正确的输出(true, false)。需要能够处理输入文件的各种特殊情况,例如:文件中的数据不符合Magic Square的定义(行列数不相等、并非矩阵等)、矩阵中的某些数字并非正整数、数字之间并非使用t分割等。若遇到这些情况,终止程序执行(isLegalMagicSquare函数返回false),并在控制台输出错误提示信息。
将generateMagicSquare产生的magic square写入文件srcP1txt6.txt中;当输入的n不合法时(n为偶数、n为负数等),不要该函数抛出异常并非法退出,而是提示错误并“优雅的”退出——函数输出false结束。利用前面已经写好的isLegalMagicSquare()函数,在main()函数判断该函数新生成的文本文件6.txt是否符合magic square的定义。

3.1.1isLegalMagicSquare()
按步骤给出你的设计和实现思路/过程/结果。
给出代码,设计思想均有注释
/先读取文件最大行数和列数,判断是否相等,若相等生成nn矩阵,初始化为0,加入数据,[2n+2]数组存储行之和列之和以及对角线之和,添加时判断是否为正整数

  • 添加数据后矩阵存在0也不符合要求

*/
public static boolean isLegalMagicSquare(String fileName) {

3.1.2generateMagicSquare()
按步骤给出你的设计和实现思路/过程/结果。
设计思路:
/奇数矩阵时依次填入1到n^2,第一个填在第一行正中间,依次向上和向右移动一位,顺序填写,第一行向上会到最后一行,最右向右会到最左,每填n个数,下一个数填在该数下面
直至填完.以3为例子,假设没填的该位置是0,如图
0 1 0 | 0 1 0 | 0 1 0 | 0 1 0 | 0 1 0 | 0 1 6 | 0 1 6 | 8 1 6 | 8 1 6
0 0 0 | 0 0 0 | 3 0 0 | 3 0 0 | 3 5 0 | 3 5 0 | 3 5 7 | 3 5 7 | 3 5 7
0 0 0 | 0 0 2 | 0 0 2 | 4 0 2 | 4 0 2 | 4 0 2 | 4 0 2 | 4 0 2 | 4 9 2
/
public static void generateMagicSquare(int n) throws IOException {
if (n % 2 == 0 || n
System.out.println(“输入错误”);
System.exit(-1);
}

分析异常 不能为偶数,分n批,每批写n个,每批结束时不能在最后一行,因为row会++,造成数组越界,而函数第一行开始,注定偶数行为结束行,因此不能为偶数
// 数组创建时元素个数不能为负数

3.2Turtle Graphics
在这里简要概述你对该任务的理解。
任务:熟悉junit,对要求函数进行补全

3.2.1Problem 1: Clone and import
从GitHub获取该任务的代码、在本地创建git仓库、使用git管理本地开发。
3.2.2Problem 3: Turtle graphics and drawSquare
turtle前进sidelength,右转90度,循环四次.即为正方形
3.2.3Problem 5: Drawing polygons
通过多边形外角和为360度这一性质,已知边数和边长,利用180.00 * (sides – 2) / sides可求得内角.turtle前进sidelength,右转内角度数,循环边数次.即为该正多边形

3.2.4Problem 6: Calculating Bearings
不断获得一个点和下一个点坐标,循环xCoords.size() – 1次,每次n保存和上一条边偏移量,m保存该与y轴的偏移量,调用n=calculateBearingToPoint(m, x1, y1, x2, y2),m=m+n;list不断添加n即可得到list
3.2.5Problem 7: Convex Hulls
1.选取最右下角的点
2.找到第二个点连上前一个点与x轴逆时针旋转夹角最小的一个点,加入集合,依此类推
3.循环时判断是否下一个点和第一个点旋转角度相等,若相等,则结束.
4.返回集合
3.2.6Problem 8: Personal art
turtle.color(PenColor.BLUE);
for (int i = 0; i
turtle.turn(30);
turtle.forward(50);
turtle.turn(120);
turtle.forward(50);
turtle.turn(300);
}
可以画出一个蓝色的飞镖形状
3.2.7Submitting
如何通过Git提交当前版本到GitHub上你的Lab1仓库。
使用命令git push -u origin master
3.3Social Network
在这里简要概述你对该任务的理解。
任务:建立一个图,通过输入搭建有向社交网,并且给出正确的输出,对非法输入进行正确的判断。
3.3.1设计/实现FriendshipGraph类
给出你的设计和实现思路/过程/结果。
private List nameList = new ArrayList(); // 名字列表,邻接表的展示结构,效率较高。在List类型内装入Person类型,而且每一个Person内有List存储每一个人的序号。
private boolean existName(Person name) // 判断是否graph中存在
public boolean addVertex(Person name) // 向图中添加成员
public boolean addEdge(Person name1, Person name2) //加上关系
public int getDistance(Person name1, Person name2) //获得距离(bfs)
3.3.2设计/实现Person类
给出你的设计和实现思路/过程/结果。
private String name; // person的名字
private List id = new ArrayList(); // 和该person有关的其他person序号list
public String getName() // 获得名字
public List getId() // 为获得有关列表
public boolean existId(int id) // 判断是否关系已存在
public void Setid(int a) // 添加关系
public Person(String name) // person名字赋值
3.3.3设计/实现客户端代码main()
给出你的设计和实现思路/过程/结果。
public static void main(String[] args) {

//should print 1
System.out.println(graph.getDistance(rachel, ben));
//should print 2
System.out.println(graph.getDistance(rachel, rachel));
//should print 0
System.out.println(graph.getDistance(rachel, kramer));
//should print -1
}

3.3.4设计/实现测试用例
给出你的设计和实现思路/过程/结果。
package P3;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
public class FriendshipTest {
FriendshipGraph graph = new FriendshipGraph();
Person rachel = new Person(“Rachel”);
Person ross = new Person(“Ross”);
Person ben = new Person(“Ben”);
Person kramer = new Person(“Kramer”);
Person tom = new Person(“Tom”);
Person jack = new Person(“Jack”);

}

3.4Tweet Tweet
请自行组织子目录结构。
3.4.1Problem 1: Extracting data from tweets
Timespan getTimespan(List tweets) 的设计思想:a记录最早时间,b记录最晚时间并都初始化为第一个tweet的timetamp,遍历tweets,每次a如果在某一tweet时间后则其替换a;b同理,即可得到最早和最晚时间.
public static Timespan getTimespan(List tweets) { //获得最早和最晚消息
// throw new RuntimeException(“not implemented”);
Instant a = tweets.get(0).getTimestamp();
Instant b = tweets.get(0).getTimestamp();

Set getMentionedUsers(List tweets)的设计思想:遍历tweets,每次获得tweet的text,找到text的@的下标,从@下标开始直到找到不为[a-z]||[A-Z]||-||_,截取这一段,判断大小写以及是否存在,选择加入names集合中
public static Set getMentionedUsers(List tweets) { // 获得所有消息中@的人
// throw new RuntimeException(“not implemented”);
Set names = new HashSet();
String aString;
String bString = null;

3.4.2Problem 2: Filtering lists of tweets
List writtenBy(List tweets, String username)设计思想:遍历tweets,如果username不分大小写和tweet的作者名字相等,则alst添加该条tweet,返回alist
public static List writtenBy(List tweets, String username) { //获得一个用户所有发出去的消息
//throw new RuntimeException(“not implemented”);
List aList= new ArrayList();
for (int i = 0; i
if(username.equalsIgnoreCase(tweets.get(i).getAuthor()))
{
aList.add(tweets.get(i));
}

List inTimespan(List tweets, Timespan timespan) 的设计思想:遍历tweets,如果一条tweet的timetamp在start和end之间,则添加该tweet到alist.返回aslit
public static List inTimespan(List tweets, Timespan timespan) { //获得所有在规定时间的消息
//throw new RuntimeException(“not implemented”);
List aList= new ArrayList();
for (int i = 0; i
if(timespan.getStart().isBefore(tweets.get(i).getTimestamp())&&tweets.get(i).getTimestamp().isBefore(timespan.getEnd()))
{
aList.add(tweets.get(i));
}

}
List containing(List tweets, List words) 的设计思想:
遍历tweets,遍历words,如果该tweet的text不分大小写包含该word,添加该tweet到alist,返回alist.
public static List containing(List tweets, List words) { //获得所有包含关键字的消息
//throw new RuntimeException(“not implemented”);
List aList= new ArrayList();
for (int i = 0; i
for(int j=0;j {
if(tweets.get(i).getText().toLowerCase().contains(words.get(j).toLowerCase()))
{
aList.add(tweets.get(i));
break;
}
}
}
return aList;
}
3.4.3Problem 3: Inferring a social network
Map guessFollowsGraph(List tweets)的设计思想:遍历tweets,获得每个user的tweet列表,从该列表中获得@对象集合,将其加入map
public static Map guessFollowsGraph(List tweets) {
// throw new RuntimeException(“not implemented”);
Map aMap = new HashMap();
List aList = new ArrayList();

List influencers(Map followsGraph) 的设计思想:
建立新map,遍历followsGraph,访问每个user的@的人,在新map的键值对的value上不断++;从而得到一个user和其影响到人数的键值对的map,最后对其降序排序存放key到list中
public static List influencers(Map followsGraph) {
// throw new RuntimeException(“not implemented”);
Map aMap = new TreeMap();
List cList = new ArrayList();
for (String user : followsGraph.keySet()) {
Set aSet = followsGraph.get(user);
for (String bString : aSet) {
if (!aMap.containsKey(bString))
aMap.put(bString, 0);
aMap.put(bString, aMap.get(bString) + 1);

来源:酒自醉
                                                        

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年5月16日
下一篇 2019年5月16日

相关推荐