Lecture 1

August 28, 2017

Homework

Write a for-loop to solve snippet problem #4 below

// Reads temperatures from the user, computes average and # days above average.
import java.util.*;

public class Weather {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("How many days' temperatures? ");
        int days = console.nextInt();

        /// 1. Create temperature array

        int sum = 0;

        for (int i = 0; i < days; i++) {    // read/store each day's temperature
            System.out.print("Day " + (i + 1) + "'s high temp: ");
            /// 2. Store temperature in array

            /// 3. Update sum

        }
        double average = (double) sum / days;

        int count = 0;
        /// 4. Count days over average

        // Report results
        System.out.printf("Average temp = %.1f\n", average);
        System.out.println(count + " days above average");
    }
}

Lecture 2

August 30, 2017 (Section 01)

August 31, 2017 (Section 02)

Homework

Read Chapter 1

Lecture 3

September 1, 2017

Lecture 4

September 6, 2017 (Section 01)

September 7, 2017 (Section 02)

Homework

\[ \sqrt{ (x_2 - x_1)^2 + (y_2 - y_1)^2 } \]

1). Using the formula above, write a method distance that computes the distance between a Point and another Point parameter. Note: this should be an accessor method part of the Point class.

2). Write an additional method, distanceFromOrigin that returns the distance between a Point and the origin, (0,0).

Lecture 5

September 8, 2017

September 11, 2017

Lab 1

Lecture 6

September 13, 2017 (Section 01)

September 14, 2017 (Section 02)

Lecture 7

September 15, 2017

Homework

What is the output of the following code:

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) {
    list.add(10 * i);   // [10, 20, 30, 40, ..., 100]
}
for (int i = 0; i < list.size(); i++) {
    list.remove(i);
}
System.out.println(list);

What is the output of the following code:

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 5; i++) {
    list.add(2 * i);   // [2, 4, 6, 8, 10]
}
int size = list.size();
for (int i = 0; i < size; i++) {
    list.add(i, 42);   // add 42 at index i
}
System.out.println(list);

In the code snippet above, what happens if you get rid of the int size variable above and replace it with list.size()?

Lecture 8

September 18, 2017

September 20, 2017 (Section 01)

September 21, 2017 (Section 02)

Lecture 9

September 22, 2017

IntArrayBag Specification

Midterm Exam 1 Review

Lecture 10

September 25, 2017

IntArrayBag Implementation

Point Implementation

Lecture 11

October 4, 2017 (Section 01)

October 5, 2017 (Section 02)

DoubleArraySeq Javadoc

DoubleArraySeq Source Code Template

Lecture 12

October 11, 2017 (Section 01)

October 12, 2017 (Section 02)

October 13, 2017

October 16, 2017

Lecture 13

Linked Lists (no slides)

October 20, 2017

October 23, 2017

Lecture 14

Doubly-Linked Lists (no slides)

October 27, 2017

Exam 2 Review

Doubly-Linked List Handout

Lecture 15

November 7, 2017

November 8, 2017

November 9, 2017

Section 1

Section 2

Lecture 16

Recursion (no slides)

November 20, 2017

November 27, 2017

Factorial

public static int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

Fibonacci

// note: this is not the most efficient
public static int fib(int n) {
    if (n <= 1) {
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}

Tower of Hanoi

public static void hanoi(int discs, int src, int dest, int temp) {
    if (discs > 0) {
        hanoi (discs - 1, src, temp, dest);
        System.out.printf("Move disc %d from %d to %d\n", discs, src, dest);
        hanoi (discs - 1, temp, dest, src);
    }
}

Digit Operations

public static int sumDigits(int i) {
    if (i == 0)
        return 0;
    return (i % 10) + sumDigits(i / 10);
}

private static int place(int n) {
    if (n == 0)
        return 1;
    return 10 * place(n / 10);
}

public static int reverse(int n) {
    if (n < 10)
        return n;
    return (n % 10) * place(n / 10) + reverse(n / 10);
}

Lecture 17

Binary Search Trees

December 1, 2017

December 4, 2017

December 8, 2017

https://algs4.cs.princeton.edu/32bst/

BinarySearchTree.java