软件构造课程实验总结1
实验一:
Fundamental Java Programming and Testing
1.实验指导:
2.某些函数:
2.1.判断某一文件里的数据是否为幻方:
public static boolean isLegalMagicSquare(String fileName) {
try {
BufferedReader in =
new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line = in.readLine();
String[] Line = line.split("\t");
int n = Line.length, m = 0;
int[][] number = new int[n][n];
for (int i = 0; i < n; i++) {
number[0][i] = Integer.valueOf(Line[i]);
}
for (int j = 1; j < n; j++) {
line = in.readLine();
Line = line.split("\t");
if ((Line.length != n) || (j == n - 1 && (line = in.readLine()) != null)) {
System.out.println(fileName + ": 不符合n*n矩阵定义!");
in.close();
return false;
}
for (int i = 0; i < n; i++) {
number[j][i] = Integer.valueOf(Line[i]);
if (number[j][i] <= 0) {
System.out.println(fileName + ": 内含不为正整数的数据!");
in.close();
return false;
}
}
}
in.close();
for (int i = 0; i < n; i++) {
m += number[0][i];
}
for (int i = 0; i < n; i++) {
int a1 = 0, a2 = 0;
for (int j = 0; j < n; j++) {
a1 += number[i][j];
a2 += number[j][i];
}
if (a1 != m || a2 != m)
return false;
}
int a3 = 0, a4 = 0;
for (int i = 0; i < n; i++) {
a3 += number[i][i];
a4 += number[i][n - 1 - i];
}
if (a3 != m || a4 != m)
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
2.2.给定一组点,计算凸包,包含所有的凸包:
/**
* Given a set of points, compute the convex hull, the smallest convex set that contains all the
* points in a set of input points. The gift-wrapping algorithm is one simple approach to this
* problem, and there are other algorithms too.
*
* @param points a set of points with xCoords and yCoords. It might be empty, contain only 1
* point, two points or more.
* @return minimal subset of the input points that form the vertices of the perimeter of the
* convex hull
*/
public static Set<Point> convexHull(Set<Point> points) {
Set<Point> Points = new HashSet<Point>();
if (points.isEmpty())
return Points;
boolean[] inPoints = new boolean[points.size()];
Point[] allPoints = points.toArray(new Point[points.size()]);
int i, origin = 0, endpoint;
double minX = allPoints[0].x();
for (i = 1; i < allPoints.length; i++) {
if (allPoints[i].x() < minX) {
minX = allPoints[i].x();
origin = i;
}
if (allPoints[i].x() == minX && allPoints[i].y() < allPoints[origin].y()) {
minX = allPoints[i].x();
origin = i;
}
}
endpoint = origin;
double addAngle = 0;
do {
int nowpoint = endpoint;
double minAngle = 360;
for (i = 0; i < allPoints.length; i++) {
double angle = calculateBearingToPoint(addAngle, (int) allPoints[endpoint].x(),
(int) allPoints[endpoint].y(), (int) allPoints[i].x(),
(int) allPoints[i].y());
if (!inPoints[i] && angle < minAngle && i != endpoint) {
minAngle = angle;
nowpoint = i;
}
if (!inPoints[i] && angle == minAngle && i != endpoint) {
double distance1 = Math.pow(allPoints[nowpoint].x() - allPoints[endpoint].x(),
2) + Math.pow(allPoints[nowpoint].y() - allPoints[endpoint].y(), 2);
double distance2 = Math.pow(allPoints[i].x() - allPoints[endpoint].x(), 2)
+ Math.pow(allPoints[i].y() - allPoints[endpoint].y(), 2);
if (distance1 < distance2) {
minAngle = angle;
nowpoint = i;
}
}
}
addAngle += minAngle;
endpoint = nowpoint;
inPoints[endpoint] = true;
} while (endpoint != origin);
inPoints[origin] = true;
for (i = 0; i < allPoints.length; i++) {
if (inPoints[i])
Points.add(allPoints[i]);
}
return Points;
}
2.3.根据推文中的证据,猜猜谁可能会跟随谁:
/**
* Guess who might follow whom, from evidence found in tweets.
*
* @param tweets a list of tweets providing the evidence, not modified by this method.
* @return a social network (as defined above) in which Ernie follows Bert if and only if there
* is evidence for it in the given list of tweets. One kind of evidence that Ernie
* follows Bert is if Ernie
* @-mentions Bert in a tweet. This must be implemented. Other kinds of evidence may be used at
* the implementor's discretion. All the Twitter usernames in the returned social
* network must be either authors or @-mentions in the list of tweets.
**/
public static Map<String, Set<String>> guessFollowsGraph(List<Tweet> tweets) {
Map<String, Set<String>> followsGraph = new HashMap<String, Set<String>>();
for (int i = 0; i < tweets.size(); i++) {
Set<String> follows = new HashSet<String>();
int j = 0, start = 0, end = 0;
do {
start = tweets.get(i).getText().indexOf("@", j);
for (end = start; (start != -1)
&& (tweets.get(i).getText().charAt(end) != ' '); end++);
if (start != -1)
follows.add(tweets.get(i).getText().substring(start + 1, end));
j = end;
} while (start != -1);
followsGraph.put(tweets.get(i).getAuthor(), follows);
}
List<Set<String>> hashtags = new ArrayList<Set<String>>();
for (int i = 0; i < tweets.size(); i++) {
int j = 0, start = 0, end = 0;
do {
start = tweets.get(i).getText().indexOf("#", j);
for (end = start; (start != -1)
&& (tweets.get(i).getText().charAt(end) != ' '); end++);
if (start != -1) {
if (hashtags.size() <= i)
hashtags.add(new HashSet<String>());
hashtags.get(i).add(tweets.get(i).getText().substring(start + 1, end));
}
j = end;
} while (start != -1);
}
for (int i = 0; i < tweets.size(); i++) {
for (String s : hashtags.get(i)) {
for (int j = i + 1; j < tweets.size(); j++) {
if (hashtags.get(j).contains(s)) {
followsGraph.get(tweets.get(i).getAuthor()).add(tweets.get(j).getAuthor());
followsGraph.get(tweets.get(j).getAuthor()).add(tweets.get(i).getAuthor());
}
}
}
}
return followsGraph;
}