VOCABULARY FOR THE UNITS

Casting and Wrapper class

  • Casting - Division: Round Division statement to the nearest integer. This can be used when a program can only handly integers
  • Casting - Rounding: Allows an equation to be rounded to the nearest integer, which can be very helpful
  • Wrapper Classes: Classes that can be created into a primtive type, such as int or boolean
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

Concatenation, math and booleans

  • Concatenation: The process of joining two strings together. The + operator is used to concatenate two strings together.
  • Java Math Class: The import java math class gives a lot of math operations and features that can be used in your code
  • Compound Boolean Expression: If/Else statement block
// Concatenation

String name = "Deez";
String lastName = "Nuts";

System.out.println(name + " " + lastName);

// Java Math Class

import java.lang.Math;
int max = 10;
int min = 1;
int range = max - min + 1;
int rand = (int)(Math.random() * range) + min;
System.out.println(rand);
System.out.println(Math.sqrt(rand));

// Compound Boolean Expression

if (true) {
    System.out.println("Hello");
} else if (false) {
    System.out.println("World");
} else {
    System.out.println("!");
}
Deez Nuts
4
2.0
Hello

Truth Table, De Morgans and Comparing Numbers

  • Truth Table: A table where one column is each variable and another column for the possible value of the variable
  • De Morgans Law: Similar to Logic Gates from CSP. Talks about how || (or) and && (and)
  • Comparing Numbers: Use the == operator to compare numbers
// De Morgans Law
if (!((true == true) && (true == false))) {
    System.out.println("De Morgans law is true");
}

// Comparing Numbers
int x = 1;
int y = 2;

if (x == y) {
    System.out.println("x is equal to y");
} else if (x > y) {
    System.out.println("x is greater than y");
} else if (x < y) {
    System.out.println("x is less than y");
} else {
    System.out.println("x is not equal to y");
}
De Morgans law is true
x is less than y

Static Variables and Inheritance

  • Static Variables: Variables that are constant throughout the whole class. This can be beneficial when you need a variable to be able to be updated throughout the whole class
  • Inheritance: You can use extend method to get attributes from the class that is being extended from
public class Test {
    public static int counter;

    public static void main(String[] args) {
        counter = counter + 1;
        System.out.println(counter);
    }
}

public class ExtendsTest extends Test {
    public static void main(String[] args) {
        counter = counter + 2;
        System.out.println(counter);
    }
}

Test testExample = new Test();
ExtendsTest extendsExample = new ExtendsTest();

testExample.main(null);
extendsExample.main(null);
1
3

Polymorphism, Overloading and Overriding

  • Polymorphism: Allows you to have multiple methods with the same name in one class. This allows you to have multiple ways to do something that is dependant on what is being passed in into the class.
    • Overloading is when there are many methods that have different parameters
    • Overriding is when there is a method with the same name but you modify in subclass
    • Late binding is when the compiler chooses which method to use
public class PolymorphismExample {
    public void yo(int x) {
        System.out.println("Integer: " + x);
    }

    public void yo(String x) {
        System.out.println("String: " + x);
    }
    
    public void yo(double x) {
        System.out.println("Double: " + x);
    }

}

PolymorphismExample poly = new PolymorphismExample();
poly.yo(1);
poly.yo("Hello");
poly.yo(1.0);
Integer: 1
String: Hello
Double: 1.0

Abstract and SUper classes

  • Abstract Class: Hiding certain details but making a barebones of something. For example, an abstract class could be car and then each class inherited would be like SUV or Truck.
  • Super Class: By using super class you can reference the upper class
abstract class Car {
    public void hi() {
        System.out.println("Barebones Class");
    }
}

class SUV extends Car {
    public void hi() {
        System.out.println("The SUV is saying hi");
    }
}

class Truck extends Car {
    public void hi() {
        System.out.println("The truck is saying hi");
    }
}

// Instantiating the Car class would throw an error as it is abstract
SUV suv = new SUV();
Truck truck = new Truck();
suv.hi();
truck.hi();
The SUV is saying hi
The truck is saying hi

Big O

  • Big O Notation: Big O notation is super helpful as it tells you how efficient your algorithm or program is. For example traversing an array would be O(n) because it depends on the size of n for how long the program will go. If you have a nested for loop then it would be O(n^2) because for each n you have another n. Hash map is O(1) and binary search is O(logn).