Recursion

public void drawLine(int n) {
    // return if n is 0 or less
    if (n < 1) {
        return;
    }

    // print number of stars based on n
    for (int i = 1; i <= n; i++) {
        System.out.print("*");
    }

    // new line + call recursively
    System.out.println();
    drawLine(n - 1);
}
drawLine(10);
**********
*********
********
*******
******
*****
****
***
**
*

Selection Sort

Write a insertion or selection sort program that sorts an ArrayList in decreasing order so that the largest country is at the beginning of the array (Create your own Country class with size). Use a Comparator.

public class Country {
    public String name;
    private long population;

    // constructor
    public Country(String name, long population) {
        this.name = name;
        this.population = population;
    }

    // getPop method to be able to compare two countries
    public long getPop() {
        return this.population;
    }

    // compare country population sizes
    public int compareCountry(Country c) {
        if (this.getPop() > c.getPop()) {
            return 1;
        }
        else if (this.getPop() < c.getPop()) {
            return -1;
        }
        else {
            return 0;
        }
    }

    // toString method for printing
    public String toString() {
        String string = "Name: " + this.name + "   | Population: " + this.population;
        return string;
    }
}

public class SelectionSort {
    public static void sort(Country[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            int min_idx = i;
            for (int j = i+1; j < arr.length; j++) {
                // use compare country, only set min_idx if -1 (smaller pop)
                if (arr[j].compareCountry(arr[min_idx]) == -1)
                    min_idx = j;
            }
            // assign temp country to swap
            Country temp = arr[min_idx];
            arr[min_idx] = arr[i];
            arr[i] = temp;
        }
    }
    public static void main(String[] args) {
        Country[] arr = {
            new Country("United States", 331900000),
            new Country("Bahrain", 1463000),
            new Country("Monaco", 36686),
            new Country("China", 1412000000),
            new Country("Netherlands", 17530000)
        };
        SelectionSort.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

SelectionSort.main(null);
Name: Monaco   | Population: 36686
Name: Bahrain   | Population: 1463000
Name: Netherlands   | Population: 17530000
Name: United States   | Population: 331900000
Name: China   | Population: 1412000000

Heap

  • Root node vs. leaf node
  • Complete binary tree where all nodes have 2 or 0 children (except last leaf node)

ArrayList

  • Collection w/ prop of resizable array

Choose two of these options to do.

  • Test if two arraylists contain the same elements in reverse order
  • Overwrite all the elements in an arraylist with the alphabet
  • Remove every other element from an arraylist
import java.util.ArrayList;

public class ReverseOrder {
    public static boolean check(int[] a, int[] b) {
        // check if end of b array is not the same as beginning of a array
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[a.length - 1 - i]) {
                return false;
            }
        }
        // return true if all values match up
        return true;
    }

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

        System.out.println(ReverseOrder.check(a,b));

        b = new int[] {5,4,2,3,1};
        System.out.println(ReverseOrder.check(a,b));        
    }
}

ReverseOrder.main(null);
true
false
public class AlternateRemove {
    public static int[] remove(int[] a) {
        // create new array with half the size of a array length
        int[] b = new int[a.length / 2];

        // iterate through array to only add every other value to new array
        for(int i = 0; i < a.length; i += 2) {
            b[i/2] = a[i];
        }
        return b;
    }

    public static void main(String[] args) {
        // initialize int[] with 8 values
        int[] a = new int[] {1,2,3,4,5,6,7,8};

        int[] b = AlternateRemove.remove(a);
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }

    }
}

AlternateRemove.main(null);
1 3 5 7