软件构造课程实验总结1

软件构造课程实验总结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;
    }

实验报告:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值