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
        }

猜你喜欢

转载自blog.csdn.net/leafdown_/article/details/79660606
今日推荐