software construction1&2

txt1
一、静态检查、动态检查、不检查
1、静态检查:检查类型,保证变量的值在这个集合
(1)语法错误 Math.sine(2) (应该是 sin.)
(2)参数的个数不对,例如 Math.sin(30, 20)
(3)参数的类型不对 Math.sin(“30”)
(4)错误的返回类型 ,例如一个声明返回int类型函数return “30”;
2、动态检查
(1)非法变量值:x/y (y=0的时候运行出错)
(2)越界访问:在字符串中使用负、或者太长的索引
(3)
(4)调用对象为null值的引用
例如:

public class Try
{
    public String a ="我是一串文字";
    public static void main(String[] args){
        Try b = new Try();//创建Try的对象事例
        //创建对象,但是对象引用为null的值
        ArrayList<String> list = null;
        list.add(b.a);
        //System.out.println();
    }
}

报错:Exception in thread “main” java.lang.NullPointerException
at rr.Try.main(Try.java:14)
改正:改成

ArrayList<String> list = new ArrayList<String>();

一些练习:

  • 练习1
int n = 5;
if (n) {
  n = n + 1;
}

注:静态错误-语法错误。因为if语句里边必须为boolean类型的内容,但是这里为int

  • 练习2
int big = 200000; // 200,000
big = big * big;  // big should be 40 billion now

注:没有提示错误,但是答案错误。int最大能表示2^31(大约2亿),在这里溢出,得到一个错误的数字。

  • 练习3
double probability = 1/5;

注:得到被截断的错误的答案0

  • 练习4
int sum = 0;
int n = 0;
int average = sum/n;

注:动态错误。除0不能产生一个整数。

  • 练习5
double a=2;
double b=0;
double c= a/b;
System.out.println(c==c+1);//true

注:没有检查出错误,但答案错误。答案为Infinity。之所以没有报异常,是因为这是浮点的除法,也就是说0.0并不是真正意义上的0,它只不过是非常接近0而已,所以y一个数除以一个接近0的数,那么结果应为无穷大。而在java浮点范围内存在Infinity表示无穷大的概念

补充:关于NAN( not a number )

System.out.println(0.0/0.0); 
double i =Math.sqrt(-6);//结果都为NAN 
System.out.println(i+i==i);//false(不是数)

有趣的事情:NaN与任何数比较均返回false

Double.NaN == Double.NaN;//false
Double a = new Double(Double.NaN); 
Double b = new Double(Double.NaN);] 
a.equals(b); //true 
float nan=Float.NaN; 
float anotherNan=Float.NaN; 
//返回0
System.out.println(Float.compare(nan,anotherNan));

一般来说,基本类型的compare()方法与直接使用==的效果“应该”是一样的,但在NaN这个问题上不一致。至于使用哪一个:当程序的语义要求两个NaN不应该被认为相等时(例如用NaN来代表两个无穷大,两个无穷看上去符号是一样,但不应该认为是相等的两样东西),就使用==判断;如果NaN被看得无足轻重(毕竟,我只关心数字,两个不是数字的东西就划归同一类好了嘛)就使用Float.compare()

二、数组

ArrayList<int> a = new ArrayList<int>();//错误!!!

Java要求我们使用对象类型而非原始类型

ArrayList<Integer> a = new ArrayList<Integer>();

三、调用静态方法的正确方法是使用类名称而不是对象引用

public class Hailstone {
    /**
     * Compute a hailstone sequence.
     * @param n  Starting number for sequence.  Assumes n > 0.
     * @return hailstone sequence starting with n and ending with 1.
     */
    public static List<Integer> hailstoneSequence(int n) {
        List<Integer> list = new ArrayList<Integer>();
        while (n != 1) {
            list.add(n);
            if (n % 2 == 0) {
                n = n / 2;
            } else {
                n = 3 * n + 1;
            }
        }
        list.add(n);
        return list;
    }
}

正确方法:

Hailstone.hailstoneSequence(83)

txt2
一、final

final int a = 5;
int b=a;
b=6;//可以对不可变的值(如)进行可变引用,其中变量的值可以更改,
//因为它可以重新指向不同的对象
System.out.println(b);

二、基础练习
- 练习1

int a = 5;
int b;
if (a > 10) {
    b = 2;
} else {
    // b = 4;
}
b *= 3;

注:编译不通过。因为认为else这个分支,b没有初始化

  • 练习2
fahrenheit = 212.0
celsius = (fahrenheit - 32) * 5/9

注:fahrenheit是一个浮点数,所以fahrenheit - 32给出了浮点结果。操作顺序表示,我们首先将该数字乘以5(浮点结果),然后除以9(再次浮点)

但是java 不同

double fahrenheit = 212.0;
double celsius = (fahrenheit - 32) * (5.0/9);//??

三、列表、集合、映射(Lists, Sets, and Maps)
1、声明

List<String> cities = new ArrayList<>();
Set<Integer> numbers = new HashSet<>();
Map<String,Turtle> turtles = new HashMap<>();

2、迭代

for (String city : cities) {
    System.out.println(city);
}

for (int num : numbers) {
    System.out.println(num);
}
//对key进行遍历
for (String key : turtles.keySet()) {
    System.out.println(key + ": " + turtles.get(key));
}
//数字索引进行迭代(不推荐,只有需要索引i的时候才需要)
for (int ii = 0; ii < cities.size(); ii++) {
    System.out.println(cities.get(ii));
}

3、数组与LIst转换

//如果大小固定,用二维数组比较简单
//char[][] grid;
List<List<Character>> grid;

//int[] numbers;
List<Integer> numbers;

四、枚举

public enum PenColor { 
    BLACK, GRAY, RED, PINK, ORANGE, 
    YELLOW, GREEN, CYAN, BLUE, MAGENTA;
}
PenColoe drawingColor = PenColor.RED;

五、如何阅读API
- 练习1

Map<String, Double> treasures = new HashMap<>();
        String x = "palm";
        treasures.put("beach", 25.);
        treasures.put("palm", 50.);
        treasures.put("cove", 75.);
        treasures.put("x", 100.);//不同于"paml"
        //如果是treasures.put("x", 100.)则只是重写更新第2条的50变成100,size是3
        System.out.println(treasures.size());//4
        System.out.println(treasures.get("palm"));//50.0
        treasures.put("palm", treasures.get("palm") + treasures.size());
        System.out.println(treasures.get("palm"));//50+4
        treasures.remove("beach");
        System.out.println(treasures.size());//3
        double found = 0;
        for (double treasure : treasures.values()) {
            found += treasure;//75+54+100
        }
software architecture复习资料(包含考点)外教课 大部分附有中文翻译 1.Please summarise the state of the art of current software systems?(请总结一下当前软件系统的发展现状。)TOPIC0 Software is very complex today 今天的软件非常复杂 Hard for one to understand it all 很难理解所有 Difficult to express in terms all stakeholders understand 很难让所有利益相关者理解 Business drivers add pressure业务驱动增加压力 Shrinking business cycle收缩商业周期 Competition increasing竞争加剧 Ever rising user expectationsn 不断增长的用户期望 “Soft” Requirement“软”要求 A common threat to schedule, budget, success对计划、预算、成功的共同威胁 Too much change can cause failure太多的改变会导致失败 Flexibility and resilience to change is key灵活性和应变能力是关键 Ability to adapt to sudden market changes适应市场突然变化的能力 Design is solid enough that change does not impact the core design in a destabilising way设计是足够牢固的,改变不影响核心设计的不稳定的方式。 Willingness to re-architect as required愿意按要求重新设计 Most projects are unpredictable大多数项目都是不可预知的 Lack of knowing where and what to measure缺乏知道在何处和何来衡量 Lack of yardsticks to gauge progress缺乏衡量进步的尺度 Requirements creep is common需求蠕变是常见的 Scrap and rework is common废料和返工是常见的 Interminably 90% done没完没了地做了90%件 Lack of quality people results in failure缺乏质量导致失败 …in spite of best processes and tools! 尽管有最好的工艺和工具! Managing people is difficult管理人是困难的 Major change is organisationally difficult重大变革在组织上是困难的 Reusing software artifacts is rare重用软件构件是罕见的 Architectures/Designs/Models建筑/设计/模型 Estimating processes估计过程 Team processesq Planning & Tracking procedures and reports团队流程规划和跟踪程序和报告 Software construction & management软件建设与管理 Reviews, testing评论、测试 etc. 等。 (The price and size of computers shrunk. Programmers could have a computers on their desks. The JCL got replaced by GUI. The most-used programming languages today are between 15 and 40 years old. The Fourth Generation Languages never achieved the dream of "programming without programmers".) (计算机的价格和规模缩小了。程序员可以在桌子上放一台电脑。 JCL被GUI替换了。 目前使用最多的编程语言在15到40岁之间。第四代语言从未实现过“无程序员编程”的梦想。) 2. What is software architecture, in your own words? (什么是软件体系结构,用你自己的话?)TOPIC1 The software architecture of a system is the structures of the system, which comprise software components, the externally visible properties of those components, and the relationships among them.(系统的软件体系结构是系统的结构,包括软件组件、这些组件的外部可见属性以及它们之间的关系。) 3.What do you think of Brooks' "Surgical Team"?(你觉得布鲁克斯的“手术队”怎么样?) The ‘surgical team’: one cutter, many supporters(“手术队”:一个裁缝,很多支持者) 4. How did Fred Brooks Jr. describe the role of the architect in his "The Mythical Man-Month"?(小弗雷德.布鲁克斯在《人月神话》中是怎样描述架构师的角色的?) The architect of a system, like the architect of a building, is the user’s agent.(系统的架构师,就像建筑的架构师一样,是用户的代理人。) 5. What have you learnt from David Parnas, for software development?(对如软件开发,你从David Parnas那里学到了什么?) Parnas developed these ‘architectural’ concerns and turned them into fundamental tenets of Software Engineering. The main principles included: Information Hiding as the basis of decomposition for ease of maintenance and reuse [72] The separation of Interface from implementation of components [71, 72] Observations on the separate character of different program elements [74] The “uses” relationship for controlling the connectivity between components [79] To increase extensibility Principles for the detection and handling of errors [72, 76] i.e., exceptions Identifying commonalities in “families of systems” [76] To provide coarse-grained, stable common structures Recognition that structure influences non-functional ‘qualities’ of a system [76]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值