Grades

Unit Grade Homework
6: Arrays 0.9/1 ArrayMethods
7: ArrayList 0.9/1 ArrayList sort
8: 2D Array 0.9/1 Class and methods
9: Inheritance 0.83/1 Notebook
10: Recursion 1/1 Review ticket
Total 4.53/5

Unit 6: Arrays

Summary:

  • Array is a collection of data (not to be confused with ArrayList
  • Can hold both primitive and referenced data types ()
  • Track position in array with index (location/order of elements) and length of array (conditional loops)
  • Cannot be changed once created
int[] arrayTest = {1, 2, 3, 4, 5, 6}; // how to initialize

for (int i = 0; i < arrayTest.length; i++) { // iteration
    System.out.println(arrayTest[i]);
}
1
2
3
4
5
6

Unit 7: ArrayList

Summary:

  • Dynamic, unlike static array
  • Need to import java.util.ArrayList
  • Can sort descending or ascending order
  • Also uses loops (enhanced for loop) to traverse
  • More useful in algorithms (size, remove, set, get)
ArrayList<String> books = new ArrayList<String>(); // creating arraylist

books.add("Great Expectations");
books.add("The Brothers Karamazov");
books.add("Frankenstein");
books.add("Fahrenheit 451");
books.add("The Great Gatsby");
books.add("Hamlet");

Collections.sort(books, Collections.reverseOrder());

System.out.println(books); // showing sorted books in descending order
[The Great Gatsby, The Brothers Karamazov, Hamlet, Great Expectations, Frankenstein, Fahrenheit 451]
import java.util.ArrayList;

public class tester {
    public static void main (String[] args) {
        ArrayList<String> words = new ArrayList<String>(Arrays.asList("piano", "tenor", "tempo"));
        System.out.println(words);

        words.add("woodwind");
        System.out.println(words);
    }
}
tester.main(null);
[piano, tenor, tempo]
[piano, tenor, tempo, woodwind]

Unit 8: 2D Array

Summary:

  • Multidimensional array, good for managing complexity
  • Change element value using location of array[i][j]
  • Use nested loops to iterate through the rows, then columns
String[][] test = {{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="},
{"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\"},
{"a", "s", "d", "f", "g", "h", "j", "k", "l"},
{"z", "x", "c", "v", "b", "n", "m", ",", ".", "/"}};
for(int i = 0;i<test.length;i++){
    for(int j = 0; j < test[i].length;j++){ //nested for loops
        System.out.print(test[i][j]+" ");
    }
    System.out.println(" ");
}
1 2 3 4 5 6 7 8 9 0 - =  
q w e r t y u i o p [ ] \  
a s d f g h j k l  
z x c v b n m , . /  

Nested loop

A loop within a loop, which can be used to iterate through each 2D array element

public void printValues() { // arbitrary values
    for (int a = 0; a<numbers.length; a++) {
        for (int b = 0; b<numbers[a].length; b++) {
            numbers[a][b] = a + b; // spacing
            System.out.println(numbers[a][b] + " "); // spacing
        }
        System.out.println();
    }
}

Unit 9: Inheritance

Summary:

  • Subclasses inherit properties of parent classes
  • Use extends to create subclasses from base class
  • Overload is instance of two methods with same name having different functions/parameters
  • Override is used when there is no parent/child method conflict, overwrites parent method

Inheritance (extends)

Extends is used to create a subclass, which inherits the

Subclass constructor, super Keyword

public class Subclass extends Parentclass {}

Super is used to refer to objects in the parent class

public class Fruits {
    public int cost;
    public int season;

    public Fruits (int cost, int season) {
        this.cost = cost; // using this. to access base variables
        this.season = season;
    }

    public String toString () {
        return "Price: $" + this.cost + ", Season: " + this.season;
    }

}

public class Mango extends Fruits { // mango is subclass of fruit
    public Fruits (int cost, int season) {
        super (cost, season); // super keyword
    }
}

Polymorphism (overloading/overriding)

Using one method in several different ways through inheritance (override)

Overload is what happens when two methods with the same name have different arguments

public class School {
    private String model = "Boeing 737";
    private int age = 3;

    public void engineBurr() {
        System.out.println("brrrrrr");
    }
}

private class Jettison extends Plane {
    public Jettison(String model, int age, String color) {
        super(model, age); 
        this.color = color; 

        @Override
        public void engineBurr() {
        System.out.println("BRRRRRRRRR"); 
    }

}

Unit 10: Recursion

Summary:

  • Recursive method calls itself, should have at least one stop case
  • Uses local variables and parameters, often interchangeable with iterative loop
  • Used to traverse String, array, and ArrayList objects
  • Recursion trees are a visual representation of each recursion case
public static void test(int n) {
    if (n > 0) {
        test(n-1);
    }
}

Standard methods

tostring() returns an object as a string

equals() compares two given strings to see if they match

hashCode() returns hash values of input objects

Integer a = 10;
System.out.println(a.toString()); // printing a as a string

String hw = "hello world";
System.out.println(hw.hashCode());
10
1794106052

Big O notation

Used to categorize an algorithm's efficiency and scalability (changes in performance based on data size)

public static int binaryRecursive(int search, int[] array, int start, int end){
                
    int middle = (start + end)/2; // setting the starting boundaries for search by dividing array in 2

    if (end < start) { // error handling
            return -1;
    } 

    if (search < array[middle]) { 
        return binaryRecursive(search, array, start, middle - 1); // lowering range of index values if search is less than the new middle
    }

    if (search > array[middle]) {
        return binaryRecursive(search, array, middle + 1, end); // narrowing range of index values to upper range of array
    }

    if (search == array[middle]) { // success  case
        return middle;
    }

    return -1;
}