Unit 1 - Primitive Types
Primities
- primitives are lowercase
- nonprimitives are uppercase
- Collegeboard need-to-know primitives: Boolean, Int, Double
Casting
- changing a variable from one data type to another
- widening: happens automatically, going from smaller to larger data type
- narrowing: need to declare to address overflow error, going from larger to smaller data type
Operators
- (+) is addition
- (-) is subtraction
- (/) is division
- (*) is multiplication
- (%) finds the remainder
- example: x += 7 increases the varaible by 7
Casting for Division
- When dividing a double by a double the output is a double, and when dividing an integer with an integer the output is an integer, but that is not always the case. like 7/3 is not an integer. To fix this you need to cast the denominator to a double like 7/(double)3
int x = 8/3;
System.out.println(x);
// vs casting with double
double y = 8/(double)3;
System.out.println(y);
double x = 8.0 / 3;
int rounded = (int)(x + 0.5);
System.out.println("8.0/3 = " + x);
System.out.println("8/3 truncated: " + (int)x );
System.out.println("8.0/3 rounded to nearest int: " + rounded);
Unit 2 - Using Objects
What is OOP
- OOP is object oriented programming
- programming pardigm centered around objects
Classes and Objects
- classes are templates or a blueprint, contatin data
- objects are instances of a class, need to be instantiated
- methods are functions of a class
- Method Declaration consists of 6 components: Access modifer, Return type, Method name, Parameter list, Exception list, Method body
- Calling a method allows for code reuse, optimization, and organization
- To call an object's method you have to make an object reference
Wrapper Classes, why wrap int, double. Show examples
- A wrapper class is a class whose object contains primitive data types. Wrapper classes allow us to wrap a primitive value into the class object
// in newer versions of Java
Integer a = 2;
Double b = 3.5;
System.out.println(a);
System.out.println(b)
Concatenation
- Concatenation is the joining or data.
- Some examples are:
- string concatenation and string and object concatenation with toString() method
Math class, specifically Random usage
- Math class contains methods from the java.lang package
- These are static methods so you call them by using ClassName.methodName()
- Ex: math.random
System.out.println(Math.abs(-2));
System.out.println(Math.pow(19, 6));
System.out.println(Math.sqrt(500));
Unit 3 - Boolean Expression and If Statements
Boolean Expressions
A Boolean expression is a logical statement that is either True or False. Boolean expressions can compare any type of data if both parts of the expression have the same basic data type. A Boolean expression can be a combination of the Boolean constants true or false.
All non zero numbers are true
- the most common example: 1 = true; 0 = false
- == checks for comparison
- not to be confused with = which is assignment
- != checks for inequality
- <, >, >=, <=
- these operands do their normal functions
- || relates to the or function
- && related to and function
Logic Gates
Logic gates are used to carry out logical operations on single or multiple binary inputs and give one binary output. In simple terms, logic gates are the electronic circuits in a digital system.
- Buffer/YES : output is the same as the input
- NOT : output is the opposite of the input
- AND : returns true if both inputs are true
- NAND : returns false if both inputs are true
- OR : returns true if at least one of the inputs are true, returns false if both inputs are false
- NOR : returns false if at least one of the inputs are true, returns true if both inputs are false
- XOR : returns true if the two inputs are different
- XNOR : returns true if two inputs are the same
Compound Boolean Expression
- Compound Boolean Expressions are like && which means that the code is executed based on if the first and second conditions are true.
De Morgan's Law
DeMorgan's laws tell us how to transform logical expressions (with multiple AND and/or OR statements) using the NOT operator.
- (not(A and B)) is equal to (not A or not B)
- (not(A or B)) is equal to (not A and not B)
- (not(A and B)) is not equal to (not A and not B) Syntax
- union symbol (∪) : OR
- intersection symbol (∩) : AND
- apostrophe (') : NOT
Truth Tables
A truth table is a breakdown of a logic function by listing all possible values the function can attain. Such a table typically contains several rows and columns, with the top row representing the logical variables and combinations, in increasing complexity leading up to the final function.
Compare methods
- Comparing numbers: compare() method of Integer class of java.lang package compares two integer values (x, y) given as a parameter and returns the value zero if (x==y), if (x < y) then it returns a value less than zero and if (x > y) then it returns a value greater than zero.
- Comparing strings: if (string1 > string2) it returns a positive value. If (string1 == string2) it returns 0. If (string1 < string2) it returns a negative value
- Comparing objects: == and != operators
int a = 10;
int b = 20;
// as 10 less than 20, Output will be a value less than zero
System.out.println(Integer.compare(a, b));
Unit 4 - Iteration
Operators
- ++ increment by 1
- -- decrement by 1
While loop
- while a specific condition is executed the condition will run
Recursion Loop
- Put your function inside its own functions
Nested Iteration
- technique that coders use
- any of the three loops can be used
- put a loop inside a loop
For loop
- for each thing in a sequence it will loop
- will run for each thing that is true
- Loops that runs for a set number of iterations
- The enhanced for loop iterates in a sequence (counter is always increased by one)
public class LoopConversion
{
public static void main(String[] args)
{
int count = 0;
//convert to for loop
for (count=0; count < 5;)
{
System.out.println("count is " + count);
count++;
}
}
}
LoopConversion.main(null);
public class WhileLoop
{
public static void main(String[] args)
{
int i = 0;
//convert to for loop
while (i < 5)
{
System.out.println("i is " + i);
i++;
}
}
}
WhileLoop.main(null);
class Main {
public static void main(String[] args) {
int weeks = 3;
int days = 7;
// outer loop prints weeks
for (int i = 1; i <= weeks; ++i) {
System.out.println("Week: " + i);
// inner loop prints days
for (int j = 1; j <= days; ++j) {
System.out.println(" Day: " + j);
}
}
}
}
Main.main(null);
Unit 5 - Writing Classes
Classes
- classes are "blueprints" that we use to instantiate objects
- classes are an essential aspect of OOP
Anatomy
Object
- state, attributes, behavior
- intance of a class
- represented by an instance in the program
Class
- defines an abstract data type
- object references: string variable
- instance variables : attributes, behaviors, data for objects
Functions
- methods : the action or behavior of an object
- constructor : special method for object instantiation, sets the initial values for variables, default constructors have no arguments
- main : tests the class
accesor
- Get of Methods or getters
- allows you to access the values of varaibles
- returns a copy of the variable
- non-void methods
- value of specific data type returned
- no parameteres
mutator
- Set of Methods or setters
- allows you to change to values of variables
- non-void methods
- no value returned
- will take parameters
Writing Methods
- method_definition(Modifiers, Return Type)
- method_signature(Name, Parameters)
- method_body(Code)
- "Object.method()"
Key Words
/Static Modifiers
- used after access modifiers
- denotes as belonging to a class /This
- refers to the constructor that is being called Access Modifiers
- Restricts scope of classes, variables and functions
Scope and Access
/Scope
- Where a variable can be accessed/used
- Class level: Instance Var
- Method level: Local, parameter var
- Block level: If else/ Loops
Public, Private, Protected
- Public - can be accessed within and outside of class
- Private - can only be accessed within the class
- Protected - can be accessed by all classes in the package and all subclass outside of the package
class Lamp {
boolean isOn;
void turnOn() {
isOn = true;
System.out.println("is the light on? " + isOn);
}
void turnOff() {
isOn = false;
System.out.println("is the light on? " + isOn);
}
}
class Main {
public static void main(String[] args) {
// create objects led and halogen
Lamp a = new Lamp();
Lamp b = new Lamp();
// turn on the light by
// calling method turnOn()
a.turnOn();
// turn off the light by
// calling method turnOff()
b.turnOff();
}
}
Main.main(null);
Inheritance, extends
- Inheritance is a way for attributes and methods to be inherited from one class to another
- Extends is what allows you to bring those attributes from one class to another
- example: fibonacci
Subclass constructor, super Keyword2
- A subclass inherits all the members from the superclass while the constructor of the superclass can be invoked from the subclass
- Super keyword refers to superclass objects; it is used to call superclass methods and to access the superclass costructor
Overloading a method, same name different parameters
- Overloading a method in java is a feature that allows a class to have more than one method with the same name, but with different parameters
- Instead of defining two methods that do the same thing, it is better to overload one
Overriding a method, same signature of a method
- Method overriding occurs when a subclass has the same method as the parent class
- When a subclass provides a particular implementation of a method declared by one of its parent classes
Abstract Class, Abstract Method
- A abstract class cannot be instantiated but they can be subclassed
- An abstract method is a method that has just the method definition but does not contain implementation
Standard methods: toString(), equals(), hashCode()
- Standard methods are blocks of code that can be called from another location in the program or class
- toString(); returns the given value in string format
- equals(); compares two strings and returns true if the strings are equal
- hashCode(); returns an integer value generated by a hashing algorithm
Late binding of object, referencing superclass object, ie Animal a = new Chicken(); Animal b = new Goat();
- Late binding is when the compiler should perform no argument checks, no type checks on a method call and should leave it all to the runtime
Polymorphism: any of overloading, overriding, late binding
- Polymorphism is the ability of a class to provide different implementations of a method depending on the type of object that is passed to the method
- Allows us to perform the same action in many different ways
Big O notation for Hash map, Binary Search, Single loop, Nested Loop
- Big O notation describes the set of all algorithms that run no worse than a certain speed, no better than a certain speed, and at a certain speed
- Shows the number of operations it will perform