1.1 Essentials

Study Guide Link: http://fa20.datastructur.es/materials/lectures/lec1/lec1.html

Notes:

1. TriangleDrawer.java

The drawTriangle method should take one parameter named N, and it should print out a triangle exactly like your triangle from exercise 1a, but N asterisks tall instead of 5.

public class TriangleDrawer {
    public static void drawTriangle(int N) {
        int row = 1;
        int col = 1;
        while(row < N + 1){
            while(row >= col){  
                System.out.print("*");  //print第i行有i个*, 不换行
                col = col + 1;
            }
            row = row + 1;
            col = 1;  //col 始终保持1
            System.out.println();  //打印完换行
        }
    }
    
    public static void main(String[] args) {
        TriangleDrawer.drawTriangle(10);
    }
 }

2. ClassNameHere.java

while loop vs. for loop

/** Returns the maximum value from m. */

//1.while loop
public class ClassNameHere {
    public static int max(int[] m){
        int i = 0;     //initializaton
        int max_num = 0;
        while(i < m.length){     //termination
            if (m[i] > max_num){
                max_num = m[i];
            }
            i = i + 1;     //increment
        }
        return max_num;
    }
    public static void main(String[] args){
        int[] numbers = new int[]{9,2,15,2,22,10,6};
        System.out.println(max(numbers));
    }
}

// 2.for loop
public class ClassNameHere {
    public static int max(int[] m){
        int max_num = 0;
				
				//for(initialization; termination; increment)
        for (int i=0; i<m.length; i++){  
            if (m[i] > max_num){
                max_num = m[i];
            }
        }
        return max_num;
    }
    public static void main(String[] args){
        int[] numbers = new int[]{9,2,15,2,22,10,6};
        System.out.print(ClassNameHere.max(numbers));
        // System.out.println(max(numbers));
    }
}

3. BreakContinue.java

Continue = "skip". 比如string contain “horse“, 一次含有horse的都不print出来。

Break 相当于只执行一次。

/** Exercise 4 
 * Write a function windowPosSum(int[] a, int n) that replaces each element a[i] with 
 * the sum of a[i] through a[i + n], but only if a[i] is positive valued. If there are 
 * not enough values because we reach the end of the array, we sum only as many values 
 * as we have.
*/
public class BreakContinue {
    public static void windowPosSum(int[] a, int n){
        for (int i = 0; i < a.length; i++){
            //if the value is less than 0
            if (a[i] < 0){
                continue;
            }
            int a_sum = 0;
            //sum a[i] to a[i+n]
            for (int j = 0; j <= n; j++){
                a_sum += a[i+j];
                if (i+j+1 == a.length){
                    break;
                }
            }  
            a[i] = a_sum;
        }
    }

    public static void main(String[] args){
        int[] a = {1,2,-3,4,5,4};
        int n = 3;
        windowPosSum(a, n);

        //Should print 4,8,-3,13,9,4
        System.out.println(java.util.Arrays.toString(a));
    }
}

4. Do-while vs. while

The difference between do-while and while is that do-while evaluates it expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.