Unit 6 Arrays

  • The unit I taught

Unit 7 ArrayList

  • Can change size
  • A class with many methods
  • Not designed to store primitives, they store object references
  • Slightly slower than arrays

7.1 Introduction to ArrayList

  • Primitive Data Types:
    • Boolean
    • Char
    • Double
    • Int
  • Wrapper Class Data Types
    • Boolean
    • Character
    • Double
    • Integer
import java.util.ArrayList; //you must import the java.util package

// Instantiating: ArrayList<DataType> variableName = new ArrayList<>(n);
//DataType must be nonprimitive data type

public class introArrayList {
    public static void main (String[] args) {
        ArrayList<Integer> e1 = new ArrayList<Integer>(); //empty
        ArrayList<String> e2 = new ArrayList<String>(5); //5 elements
        ArrayList<Dogs> e3 = new ArrayList<Dogs>(); //you can store whatever objects you want
    }
}

7.2 ArrayList Methods

  • size();
    • Returns the number of elements in the list
  • add(obj);
    • Adds elements at the end
  • remove(index);
    • Removes element at a specific index
  • set(index, object);
    • Replaces element at index with new object
  • get(index);
    • Returns element at index

Unit 8 2D Arrays

Objectives

  • Representing collections of related primitive and object reference data using two dimensional array objects

2D Array Vocab

  • Arrays: 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 (it starts from 0)
  • Array length: the number of elements in the array (public and final)
// Initializing a sample Array
public class Test {

    public static void main(String[] args) {
 
       int[][] arr = {
          { 1, 2, 3 },
          { 4, 5, 6 },
          { 7, 8, 9 }
       };
 
       System.out.println("arr[0][0] = " + arr[0][0]);
       System.out.println("arr[1][2] = " + arr[1][2]);
       System.out.println("arr[2][1] = " + arr[2][1]);
       
    }
 
 }
 Test.main(null);
arr[0][0] = 1
arr[1][2] = 6
arr[2][1] = 8

Nested Loops

  • Can be used to traverse a 2D array
// Example of a nested loop for traversing an array
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

Notes of Key Concepts

  • Each class can extend from it's base class
  • When a class extends from its base class, it inherits the methods and other attributes
  • Protected: an access modifier so that the attribute isn't affected by access modifier that isn't affected by outside modifiers
  • If the attributes of the subclass are exactly the same as the attributes of the superclass, then we can just use the constructor of the superclass.
  • An additional attribute that's added to the subclass class does not have to be added to the superclass if it does not apply to it
  • Overriding methods allows the subclass to provide specific implementation of a method
    • Has to be already provided by the super-class
  • If a method in a subclass has the same name, same parameters or signature, and same return type as a method in its super-class, then the method in the subclass will override the method in the super-class.
  • Uses of the super keyword
    • Use constructors in the superclass
    • Use the methods in the superclass in the child class
  • You can use a inheritance hierarchy to organize references
  • Polymorphism: You can do one action many ways
    • Runtime Polymorphism: method overriding
    • Compile Time Polymorphism: also using method overrloading
  • Object class is the superclass of all other classes in Java
  • toString Method: Prints out the attributes of an object and converts a string object to string
  • equals Method: Compares two strings and returns a boolean value of true if equal

Unit 10 Recursion

Learning Objectives

  • Learn about how to write simple recursive methods and determine the purpose or output of a recursive method by tracing
  • Determine the result of recursive method calls
  • Primarily in the multiple choice section; students were asked to determine the result or describe the behavior of a recursive method
  • 5 to 7.5% exam weighting

10.1 Recursion

  • A recursive method is a method that calls itself - a subproblem that calls itself repeatedly
  • To parts to the method:
    • a base case
    • recursive call
  • After multiple calls, the base case is reached where recursion is stopped and a value is returned
    • Should be written first to avoid infinite recursion
  • Recursions are similar to loops - recursions can be written as loops
  • Iteration vs. Recursion:
    • Iteration is used when we execute a set of instructions repeatedly (through a loop) until the condition becomes false
    • Recursion is used when the solution to a bigger problem can be expressed in terms of smaller problems
    • Main Difference: recursion uses function calls vs. iteration uses for and while loops
  • Binary search algorithm
    • Data must be in sorted order
    • Keeps halving array until value is found
    • More efficient than linear search (O(log2n) vs O(n))

Selection Sort

  • Linear Recursion:
    • A function that only makes a single call to itself each time the function runs (as opposed to one that would call itself multiple times during its execution)
  • Selection Sort:
    • The algorithm works by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the end of the sorted part

Merge Sort

  • Can be used to sort ArrayList structures
  • Uses a Divide and Conquer algorithm
  • It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves
  • merge() function is used for merging two halves