Unit 6

  • Arrays are objects defined by type[] name = new type[size] or initialized with elements using type[] name = new type[]{a,b,c,...}
  • Loops an enhanced can iterate through arrays
  • Index goes from 0 to size-1
  • Algorithms can be used to modify or get info about arrays
  • By default, each element in the array is value 0
int[] arraytest = new int[5];
int[] arraytest2 = {10, 9, 5, 2, 4};

Bound Errors

// good
arraytest[3];
0
//bound error
arraytest[5];
---------------------------------------------------------------------------
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
	at .(#17:1)
  • Uninitialized and Unfilled Arrays - Allocate a single array variable but not the whole array
  • Traversing an array - incrementing through the array and doing stuff
for(int i = 0; i < arraytest2.length; i++){
	System.out.println(arraytest2[i]);
}
10
9
5
2
4

Unit 7

  • Wrapper class stores prim in objects
  • make sure to specify the data type
  • return arraylist using the return command
  • traverse arraylist through for loops and while loops
  • get() to get an element
  • size() to get size
import java.util.ArrayList;

  ArrayList<String> list = new ArrayList<String>();
  list.add("hi"); 
  list.add("hey"); 
  list.add("hello"); 
  list.add("nice");
true
System.out.println(list);
[hi, hey, hello, nice]

add(int index, element)

list.add(1, "test");
System.out.println(list);
[hi, test, hey, hello, nice]

addAll(int index, Collection collection)

ArrayList<String> list2 = new ArrayList<String>();
list2.add("apple"); 
list2.add("orange"); 
list2.add("grape"); 
list2.add("pineapple"); 

System.out.println(list2);
[apple, orange, grape, pineapple]
list.addAll(3, list2);
System.out.println(list);
[hi, test, hey, apple, orange, grape, pineapple, hello, nice]

size()

list.size();
9

clear()

list2.clear();
System.out.println(list2);
[]

remove(int index)

list.remove(2);
System.out.println(list);
[hi, test, apple, orange, grape, pineapple, hello, nice]

remove(element)

list.add("nice");
System.out.println(list);
[hi, test, apple, orange, grape, pineapple, hello, nice, nice]

get(int index)

list.get(3);
// first element is actually the 0th index
orange

set(int index, element)

list.set(4, new String("grapeskittlessuck?"));
System.out.println(list);
[hi, test, apple, orange, grapeskittlessuck?, pineapple, hello, nice, nice]

Unit 8

  • Array = a data structure used to implement a collection (list) of primitive or object reference data
  • Element = a single value in the array
  • Index = the position of the element in the array (starts from 0)
  • Array Length = the number of elements in the array
  • Format: data type[][] name = new data type[num row][num column]

Accessing an element

//Hack 1

public class Test {

    public static void main(String[] args) {
 
      String[][] arr = {
         { "a", "f", "g" },
         { "b", "e", "h" },
         { "c", "d", "i" }
      };
 
      // Print the last element in the array!
      System.out.println(arr[2][2]);
       
    }
 
 }
 Test.main(null);
i

Traverse 2D arrays through Nested Loops

public class Test {

    public static void main(String[] args) {
 
      String[][] arr = {
         { "a", "f", "g", "l" },
         { "b", "e", "h", "k" },
         { "c", "d", "i", "j" }
      };
 
      for (int row = 0; row < 3; row++) {
         for (int col = 0; col < 4; col++) {
            System.out.print(arr[row][col] + " ");
         }
        System.out.println(" ");
      }
       
    }
 
 }
 Test.main(null);
a f g l  
b e h k  
c d i j  

Unit 9

  • Inheritance useful because it is efficeint
  • don't need to define methods over and over again
  • extend from base class
  • base class -> super class
  • protected is an access modifier so that the attribute isn't affected by outside modifiers
  • super keyword allows us to use constructors that we define in the superclass
  • sub and superclass can have some varying attributes
  • Overriding allows a subclass or child class to provide a specific implementation of a method that has already been provided by a super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature, and same return type (or sub-type) as a method in its super-class, then the method in the subclass will override the method in the super-class
public class School {
    protected double numberpencil;
    protected double numberpapaer;
    protected double numberscis;

    public School(double numberpencil, double numberpapaer, double numberscis) {
        this.numberpencil = numberpencil;
        this.numberpapaer = numberpapaer;
        this.numberscis = numberscis;
    }

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

    
}
public class Math extends School {
    protected double numbercalc;

    public Math(double numberpencil, double numberpapaer, double numberscis, double numbercalc) {
        // We use the Superclass constructor for the shared attributes through the keyword "super"
        super(numberpencil, numberpapaer, numberscis);
    }
        // hornSound is not in the Superclass, so we add it separately in the constructor
        
        @Override
        public void test () {
            System.out.println("tets test");
        }

        public static void main(String[] args) {
            // 5 argument constructor
            Math william = new Math(2, 3, 1, 5);
            william.test();
        }

    }
    Math.main(null);
tets test

Polymorphism - methods with the same name that take in different parameters

public void turbo (int a) {
    System.out.println("Engaging turbo " + a);
}

public void turbo (int a, int b) {
    System.out.println("Engaging turbo " + a + " and nitro " + b);
}

toString Method - Prints out the attributes of an object. Converts string objects into a string equals Method - Compares two strings

Unit 10

  • a recursive method is a method that calls itself - a subproblem that calls itself repeatedly
  • contains a base and recursive call.
  • the base case is reached where recursion is stopped and a value is returned
  • base case should be written first to avoid infinite recursion
  • recursion uses function calls vs. iteration uses for and while loops
  • binary search - sorted in order to maximize efficiency
  • Linear recursion - only calls itself once
  • Selection sort - finds the minimum value
  • Merge sort - divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves
class Factorial {

    static int factorial( int n ) {
        if (n != 0)  // termination condition
            return n * factorial(n-1); // recursive call
        else
            return 1;
    }

    public static void main(String[] args) {
        int number = 4, result;
        result = factorial(number);
        System.out.println(number + " factorial = " + result);
    }
}
Factorial.main(null);
4 factorial = 24