软件构造lab1心得与体会

0x00 序

上一次接触java还是一年前写Android程序的时候,虽说写了不少代码其实本身对java并不熟悉,借此次Lab1的机会也正好学学java。

0x01 Magic Squares (MIT)

  1. 文件读入

有很多种方法,本此实验用到了以下方法

FileReader in = new FileReader(fileName);
BufferedReader br = new BufferedReader(in);
String oneLine;
// read by line
while((oneLine = br.readLine()) != null){
	...
}
  1. 文件读出
File file = new File("src/P1/6.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
   file.createNewFile();
   }
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// write 
bw.write(...);
bw.close();

需要注意参数fileName(字符串)和file(文件)的区别

  1. 其他函数

用法类似,一触即通(split也太香了

//Integer ---> string / ...
Integer.toString(...) 

//check if is digit / ...
Character.isDigit(...) 

// Sring.split
String oneLine;
// split by ...
String[] word = oneLine.split(...);
  1. 数组
// 静态
int[] m = new int[len]
int[][] matrix = new int[wid][len];

// 动态
int []arr
int [][] matrix ;  

// 动态创建
arr = new int[len];
matrix = new int [wid][];     
for ( i = 0 ; i < wid ; i++ ) {  
      matrix [i] = new int [len];      
 }

// ArrayList
ArrayList list = new ArrayList(); 
for( int i=0;i <10;i++ ) 
    list.Add(i);
    

与c语言不同,java的数组全部存放在堆(?)上,堆内存映射二进制0,这也解释了为什么数组数据一开始都是0。

  1. 异常处理

不太理解任务2为什么会自动抛出n不是奇数的异常(代码中并没有异常处理)。
如果是自动触发的为什么读写file的时候得try catch指出IOException…

0x02 TurtleSoup

这题配置环境给我恶心坏了,同时由于周围同学基本上都用eclipse,idea新手用户表示一切都要自己摸索。但当我欣赏着idea舒适的界面与强大的功能时,我明白了我的选择没有错。

  1. 找不到程序包

由于敝校在MIT的程序任务中多加了一层路径P2导致文件全部冒红,将冒红处添上P2.即可,如将下图地址修改为P2.turtle.Action.ActionType
在这里插入图片描述

  1. junit报错

须手动添加junit包
Ans1.直接在File->Project Structure->library->Java中找到idea安装位置下lib文件里的junit-4.12.jar并添加(但助教好像说这样他没发批改)

Ans2.也可以将junit-4.12.jar复制出来,建立lib文件夹并放进去,右键->add to library即可。

同时由于junit-4.11以后不再包含hamcrest包,也得把这个包添加进来。完成后可以在.iml文件中看到下图改变:
在这里插入图片描述
然后就可以使用junit了,同时可以看到idea在一个project中运行多个test\main()是多么方便,即可以run也可以debug。
在这里插入图片描述

  1. calculateBearingToPoint()

统一标准为计算顺时针转过的角度(0-360°),其中用到Math.atan2()函数,可以在此网站中测试。

public static double calculateBearingToPoint(double currentBearing, int currentX, int currentY,
                                                 int targetX, int targetY) {
        if (currentX == targetX && currentY == targetY)
            return 0;
        double targetBearing = Math.atan2(targetX - currentX, targetY - currentY) / Math.PI * 180.00;
        double result = targetBearing-currentBearing;
        return result>=0 ? result : result+360;
    }
  1. 凸包问题

有了calculateBearingToPoint()解决方法就很自然能想到:
①取最左边的点(最左边点有不止一个就取他们中最下面的)
②不断找到顺时针旋转最大的点加入到result中,直至找到了最初的点
如此便构成了一个凸包。

Set元素是无序的,遍历可以有两种方法:

// for 
Set<Point> points;
for(Point p:points)
{
       p....
}

//iterator
Iterator<Point> it = points.iterator();
while(it.hasNext())
{
      Point p = it.next();
      p....
}

但笔者能力有限,如何避免一条直线上中间的点不会被加入闭包的问题还没有得到完美解决。

  1. PersonalArt

只要改变步长,并每次旋转一个角度就能产生舒适的图形
在这里插入图片描述

0x03 Social Network

  1. 有向图存储

如果不需要存储其他信息,可以如下存储点和边:

//点为Person,即一个个体
Person p1;
//边为Friends,为人的集合
Set<Person> friends;
  1. 类的属性、方法分配

Person类

public class Person {

    public String name;
    public boolean isVisit;    // whether is visited
    Set<Person> friends = new HashSet<Person>();
    public Person(String name){
        this.name = name;
    }

    public void makeFriends(Person p){
        friends.add(p);
    }
}

FriendshipGraph类


public Set<Person> people = new HashSet<Person>();
private boolean findFlag;
public void addVertex(Person p){...}
public void addEdge(Person p1,Person p2){...}
public int getDistance(Person p1,Person p2){...}

  1. DFS求最短路径

通过递归实现,比较基础的内容但不够熟练

  1. 不足

测试用例不太会构造,Junit也不是很会写,希望后续可以学到。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值