Certified Hood Study Material

Code credits by JM

Extra Materials:

1 | Click here

2 | Click here

3 | FRQ Scoring Guidelines

System.out.println(... args) → prints args then prints a new line (\n)

  1. Can take no arguments to just print a new line

System.out.print(... args) → println without the new line at the end

Comments:

Common Core Terms:

Boxed Expression (here either means .equals() or a wrapper class in this document)

Primitives :

Int:

-boxed expression : (java.lang.Integer)

  1. Primarily used for generics, such defining what type of objects are going in an ArrayList (java.util.ArrayList)

-represents a whole number or a regular integer

-IS NOT FLOATING POINT

-defaults initialization to 0

-an int can be directly assigned to a double

-can hold in the range of -2,147,483,648 to 2,147,483,647

  1. If the value of this integer is exceeded, an IntegerOverflowException will be thrown and the entered value will be become minuscule (negative)
  2. Integer.MIN_VALUE & Integer.MAX_VALUE holds these ranges

Double:

-boxed expression : (java.lang.Double)

  1. Primarily used for generics, such as defining what type of objects are going in an Arraylist (java.util.ArrayList)

-represents a FLOATING POINT EXPRESSION

-default initialization to 0.0

-a double cannot be directly assigned to an int

-can hold in the range 1.7976931348623157*10^308 to 4.9406564584124654*10^-324

^typo here, the second line should start with “double” not “int”

Char:

-boxed expression : (java.lang.Character)

  1. Primarily used for generics, such as defining what type of objects are going in an Arraylist (java.util.ArrayList)

-represents a single character (ASCII or Unicode, but will be only ASCII in APCSA)

  1. Thus meaning a char can be easily cast to an Integer to get the ASCII value of that char

-an unsigned 16-bit integer (unsigned meaning it is never negative) and is in the range of: 0 to 65,535

-declared with single quotes (‘)

Boolean:

-boxed expression : (java.lang.Boolean)

  1. Primarily used for generics, such as defining what type of objects are going in an ArrayList (java.util.ArrayList)

-represents a true or false and only these two values

-cannot be cast to another type without doing some like:

 

-initialized default with false

String*:

-boxed expression : (java.lang.String)

  1. Primarily used for generics, such as defining what type of objects are going in an ArrayList (java.util.ArrayList)

-strings are immutable, meaning they are READ-ONLY

-escape codes such as \n \” \’ \\ can be used to print special characters or to override certain things like quotes

-represents an array or list of characters to maybe form a word, phrase, etc.

-checking content between strings should always use the .equals() method

String Literal vs String Object:

-literals with the same content will point to the same literal in memory (shared)

-objects will create new strings no matter if the memory pool already has similar strings (heap)

Complex Types:

-Complex Types (what I call them :/) are things like 2D arrays, 1D arrays of objects or primitives.

-to access a member of the array we do Array[index] where index is from 0 to arr.length - 1

Note: specifying the array length is from 1 to n while specifying index is from 0 to arr.length - 1

-An array is just a list of objects/primitives

-Arrays are declared with []:

^the number at the end specifies the size of the array, so this array declared can hold 3 values

-going out of bounds would throw an IndexOutBoundsException (AKA ArrayIndexOutOfBoundsException)

-to get the length of the array, we use Array.length

-Array of Objects:

^holds 60 objects in total

-go to the loop section to see how you can modify or get values from primitive arrays

-to swap things within an array, we need to create a temporary variables, because the original swap will wipe the old value:

^this swaps the start and the end of the array

-2D arrays are just regular 1D arrays stacked on top of each other (with cols and rows)

-they are declared with two []:

-this could be visualized as:

-a 4x4 2D array is this

-to get the rows length, we do Array2D.length

-to get the cols length, we do Array2D[row].length (in the AP-CSA subset, all 2D arrays are rectangular, so you most of the time would be doing Array2D[0].length)

You could go to a 3D array or an nD array, but those are not covered on this AP curriculum, so I am not going over it

Methods:

-methods are used to perform repetitive actions

-they can be static or non-static (below for def)

-methods can be overridden (from inheritance) unless they are declared as final

-methods can have a return type, but if not, they are “void” (you can still type the keyword “return” without a value to jump out of the method)

Casting & Operations :

Custom Classes:

Math → abs, pow, random, sqrt

abs → absolute value: Math.abs(...)

Pow → raises a value to a power Math.pow(base, power/exponent)

Random → returns a (pseudo) random between 0.0 and 1.0 exclusive

Sqrt → square root Math.sqrt(...)

Math.random between a start and a finish:

Final:

-variables with final cannot have their value modified

-methods with final cannot be overridden

-classes with final cannot be inherited (extension)

Rounding:

-to round an integer up, we can do:

-to round down an integer, we can do:

Explicit & Implicit Casting:

-explicit casting is when the type to cast to is EXPLICITLY STATED:

-implicit casting is when the compiler handles the casting automatically and follows this tree of who gets the upper cast:

(int is lower than double, which means whenever a double and an int are in the same expression, the final value would always be a double)

-this means int (+,/,-,*,%) double → double

Operations:

-assignment is done with the “=” symbol

-arithmetic operations are:

+,-,*,/,%

“+” --> Addition, adds two numbers together

“-” → Subtraction, subtracts two numbers together

“*” → Multiplication, multiplies two numbers together

“/” → Division, divides two numbers together

“%” → Modulo, divides two numbers together and returns the remainder

-incrementing and decrementing are represented by:

“--” → Subtract 1 to the variable; decrement per 1

“++” → Add 1 to the variable; increment per 1

Refresher on what a remainder, it is the amount left over after division, so for example: 10%4 would give us 2 because 4*2 = 8, 10-8 = 2, which is the amount left over: 2. Another good usage of the modulo operation is to check if a number is even or odd:

-compound operations are the standard arithmetic operations by along with the assignment operator:

“+=” add and assign value

“-=” subtract and assign value

“/=” divide and assign value

“*=” multiple and assign value

“%=” mod and assign value

However, compound operators are basically just:

And is the same as doing the last line as:

-you can also concat or append two strings together with “+” or “+=”, “+” can be used during the creation of a string to put multiple variables together (no matter type)

-an arithmetic error can occur if one tries to divide by zero, this is known as a ArithmeticException “Divide by zero”

Overloading:

-overloading is when there are multiple methods or constructors with the same name, but different parameters

Logical Operators :

-Comparison between values (primitives)

-returns a boolean if the specified operands are within the logic operations

-the curriculum covers:

<,>,==,!=,>=,<=,!

“>” - greater than; exclusive

“<” - less than; exclusive

“==” - is equal to (NOTE: USE the METHOD WHEN COMPARING STRINGS OR OBJECTS)

“!=” - is not equal to

“!” - NOT (logical NOT not bitwise NOT [~])

“>=” - greater than or equal to; inclusive

“<=” - smaller than or equal to; inclusive

IF/IF-ELSE/IF-ELSE_IF

-if statements follow that the parameterized argument is TRUE (boolean), the code piece in the brackets would be run:

^this would print Hi because, 10 % 2 is equal to 0, thus is true

-if-else statements provide an alternative passageway if the previous if-statement fails (FALSE):

^this would print :( because 7 % 2 is not 0, however if is it was 10 % 2 instead of 7 %2, it would print :) because it meets the requirements

-if-else_if provides multiples passageways for if the user wants to have different results for specific conditions, an else can also be used in conjunction to operate on else cases:

^this would print “ONE”, however if i was equal to 1, it would print “TWO”, otherwise it would print “SOMETHING_ELSE”

Logical Expressions:

-anything that uses a logical operator:

  1. this would be a raw logical expression

  1. The logical expression would be 10 % 2 == 0

Combining Logical Expression:

-we can combine multiple logical expressions together using:

&&, ||

“&&” - Logical AND (not bitwise AND [ & ]), two sides of this operator must be TRUE

^here i MUST be 3 AND r MUST be 2 for the program to print HI

“||” - Logical OR (not bitwise OR [ | ]), any one of the sides that is TRUE

^here i can be EITHER 3 or 4, for the program to print HI

-along with this, we can put logical expressions within logical expressions as inside of parenthesis, doing this deeply would mean we need to follow DeMorgan’s Law:

Distribution of Logical/Boolean Operators “DeMorgan’s Law”

!(X && B) → !X || !B

!(X  || B) → !X && !B

Extras:

X || (C && R) → (X || C) & (X || R)

X && (C || R) → (X && C) || (X && R)

-a good way to do this is thinking of && like multiplication (*) and || as addition (+)

Nested Logical IF:

-you can put IF statements within each other:

Loops :

-loops are used to perform actions that occur for either an undefined amount of times or a defined amount of times without having to be repetitive

-there are 3 main types of loops:

Do-While - Similar to a regular While Loop, checks the conditions late

While - Checks the condition before running

For - Has a start, condition, and an increment

For-Each (Enhanced For Loop)

While & Do-While loops run until the parameterized logical expression becomes false

For -Loop structure:

For(start index, condition/end constraint, increment) { }

Example:

^starts from 0 goes to 5 (< is exclusive), goes up by 1

Usages (Looping through an array):

^this method of going through an array allows modifying and read values within an array

Sometimes, if we only need to look at the values of an array/arraylist, we can use a for-each loop:

^this method does not allow us to modify the value, and we do not know where we are in the array

Note to jump out of the loop, we use the “break” keyword.

2D Arrays:

Recursion:

A method that calls itself many times in order to accomplish something

-must have a valid base case in order to not throw an exception (in particular a StackOverflowException)

  1. A base case is the condition in which the recursion would be able to exit

-

Objects & Classes (+Design) :

-objects are instances of classes and has certain values/attributes

-a class defines the implementation of an object; blueprint

Objects (java.lang.Object):

-declared with the new keyword

-all user created classes and instance objects are children of java.lang.Object

-to check the equality between 2 objects, we use Object.equal(Object args) method

-an Object is declared by default as null and if tried to use without any guards to prevent it being null, such as:

Will throw a NullPointerException

Global & Local:

-variables can only be used in the most outer brackets that they are found at

Global: declared at the top of the class and can be used at anywhere in the class

Local: can only be used in the current brackets block

Visibility Modifiers:

Private, public

Data Encapsulation → “A pillar of OOP that protects data from being accessed or modified by any part of a program, except with explicit calls to the accessor methods and mutator methods”

Private → non-accessible and only within the class itself; not exposed

Public → Accessible; exposed (a variable without a visibility modifier is automatically marked as public)

Static & Non-Static:

Static → Belongs to the class

Non-Static → Belongs to the object instance

Class Design:

Instance variables: Should be declared as private and non-static variables

Constructors:

!!!! THIS keyword references the current instance, mostly used to reference something from the current class, for example another constructor this(...) or a variable “this.variableName”

Getter methods:

Setter Methods:

A good note is to name your getter and setter methods with the same name, except for the get and set part. The name must include only the instanceVariable name and the identifier get or set (in order to not inhibit the meaning).

Common Algorithms :

Linear Search, Binary Search, Selection Sort, Insertion Sort, Merge Sort

HOW TO RECURSION

TRACE THE CODE!!!

Class Mechanics & Inheritance & Polymorphism :

-there can be parent classes and child classes

-we use the “extend” keyword to define that a class will be a child of a parent class:

-the super keyword is called in the constructor ON THE FIRST LINE if needed to specify any parameters needed to be passed to the parent

-super keyword behaves similarly to this keyword

Extension:

-when a child class extends a parent class, all the parents class are automatically inherited

  1. This means that within the child class we can call any method from the parent class
  2. All methods inherited are overridden if the child class have redefined them, meaning the methods have new content, the child class’s implementation would be called unless super is called on the method, then the parents method is called and then the child

^this would print if the child class’s print was called first: “HiWorld”

  1. WE CANNOT CALL CHILD CLASS METHODS FROM THE PARENT CLASS

-if you used line 2 from ^ above, and you want to call a child method, you need to explicitly cast that object:

ArrayList & Primitive Data Structures :

-imported as java.util.ArrayList and/or java.util.List

-List is an interface

-declared as:

^you can replace Object with another Object type or another Object

-for primitives, use wrapper classes (Double, Integer, Character, Boolean)

Common methods in an arraylist :

NOTE WHENEVER YOU REMOVE SOMETHING FROM THE ARRAYLIST, ALL THE ITEMS IN THE ARRAYLIST SHIFT DOWNWARDS, THUS MAKE SURE TO KEEP YOUR INDEX TRACKING CORRECT

Using loops: