跳转至

CS61B week1

Lecture 1

学习的顺序:Reading(精华的课程内容)-> Guide (课程内容的总结)-> Slides + Video -> Lab -> HW -> project

entry code: 93PK75

lab1 setup就是教了一下怎么配置自己的电脑,用来跑java

cd . 回到现在这个文件夹,相当于什么都没做
cd .. 回到parent文件夹
ls -l 会把读写状态都给显示出来
mkdir 新建文件夹
cp 复制一个文件
mv 移动一个文件
open . 是打开file explorer在现在的文件夹
rm -r

println包含了一个新的行

Java有static typing, a key feature of Java compiler is that it performs a static type check.

出现类型错误的话,compiler会拒绝运行

python是dynamically typed language

Java可以System.out.println(5 + " ");也可以String h = 5 + "horse";

但是python不可以print(5 + "horse"),因为python doesn't know what the statement is supposed to be, a number or a string?

As an analogy, programming in Python can be a bit like Dan Osman free-soloing Lover's Leap. It can be very fast, but dangerous. Java, by contrast is more like using ropes, helmets, etc. as in this video.

comment

In a Javadoc comment, the block comment starts with an extra asterisk, e.g. /**, and the comment often (but not always) contains descriptive tags.

定义array

public class HelloNumbers {
    public static void main(String[] args){
        int[] numbers = new int[]{1, 3, 5, 7};
        System.out.println(numbers[3]);
        System.out.println(numbers.length);
    }
}

continue和break

解释是continue可以直接跳过现在所在的这个循环,进入到下一个i的自增循环

The continue statement skips the current iteration of the loop, effectively jumping straight to the increment condition.

break是直接终止包含这个break的最内层的循环

By contrast, the break keyword completely terminates the innermost loop when it is called.

public class HelloNumbers {
    public static void main (String[] args) {
        String[] a = {"cat", "horse", "dog", "elephant"};
        for (int i = 0; i < a.length; i += 1) {
            if (a[i].contains("horse")) {
                continue;
            }
            for (int j = 0; j < 3; j += 1) {
                System.out.println(a[i]);
            }
        }
    }
}

​ 输出是:

cat
cat
cat
dog
dog
dog
elephant
elephant
elephant

而将continue改成break之后,那么会输出:

cat
cat
cat

另外一个break的例子:

public class HelloNumbers {
    public static void main(String[] args) {
        String[] a = {"cat", "dog", "laser horse", "ketchup", "horse", "horbse"};

        for (int i = 0; i < a.length; i += 1) {
            for (int j = 0; j < 3; j += 1) {
                System.out.println(a[i]);
                if (a[i].contains("horse")) {
                    break;
                }
            }
        }
    }
}

输出:

cat
cat
cat
dog
dog
dog
laser horse
ketchup
ketchup
ketchup
horse
horbse
horbse
horbse

在这个例子里面,horselaser horse都只输出了一次,然后内层的循环就直接被终止了

增强for循环(enhanced for loop)

public class EnhancedForBreakDemo {
    public static void main(String[] args) {
        String[] a = {"cat", "dog", "laser horse", "ketchup", "horse", "horbse"};

        for (String s : a) {
            for (int j = 0; j < 3; j += 1) {
                System.out.println(s);
                if (s.contains("horse")) {
                    break;
                }                
            }
        }
    }
}

输出是

cat
cat
cat
dog
dog
dog
laser horse
ketchup
ketchup
ketchup
horse
horbse
horbse
horbse

Lecture 2

调用其他类的类被称为被调用类的“client”

A class that uses another class is sometimes called a "client" of that class, i.e. DogLauncher is a client of Dog.

public class Dog {

    // instance variable
    public int weightInPounds;
    /** one integer constructor for dogs. */
    public Dog(int w) {
        weightInPounds = w;
    }
    // non-static method a.k.a Instance Method
    public void makeNoise() {
        if (weightInPounds < 10) {
            System.out.println("yip!");
        } else if (weightInPounds < 30) {
            System.out.println("bark.");
        } else {
            System.out.println("wooof!");
        }
    }
}
public class DogLauncher {
    public static void main(String[] args) {
        Dog d;
        // An Object in Java is an instance of any class.
        d = new Dog();
        // Members of a class are accessed using dot notation.
        d.weightInPounds = 20;
        d.makeNoise();
    }
}

key features

  • An Object in Java is an instance of any class.
  • The Dog class has its own variables, also known as instance variables or non-static variables. These must be declared inside the class, unlike languages like Python or Matlab, where new variables can be added at runtime.
  • The method that we created in the Dog class did not have the static keyword. We call such methods instance methods or non-static methods.
  • To call the makeNoise method, we had to first instantiate a Dog using the new keyword, and then make a specific Dog bark. In other words, we called d.makeNoise() instead of Dog.makeNoise().
  • Once an object has been instantiated, it can be assigned to a declared variable of the appropriate type, e.g. d = new Dog();
  • Variables and methods of a class are also called members of a class.
  • Members of a class are accessed using dot notation.

构造函数其实就是用来创建对象的函数,又叫构造器。构造函数是一个类创建对象的根本途径,如果一个类没有构造器,则它无法创建实例(对象)。如果你没有给一个类显式地创建一个构造器,则系统会自动为其创建一个默认的构造器。如果你显式地为一个类创建了构造器,则系统不会再为其提供默认构造器。

无参构造函数(默认构造函数):

public 类名称(){
    ……
}

带参构造函数:

public 类名称(参数1参数2) {
}

为了实现在面向对象的语言里创建对象Dog d = new Dog(20)

public class DogLauncher {
    public static void main(String[] args) {
        Dog d = new Dog(20);
        d.makeNoise();
    }
}

我们需要往被调用的class里面加入一个构造器constructor:

public class Dog {
    public int weightInPounds;

    // constructor
    // The constructor with signature public Dog(int w) will be invoked anytime that we try to create a Dog using the new keyword and a single integer parameter.
    public Dog(int w) {
        weightInPounds = w;
    }

    public void makeNoise() {
        if (weightInPounds < 10) {
            System.out.println("yipyipyip!");
        } else if (weightInPounds < 30) {
            System.out.println("bark. bark.");
        } else {
            System.out.println("woof!");
        }    
    }
}

we can create arrays of instantiated objects in java

public class DogLauncher {
    public static void main(String[] args) {
        Dog d = new Dog(51);
        d.makeNoise();
        // create an array to hold two Dog objects
        Dog[] dogs = new Dog[2];
        // create each actual Dog
        dogs[0] = new Dog(5);
        dogs[1] = new Dog(20);
        dogs[0].makeNoise();
        dogs[1].makeNoise();
    }
}

static vs. non static method (instance method)

Java allows us to define two types of methods:

  • Class methods, a.k.a. static methods.
  • Instance methods, a.k.a. non-static methods.

Instance methods are actions that can be taken only by a specific instance of a class. Static methods are actions that are taken by the class itself. Both are useful in different circumstances. As an example of a static method, the Math class provides a sqrt method. Because it is static, we can call it as follows:

x = Math.sqrt(100);

If sqrt had been an instance method, we would have instead the awkward syntax below. Luckily sqrt is a static method so we don't have to do this in real programs.

Math m = new Math();
x = m.sqrt(100);
public class DogLauncher {
    public static void main(String[] args) {
        Dog d = new Dog(51);
        d.makeNoise();
        // create an array to hold two Dog objects
        Dog[] dogs = new Dog[2];
        // create each actual Dog
        dogs[0] = new Dog(5);
        dogs[1] = new Dog(20);
        dogs[0].makeNoise();
        dogs[1].makeNoise();
        double x = Math.round(36);
        System.out.println(x);
        Dog d1 = new Dog(5);
        Dog d2 = new Dog(40);
        // 我们在这里使用Dog.maxDog,这种方法调用的Dog class,而直接调用Dog类中的方法是static方法,所以maxDog定义的时候必须是public static Dog maxDog
        // Dog bigger = Dog.maxDog(d1, d2);

        // 这里我们使用的是实例化之后的d1.maxDog,所以是non-static的
        Dog bigger = d1.maxDog(d2);
        bigger.makeNoise();

        //这种是非常不好的,bad style,因为static的变量最好是通过Dog.bionome来进行调用
        System.out.println(d.bionome);
        // 这种就比较好
        System.out.println(Dog.bionome);
    }
}
public class Dog {

    // instance variable
    public int weightInPounds;
    public static String bionome = "canis familiaris";
    /** one integer constructor for dogs. */
    public Dog(int w) {
        weightInPounds = w;
    }
    // non-static method a.k.a Instance Method
    public void makeNoise() {
        if (weightInPounds < 10) {
            System.out.println("yip!");
        } else if (weightInPounds < 30) {
            System.out.println("bark.");
        } else {
            System.out.println("wooof!");
        }
    }
    // in this case, we are invoking the method using the class name.
    public static Dog maxDog(Dog d1, Dog d2) {
        if (d1.weightInPounds > d2.weightInPounds) {
            return d1;
        } 
        return d2;
    }
    public Dog maxDog(Dog d2) {
        if (this.weightInPounds > d2.weightInPounds) {
            return this;
        }
        return d2;
    }
}
  • a variable or method defined in a class is also called a member of that class
  • static members are accessed using class name, e.g. Dog.bionome
  • Non-static members cannot be invoked using class name
  • static methods must access instance variables via a specific instance, e.g. d1.

public static void main (String[] args)

  • public: So far, all of our methods start with this keyword.
  • static: It is a static method, not associated with any particular instance.
  • void: It has no return type.
  • main: This is the name of the method.
  • String[] args: This is a parameter that is passed to the main method.

args[]的使用-summing command line arguments

public class DemoSum {
    public static void main (String[] args) {
        int sum = 0;
        int i = 0;
        while (i < args.length) {
            sum = sum + Integer.parseInt(args[i]);
            i += 1;
        }
        System.out.println(sum);
    }
}

Exercise

public class HelloNumbers {
    public static void main (String[] args) {
        int x = 0;
        int sum = 0;
        while (x < 10) {
            System.out.println(sum);
            x = x + 1;
            sum = sum + x;
        }
    }
}

Homework exercise 2

public class Max {
    public static int max (int[] array) {
        int x = 0;
        int m = array[0];
        int length = array.length;
        while (x < length) {
            if (array[x]> m ) {
                m = array[x];
            }
            x = x + 1;
        }
        return m;
    }
    public static void main (String[] args) {
        int[] numbers = new int[]{9, 2, 15, 2, 22, 10, 6};
        System.out.println(max(numbers));
    }
}

Homework exercise 4

public class WindowPosSum {
    public static void windowPosSum(int[] a, int n) {
        /**
         * replace each element a[i] with the sum of a[i] through a[i + n]
         */
        // copy the a array to a new array b
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i += 1) {
            b[i] = a[i];
        }
        // 遍历每个元素
        for (int i = 0; i < a.length; i += 1) {
            if (a[i] <= 0) {
                continue;
            }
            else {
                if ((i + n) < a.length) {
                    int temp = 0;
                    for (int j = i; j <= i + n; j += 1) {
                        temp += b[j];
                    }
                    a[i] = temp;
                }
                else {
                    int temp = 0;
                    for (int j = i; j < a.length; j += 1) {
                        temp += b[j];
                    }
                    a[i] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
        int[] a = {1, 2, -3, 4, 5, 4};
        int n = 3;
        windowPosSum(a, n);
        System.out.println(java.util.Arrays.toString(a));
    }

}

使用break:

public class WindowPosSum {
    public static void windowPosSum(int[] a, int n) {
        /**
         * replace each element a[i] with the sum of a[i] through a[i + n]
         */
        // copy the a array to a new array b
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i += 1) {
            b[i] = a[i];
        }
        // 遍历每个元素
        for (int i = 0; i < a.length; i += 1) {
            if (a[i] <= 0) {
                continue;
            }
            else {
                int temp = 0;
                for (int j = i; j <= i + n; j++) {
                    if (j >= a.length) {
                        break;
                    }
                    temp += b[j];
                }
                a[i] = temp;
            }
        }
    }
    public static void main(String[] args) {
        int[] a = {1, 2, -3, 4, 5, 4};
        int n = 3;
        windowPosSum(a, n);
        System.out.println(java.util.Arrays.toString(a));
    }

}

Exercise 1.2.1

public static Dog maxDog(Dog d1, Dog d2) {
    if (weightInPounds > d2.weightInPounds) {
        return this;
    }
    return d2;
}