• Casting for division: if you are dividing 2 ints into a number that is a decimal, that number should be cast as a float
int val1 = 5;
int val2 = 3;

float res = (float) val1/val2; // (float) is the cast

System.out.println(res);
1.6666666
  • Casting for trunkating and rounding: by casting a double into an int, the number is trunkated/rounded by removing the decimal
int val1 = 5;
int val2 = 3;

int res = val1/val2; // (float) is the cast

System.out.println(res);
1
  • Wrapper class: it lets you use a primitive as an object, and perform methods on them
Integer val2 = 2;
System.out.println(val2.intValue());


int val = 2;
System.out.println(val.intValue());
2
|   System.out.println(val.intValue());
int cannot be dereferenced
  • Concatenation: convert everything to strings and add them end to end
String world = "world";
System.out.println("Hello " + world + " " + 1);
Hello world 1
  • Math class: an import used to do math functions. Math.random() generates a random number
import java.lang.Math;

System.out.println(Math.random());
System.out.println(Math.random());
System.out.println(Math.random());
0.889016555574831
0.20211756754433519
0.018498405592415446
  • Compound boolean expression: combinations of expressions by using && or ||.
boolean x = true;
boolean y = false;

if (x&&y) {
    System.out.println("and true");
}

if (x||y) {
    System.out.println("or true");
}
or true
  • Truth Tables: a table that shows the combinations of inputs and their respective outputs for boolean expressions

  • De Morgan's law: a compound expression is = to the same expression with the && and || switched and everything is !
boolean x = true;
boolean y = true;

if (x&&y) {
    System.out.println("test1");
}

if (!(!x||!y)) {
    System.out.println("test2");
}
test1
test2
  • Comparing Numbers: if x == y?
int x = 6;
int y = 5;

if (x==y) {
    System.out.println("test1");
}

y = 6;

if (x==y) {
    System.out.println("test2");
}
|   int x = 6;
incompatible types: int cannot be converted to Object
  • Comparing Strings: String.equals(otherString); equals compares the values and == compares the datatype, so obj.getVal() == obj2.getVal will return false, but .equals will return true
public class test {
    static String x = "hello";
    static String y = "hello";

    public static void main(String[] args) {
        
        if (x.equals(y)) {
            System.out.println("test2");
        }
    
        if (x == y) {
            System.out.println("test1");
        }
    }
}

test.main(null);
test2
test1
  • Comparing objects: Object.equals(otherObject);
public class Object {
    
    private String value;

    public Object(String value) {
        this.value = value;
    }
    public String getValue() {
        return this.value;
    }
}

Object obj1 = new Object("hello");
Object obj2 = new Object("hello");

if (obj1.getValue() == obj2.getValue()) {
    System.out.println("test1");
}

if (obj1.getValue().equals(obj2.getValue())) {
    System.out.println("test2");
}
test1
test2
  • For loop: for (int i = 5; i >0; i--) {} use this for when you need to count by more than one or its not in a list
for (int i = 0; i <= 5; i++) {
    System.out.println(i);
}
0
1
2
3
4
5
  • Enhanced for loop: for (int i : arr) {} use this when you need to perform a method on each item in an arrayList
import java.util.ArrayList;
import java.util.Arrays;

public class test {

    public static void main(String[] args) {
        
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);

        int count = 0;

        for (int i : list) {
            count += i;
        }

        System.out.println(count);
    }
}

test.main(null);
6
  • While loop vs Do while loop: while loop runs while a conditions true, while do while loop runs once and then checks the condition
public class test {

    public static void main(String[] args) {

        int x = 13;
        
        do {
            System.out.println(x);
            if (x%2==0) {
            x /= 2;
            } else {
            x = 3*x+1;
            }
        } while (x!=1);
    }
}

test.main(null);
13
40
20
10
5
16
8
4
2
  • Nested loop: used when you want to search a multidimensional arraylist (2d 3d Array etc)
public class test {

    public static void main(String[] args) {
        String[][] array = {
            {"brook", "bknight", "bbishop", "bqueen", "bking", "bbishop", "bknight", "brook"},
            {"bpawn", "bpawn", "bpawn", "bpawn", "bpawn", "bpawn", "bpawn", "bpawn"},
            {"", "", "", "", "", "", "", ""},
            {"", "", "", "", "", "", "", ""},
            {"", "", "", "", "", "", "", ""},
            {"", "", "", "", "", "", "", ""},
            {"", "", "", "", "", "", "", ""},
            {"", "", "", "", "", "", "", ""},
            {"wpawn", "wpawn", "wpawn", "wpawn", "wpawn", "wpawn", "wpawn", "wpawn"},
            {"wrook", "wknight", "wbishop", "wqueen", "wking", "wbishop", "wknight", "wrook"}
            };
        
        int count = 0;

        for (String[] row : array) {
            for (String piece : row) {
                if (piece.equals("bpawn")) {
                    count++;
                }
            }
        }

        System.out.println(count);
    }
}

test.main(null);
8
  • Creating a class: public class Class {} should have a constructor and can have a 1 arguement constructor or multiple arguement constructor. Name of class should be caps and the name of the file.
  • Constructor: allocates parameters to the methods private variables. Can be a no arguement constructor, multi arguement constructor, or all arg constructor
  • Accesor method: example is a getter. returns a variable type. Just returns this.var() so you can access a private variable for a specific object
  • Mutator method: Updates a variable inside of the object. Doesnt have to return something in which it would be void.
  • Overloading a method: it lets you pass different combinations of parameters
public class Piece {    // this is a class

    private String type;

    // Overloading a method except this method is a constructor but does the same thing
    public Piece() {    // this is a no arg constructor
        this.type = "piece default";
    }

    public Piece(String type) { // this is a arg constructor
        this.type = type;
    }

    public String getType() {   // this is a accesor method
        return this.type;
    }  

    public void getTypeToString() {
        System.out.println(this.getType());
    }

    public void setType(String type) {  // this is a mutator method
        this.type = type;
    }

    public static void main(String[] args) {
        
        Piece thing1 = new Piece();
        Piece thing2 = new Piece("piece 2");

        thing1.getTypeToString();
        thing2.getTypeToString();

        thing1.setType("updated type");
        thing1.getTypeToString();
    }
}

Piece.main(null);
piece default
piece 2
updated type
public class test {

    public static void main(String[] args) {
    
    }
}

test.main(null);
  • Static variable is a variable that does not update (you can use final). Class variable is a variable somethig that can be accessed from in and out of the class. private is for something that can only be used by the object. Protectdd is like public but it cant be accessed outside of the package
  • Static is for methods that belong to the whole class instead of just one object.
  • this: this is used to call specific variables of an object (this.variable)
  • main method: This is typically used as a tester method to test all of the variables and functions work in a POJO. It lets you test your code even if it depends on other files to work
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Budget {

    // static variable
    static ArrayList<String> months = new ArrayList<>(Arrays.asList(
        "january",
        "february",
        "march",
        "april",
        "may",
        "june",
        "july",
        "august",
        "september",
        "october",
        "november",
        "december"
    ));

    private HashMap<String, Integer> monthlyExpenses;
    private HashMap<String, Integer> monthlyBudget;

    public Budget() {
        this.monthlyExpenses = new HashMap<>();
        this.monthlyBudget = new HashMap<>();
    }

    public void setMonthlyExpenses() {

        Scanner scanner = new Scanner(System.in);

        for (String month : months) {
        
            System.out.println("Enter the monthly expenses for " + month + ": ");
            String input = scanner.nextLine();
            this.monthlyExpenses.put(month, Integer.parseInt(input));

        }

        
    }
    
    public void setMonthlyBudget() {
     
        Scanner scanner = new Scanner(System.in);

        for (String month : months) {
        
            System.out.println("Enter the monthly budget for " + month + ": ");
            String input = scanner.nextLine();
            this.monthlyBudget.put(month, Integer.parseInt(input));

        }

    }

    public int findDifference() {
        int sum = 0;

        for (String month : months) {
            sum += monthlyBudget.get(month);
            sum -= monthlyExpenses.get(month);
        }

        return sum;
    }

    public void findDifferenceToString() {  // this keyword
        System.out.println("Your difference is " + this.findDifference());
    }

    // static method and also a main method
    public static void main(String[] args) {
        Budget budget = new Budget();
        budget.setMonthlyBudget();
        budget.setMonthlyExpenses();
        budget.findDifferenceToString();
    }
}

Budget.main(null);
  • Inheritance: allows you to extend one java file and its methods and variables into another and save code (ie flower extends plant because a flower has all the attributes of a plant)
  • subclass construcor: instead of instantiating as this.var = var set it as super(var)
  • overriding a method: overrides an inherited method so that it does something else
  • Polymorphism: allows you to implement the smae method in different ways by overloading, overriding, or latebinding
  • Abstract class & method: a class that cant create objects but can only be extended. The method can also not be used but only extended
// abstract class
abstract class Animal {
    private int height;
    private String name;

    public Animal(String name, int height) {
        this.name = name;
        this.height = height;
    }

    public void bite() {
        System.out.println("nom!");
    }

    public String getName() {
        return this.name;
    }
}
// Inheritance
public class Dog extends Animal {

    private int speed;

    public Dog(String name, int height, int speed) {
        
        // Subclass constructor
        super(name, height);
        this.speed = speed;
    }

    public void bark() {
        System.out.println("woof!");
    }

    // Overriding a method
    @Override
    public void bite() {
        System.out.println("chomp!");
    }

    public static void main(String[] args) {
        Animal dog = new Dog("todd", 4, 12);
        dog.bite();
        System.out.println(dog.getName());
        dog.bark();
    }
}

Dog.main(null);
|   // Inheritance
|   public class Dog extends Animal {
|   
|       private int speed;
|   
|       public Dog(String name, int height, int speed) {
|           
|           // Subclass constructor
|           super(name, height);
|           this.speed = speed;
|       }
|   
|       public void bark() {
|           System.out.println("woof!");
|       }
|   
|       // Overriding a method
|       @Override
|       public void bite() {
|           System.out.println("chomp!");
|       }
|   
|       public static void main(String[] args) {
|           Animal dog = new Dog("todd", 4, 12);
|           dog.bite();
|           System.out.println(dog.getName());
|           dog.bark();
|       }
|   }
Unresolved dependencies:
   - method bark()
  • Standard method: toString() parses something into a string, equals() compares the values of two things, and hashCode() hashes something and encrypts it
public class User {

    private int hashedPassword;
    
    public User() {
        this.hashedPassword = "Passwordles".hashCode();
    }

    public User(String password) {
        // hashed password never actually stores the real password
        this.hashedPassword = password.hashCode();
    }

    public boolean passwordChecker(String guess) {

        // compares if two values are equal
        if (this.hashedPassword.Equals(guess.hashCode())) {
            return true;
        } else {
            return false;
        }
    }

    // to string converts the results of a method into a string so it can be seen
    public void passwordCheckerToString(boolean isCorrect) {
        if (isCorrect) {
            System.out.println("The password is correct");
        } else {
            System.out.println("The password is incorrect");
        }
    }

    public static void main(String[] args) {
        User user1 = new User();
        user1.passwordCheckerToString(user1.passwordChecker("password"));
        user1.passwordCheckerToString(user1.passwordChecker("Passwordles"));
    }

}

User.main(null);
The password is incorrect
The password is correct
  • Late binding of object: Used to instantiate an object when the type of that object is unknown
public class Bird {

}
public class Chicken {
    
    public static void main(String[] args) {
        
        Bird object;

        // late binding
        object = new Chicken();
    }
}
  • Big O notation: Allows you to estimate how effective a segment of code is
    • Hashmap: O(1)
    • Binary search: O(log N)
    • Single loop: O(n)
    • Nested loop: O(n^2)