Table of Scores

Unit Score Grading
Unit 1 1/1 Link
Unit 2 0.95/1 Link
Unit 3 0.9/1 Link
Unit 4 1/1 Link
Unit 5 1/1 OUR OWN LESSON - FULL SCORE
TOTAL SCORE 4.85/5 2 Unknown ones




Unit Score Grading
Unit 6 0.9/1 Link
Unit 7 0.8/1 (late) Link
Unit 8 0.95/1 Link
Unit 9 1/1 Link
Unit 10 0.9/1 Link
TOTAL SCORE 4.55/5 -----

Unit 1 Primitives

Casting (Division & Rounding/Truncating)

int a = 10;
int b = 3;
// Examples of Casting
// Casting in Division w/ casting (rounds down a/b in comparison to a more precise answer)
System.out.println(a/b);
System.out.println((double) a / b);

// Casting to Round/Truncate
double c = 3.5;
System.out.println((int)(a + c));
3
3.3333333333333335
13

Wrapper Classes

import java.util.ArrayList;

// To initialize a Wrapper class as a variable, you instantiate just like any object
Integer wrapperInteger = new Integer(10);
System.out.println(wrapperInteger);

// Some object only methods are shown below
// toString() method
System.out.println("This is a Wrapper Class: " + wrapperInteger.toString());

// ArrayLists
ArrayList<Integer> integers = new ArrayList<Integer>();
integers.add(wrapperInteger);
System.out.println(integers);
10
This is a Wrapper Class: 10
[10, 5]

Unit 2 Using Objects

Concatenation

String name = "Nathan"; Integer age = new Integer(17);
System.out.println("My Name: " + name);
System.out.println("My Age: " + age.toString());
My Name: Don
My Age: 17

Math Class (Random)

// Floor Function  
System.out.println(Math.floor(10.999999999));

// Ceiling Function
System.out.println(Math.ceil(10.999999999));

// Exponents
System.out.println(Math.pow(10, 3));

// Logarithms in base 10
System.out.println(Math.log(2));

// Rounding
System.out.println(Math.round(15.546432));

// Random (generates random number btwn 0 & 1)
System.out.println(Math.random());
10.0
11.0
1000.0
0.6931471805599453
16
0.7980473864210647

Unit 3 Booleans, If/Else Statements, & Comparison

Comparisons w/ Primitives (numbers, characters, booleans), Strings, & Objects

int a = 0;
int b = 0;
int c = 1;

// Comparing two same numbers
System.out.println(a == b);

// Comparing two different numbers
System.out.println(a == c);

String as = new String("yay");
String bs = new String("cool");

// Comparing the same string to itself (SAME memory location)
System.out.println(as == as);

// Comparing strings with same content using wrong operator (DIFFERENT memory location)
System.out.println(as == bs);

// Comparing strings with same content using correct .equals()
System.out.println(as.equals(bs));
true
false
true
false
false

Compound Boolean Expression

boolean a = true;
boolean b = false;

// Creating a compound expression
boolean compound = !(a && b) && (b || a) && (!b && !a);

// Printing the result
System.out.println(compound);
false

De Morgan's Law

boolean a = true;
boolean b = false;
boolean c = true;
boolean d = false;

// complicated boolean expression
boolean res1 = !((!(a && b)) || (!(a || b)));

// simplified using De Morgan's Law once
boolean res2 = !((!a || !b) || (!a && !b));

//simplified using De Morgan's Law twice
boolean res3 = !(!a || !b) && !(!a && !b);

// all results are the same
System.out.println(res1 + " " + res2 + " " + res3);
false false false

Unit 4 Loops

For Loop & Enhanced For Loop

// looping through even numbers
for (int i = 0; i<10; i+=2) {
    System.out.println(i);
  }
  
  int[] arr = {1, 2, 3, 7, 8};
  
  // looping through array with conventional for lopo
  for (int i = 0; i<arr.length; i++) {
    System.out.println(arr[i]);
  }
  
  // looping through array with enhanced for loop
  for (int i : arr) {
    System.out.println(i);
  }

While Loop & Do While Loop

int i = 0;
boolean falseBool = false;

// printing even numbers with while loop
while (i < 10) {
  System.out.println(i);
  i += 2;
}

// if condition is false while loop does not run at all
while (falseBool) {
  System.out.println("inside while loop");
}

// if condition is false in do while, the loop runs once
do {
  System.out.println("inside do-while loop");
} while (falseBool);

Nested Loops

int[][] arr = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
  };
  
  // using nested for loops for 2D array
  for (int i = 0; i<arr.length; i++) {
    for (int j = 0; j<arr[i].length; j++) {
      System.out.print(arr[i][j] + " ");
    }
    System.out.println();
  }
1 2 3 
4 5 6 
7 8 9 

Unit 5 Classes (Own Lesson)

Creating a Class

class SomeClass{
    
}

Main Method & Tester Methods

class SomeClass {
    // main method (definition for main method, more on individual parts later)
    public static void main(String[] args){
        System.out.println("cool main method");
    }
}

SomeClass.main(null);
cool main method

this Keyword The "this" keyword allows you to access properties of the class. See constructor example to see use of this keyword.

Constructors

// creating a class (camel casing w/ first letter capitalized)
class SomeClass {
    int someInt;
    String someString;

    // Constructor
    public SomeClass(int someInt, String someString){ // constructor passes in outside parameters if we want to initialize certain fields
        // this references objects own fields to differentiate (more on that later)
        this.someInt = someInt;
        this.someString = someString;
    }

    public static void main(String[] args){
        SomeClass obj = new SomeClass(123, "abc");
    
        System.out.println(obj.someInt);
        System.out.println(obj.someString);
    }
}

SomeClass.main(null);
123
abc

Mutator Methods & Setter Methods These methods are used to get properties of an object from the outside the class definition. They are almost always necessary for private variables within classes.

Getters can be applied on only the properties which should be accessed outside the class. They always have a return type of whatever data field is being retrieved.

Setters are used to only set properties which are set outside the class. They are always void methods as their only purpose is to set variables.

class SomeClass {
    private int someInt;
    private String someString;

    public SomeClass(int someInt, String someString){ 
        this.someInt = someInt;
        this.someString = someString;
    }

    // Getter method
    public int getSomeInt(){
        return this.someInt;
    }

    public static void main(String[] args){
      SomeClass obj = new SomeClass(123, "abc");

      //Using Getter Method
      System.out.println(obj.getSomeInt());
    }
}

SomeClass.main(null);
123
class SomeClass {
  private int someInt;
  private String someString;

  public SomeClass(int someInt, String someString){ 
      this.someInt = someInt;
      this.someString = someString;
  }

  public int getSomeInt(){
      return this.someInt;
  }

  // Setter
  public void setSomeInt(int newInt){
       this.someInt = newInt;
  }

  public static void main(String[] args){
      SomeClass obj = new SomeClass(123, "abc");

      // Using setter method
      obj.setSomeInt(111);
      System.out.println(obj.getSomeInt());
  }
}

SomeClass.main(null);
111

Unit 6 Arrays

What is an Array? Element -> 1 value in an array Index -> the position of the value in the array Declaring an array int[] array = new int[10]; Bound errors -> accessing an element that does not exist Uninitialized and Unfilled arrays, assigning an array variable but not the whole array Traversing array, can use while but mainly for loop enhanced for loop .. for (int element:values)

public class ArrayMethods {
    private static int[] values = {1,2,3,4,5}; // Array

    public static void swap(){
        System.out.println("0th Index = " + values[0]);
        System.out.println("4th Index = " + values[4]);
        int a = values[0];
        int b = values[4];
        values[0] = b;
        values[4] = a;
        System.out.println("0th Index = " + values[0]);
        System.out.println("4th Index = " + values[4]);
    }


    public static void replaceZero(){
        for(int i = 0; i < values.length; i++){
            if(values[i] % 2 == 0){
                values[i] = 0;
            }
        }
        for(int i : values){
            System.out.print(i);
        }
    }
}

ArrayMethods.swap();
ArrayMethods.replaceZero();
0th Index = 1
4th Index = 5
0th Index = 5
4th Index = 1
50301

Unit 7 ArrayLists

An ArrayList is like an array but the length can be changed. For ArrayList you must use a wrapper class rather than a primitive Due to ArrayList using generics. ArrayList can also work with enhanced for loops and have many convenient functions.

// using "Integer" wrapper class
ArrayList<Integer> listOfIntegers = new ArrayList<>();

// Explicitly creating integer
listOfIntegers.add(new Integer(10));

// automatically converts to Integer
listOfIntegers.add(1);

// using toString method of ArrayList
System.out.println(listOfIntegers);

Unit 8 2D Arrays

Arrays can be placed inside arrays, creating 2D array. Useful for representing 2d space, or text (as 2d). Defined by using two pairs of square brackets after the type. Can be traversed using nested for loop. Concept of putting one for loop inside another. Really useful for traversing 2d arrays.

public class AaditGupta {
    int[][] bananas = {{1,2,3}, {4,5,6}, {7,8,9}};

    public void regular(){
        System.out.println("Regular Array - Unaltered");
        for(int i = 0; bananas.length > i; i++){
            for(int j=0; j < bananas[i].length; j++){
                System.out.print(bananas[i][j]);
            }
            System.out.println(" ");
        }
        System.out.println(" ");
    }

    public void mixmix(){
        int[] a = bananas[0];
        int[] b = bananas[2];
        bananas[0] = b;
        bananas[2] = a;
        System.out.println("Array - Reversed");
        for(int i = 0; bananas.length > i; i++){
            for(int j=0; j < bananas[i].length; j++){
                System.out.print(bananas[i][j]);
            }
            System.out.println(" ");
        }
        bananas[2] = b;
        bananas[0] = a;
        System.out.println(" ");
    }
    
    public void input(){
        System.out.println("Array - Input display");
        Scanner myObj = new Scanner(System.in);  
        System.out.println("Enter row (integer)");
        int row = myObj.nextInt() - 1;  // Read user input
        System.out.println("Enter column (integer)");
        int column = myObj.nextInt() - 1;  // Read user input
        System.out.println("Row: " + (row+1) + " | Column: " + (column+1) + " | Number: " +bananas[row][column]);
        System.out.println(" ");
    }

    public void multiply(){
        System.out.println("Array - Sum of Products of Rows");
        int a = 0;
        int sum = 0;
        boolean first = true;
        for(int i=0; i < bananas.length; i++){
            for(int j=0; j < bananas[i].length; j++){
                if(first == true){
                    a += bananas[i][j];
                    first = false;
                }
                else{
                    a *= bananas[i][j];
                }
            }
            sum += a;
            a = 0;
            first = true;
        }
        // 6, 120, 504
        System.out.println("Sum = " + sum); // 630
    }

    public static void main(String[] args) {
        AaditGupta aaditGupta = new AaditGupta();
        aaditGupta.regular();
        aaditGupta.mixmix();
        aaditGupta.input();
        aaditGupta.multiply();
    }
}

AaditGupta.main(null);
Regular Array - Unaltered
123 
456 
789 
 
Array - Reversed
789 
456 
123 
 
Array - Input display
Enter row (integer)
Enter column (integer)
Row: 1 | Column: 1 | Number: 1
 
Array - Sum of Products of Rows
Sum = 630

Unit 9 Inheritance

Inheritance can be used when two classes share similar functionality. Allows super class to have base functionality (ie. car). Sub class adds additional functionality to the base (ie. tesla car) "extends" and "abstract" keywords can be used to define inheritance in Java.

Extends key word Defines a sub class that inherits all the methods from the super class. Useful because you don't need to redefine everything from super class.

public class Person {
    protected String name;
    protected String birthday;
 
    public Person (String name, String birthday){
       this.name = name;
       this.birthday = birthday;
    }
 
    public String getName(){
       return name;
    }

    public int getAge() {
         return 2022 - Integer.parseInt(birthday);
    }

    @Override
    public String toString() {
         return "Person (name: " + name + ", birthday: " + birthday + ")";
    }
 }
 
 public class Student extends Person { // extends
    private int grade;
    private double gpa;
    private String coding;
 
    public Student (String name, String birthday, int grade, double gpa, String coding) {
       super(name, birthday); // use of super()
       this.grade = grade;
       this.gpa = gpa;
       this.coding = coding;
    }

    // return gpa
    public double getGPA() {
        return gpa;
    }

    public String coding() {
        return coding;
    }

    // return grade
    public int getGrade(){
       return grade;
    }

    @Override // override override
    public String toString() {
         return "Student (name: " + name + ", birthday: " + birthday + ", coding: " + coding + ", gpa:" + gpa + ", grade: " + grade + ")";
    }
 }

 public class Teacher extends Person {
      private String subject;
      private String degree;
   
      public Teacher (String name, String birthday, String subject, String degree) {
         super(name, birthday);
         this.subject = subject;
         this.degree = degree;
      }
   
      // return subject
      public String getSubject() {
         return subject;
      }

   
      // return degree
      public String getDegree() {
         return degree;
      }

      @Override
      public String toString() {
            return "Teacher (name: " + name + ", birthday: " + birthday + ", subject: " + subject + ", degree: " + degree + ")";
      }
 }

Person johnnysins = new Person("Johnny Sins", "1985");
Person don = new Student("Don", "2005", 11, 5.0, "true");
Person mortensen = new Teacher("Mortensen", "Since Forever", "CODE CODE CODE", "IDK lmao");
System.out.println(don.toString());
System.out.println(johnnysins.toString());
System.out.println(mortensen.toString());
Student (name: Don, birthday: 2005, coding: true, gpa:5.0, grade: 11)
Person (name: Johnny Sins, birthday: 1985)
Teacher (name: Mortensen, birthday: Since Forever, subject: CODE CODE CODE, degree: IDK lmao)

Unit 10 Recursion

Recursion can be used in situations where you need to repeatedly do something, instead of loops. Recursion must call itself and have a base case. Base case allows the recursion to end at some point.

Big O notation for Hash map, Binary Search, Single loop, Nested Loop Used to describe the most time it would take for a function to run (without constants). For example, the recursion below would take O(n) time as it has to go through n iterations to calculate the factorial.

public int factorial (int n) {
    if (n == 0 || n == 1) {
      return 1;
    }
    return n * factorial(n-1);
}
  
  System.out.println(factorial(5));