Grades

Unit Grade Homework
1: Primitives 0.93/1 2006 FRQ
2: Objects 1/1 (taught lesson) Lesson notebook
3: Booleans 0/1 Did not complete
4: Iteration 0.8/1 Quiz done separately, notes
5: Classes 0.9/1 2021 FRQ
Total 3.63/5

Unit 1: Primitives

Link to notebook

Summary: Primitives are a data type in Java with limited properties (lowercase, varying size, can't call methods). Primitive data types include bool, int, char, double, etc. They can be compared with == instead of .equals().

Casting

Casting is when you assign the value of a primitive to another primitive type. It can either widen (smaller to larger type) or narrow (larger to smaller type).

// In this example you can see the difference in data sizes
int x = 2;
int y = 4;

// without casting 
System.out.println(y/x);

// With casting
System.out.println((double)y/(double)x);
2
2.0
// this example shows truncating with narrowing
double a = 3.7;

// without casting
System.out.println(a);

// with casting
System.out.println((int)a);
3.7
3

Wrapper

Wrapper classes allow us to use primitives as objects (capitalized data type, think int to Int). One example of this is ArrayList.

// assign primitive variable
int x = 1;

// instantiate arraylist of integers
ArrayList<Integer> intList = new ArrayList<Integer>();

// create Integer from int
Integer a_wrapper = new Integer(x);
intList.add(x);

System.out.println(intList);
[1]

Unit 2: Objects

Link to notebook

Summary: Objects are instances of a class. They inherit properties from the class, which greatly simplifies attribute management. Methods in objects can be void or return something.

Concatenation

Concatenation allows you to combine strings. You can also combine non-strings with toString()

// assigning variables
String x = "Hippo";
String y = "pota";
String z = "mus";
int a = 12;

// concatenating all strings
System.out.println(x + y + z);

// concatenate x "Hippo" and int a
System.out.println(x + a);
Hippopotamus
Hippo12

Math class

Math class allows you to use more math functions such as finding the max of two numbers or square roots. In this case, we will focus on Math.random()

import java.lang.Math; // importing

// by itself
Math.random(); // generates random number between 0 and 1, inclusive

// can multiply by integer to expand range (0-100)
int randomNumber = (int)(Math.random() * 101);  
System.out.println(randomNumber);
48

Comparisons

Numbers can be compared with ==. Strings (objects) must be compared with equals().

// assigning variables
int x = 2;
int y = 2;
int z = 1;

// return True
System.out.println(x == y);

// return False
System.out.println(x == z);
true
false
// assigning strings
String a = "Hi";
String b = "Bye";
String c = "Hi";

// this should return false
System.out.println(a.equals(b));
// this should return true
System.out.println(a.equals(c));
false
true

Unit 3: Booleans

Booleans can only hold one of two possible values, true or false. They are the simplest data type, and are often used to check loop conditions with >, <, or ==.

Compound boolean expression

A compound boolean is one that involves several boolean variables.

// simple expression
boolean x = true;
boolean y = false;
System.out.println(x);
System.out.println(y);

// compound
boolean compound = !(x && y) || (x || y) && !(x || y);
System.out.println(compound)
true
false
true

Truth Tables

Truth tables are used to view all the possible values of a boolean, given all possible values of the variables they use.

DeMorgan's Law

Essentially !(a && b) = !a || !b and !(a || b) = !a && !b. You distribute the negation of the and expression to each of the individual components of the or expression, and vice versa.

boolean x = false;
boolean y = false;

// De Morgan's Law two
boolean testDMLaw = !(!x || !y) && !(!x && !y);
System.out.println(testDMLaw);
false

Unit 4: Iteration

Link to notebook Iteration uses loops that continue based on certain conditions. You can have several loops nested in one another

  • For continues
  • While continues while a certain condition(s) remain true
  • Recursive recurs by calling itself

For loops

For loops continue for as long as a certain condition is met, and end after.

// regular for loop
for (int i = 1; i<=9; i+=2) { // condition for ending loop is when current integer is <= 9
    System.out.println(i);
  }
1
3
5
7
9
// enhanced loop for array
int[] array = {2, 4, 6, 8, 10};

// syntax is for (dataType variable : array)
for (int a : array) {
    System.out.println(a);
  }
2
4
6
8
10

While loops

While loops continue while a condition is met (checking each iteration before). Do while loops check the condition after the iteration.

// regular while loop
int i = 0;
while (i <= 5) {
  System.out.println(i);
  i++;
}
0
1
2
3
4
5
// if i set the parameter i to >5 in the beginning it will not output anything
int i = 6; // regular while loop
while (i <= 5) {
  System.out.println(i);
  i++;
}
// do while loop
int i = 0;
do {
  System.out.println(i);
  i++;
}
while (i <= 5); // condition is at the end
0
1
2
3
4
5

Nested loops

A nested loop is a loop inside a loop. This is very useful for managing program abstraction and complexity.

// Outer loop
for (int i = 1; i <= 3; i++) { // printing i and adding 1 each time, 3 times
    System.out.println("Outer: " + i); 
    
    // Inner loop
    for (int j = 1; j <= 2; j++) { // runs 2 times for each loop of the outer
      System.out.println("  Inner: " + j); 
    }
  }
Outer: 1
  Inner: 1
  Inner: 2
Outer: 2
  Inner: 1
  Inner: 2
Outer: 3
  Inner: 1
  Inner: 2

Unit 5: Classes

Link to notebook

A class is a blueprint with properties and methods. They serve as blueprints for creating objects. Properties can either be public or private (accessible outside of the class), and methods can modify objects (object-oriented). You can use methods such as getter and setter to modify the class properties.

Creating a Class

Use the "class" keyword, capitalize first letter and use CamelCase

class ThisClass {
    
}

Main and Tester methods

Main method is used to test a class, automatically called and can test other methods.

class Hello {
    public static void main (String[] args) {
        Hello object = new Hello();
    }
}

Hello.main(null);

This keyword

Used to access properties of a class.

Constructor

Used to initialize objects.

public class Main {
    int a;

    public Main(int a) {
        this.a = a; // using this to access a
      }
    public static void main(String[] args) {
        Main newObject = new Main(2); // this is the constructor
        System.out.println(newObject.a);
    }
}

Main.main(null);
2

Accessor/Getter method

Allows you to get values of variables, no parameters and not void since it returns something.

Mutator/Setter method

Allows you to change variable values, void since it takes parameters but doesn't return anything.

public class Main {
    private String hello; // private variable

    public String getHello() { // getting private hello str
        return hello;
    }

    public void setHello (String hello) { // method to set hello
        this.hello = hello;
    }
}

Access modifier

Types of access: public, private (in-class only), protected (all classes in package and subclasses outside).

public void PublicTest() {
    System.out.println("Hello, world!");
}

private void PrivateTest() {
    System.out.println("Hello, world!");
}

protected void ProtectedTest() {
    System.out.println("Hello, world!");
}