1-5 Blog
With Vocab
- Unit 1 - Primitive Types
- Unit 2 - Using Objects
- Unit 3 - Boolean Expressions and If Statements
- Unit 4 - Iteration
- Unit 5 - Writing Classes
- Classes
- Anatomy of a Class
- Accessor Methods
- Mutator Methods
- Writing Classses
- Key Words
- Scope and Access
- Public, Private, Protected
- Inheritance, extends
- Subclass constructor, super Keyword2
- Overloading a method, same name different parameters
- Overriding a method, same signature of a method
- Abstract Class, Abstract Method
- Standard methods: toString(), equals(), hashCode()
- Late binding of object, referencing superclass object, ie Animal a = new Chicken(); Animal b = new Goat();
- Polymorphism: any of overloading, overriding, late binding
- Big O notation for Hash map, Binary Search, Single loop, Nested Loop
Unit 1 - Primitive Types
Primitives
- 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
int x = 7/3;
System.out.println(x);
// vs casting with double
double y = 7/(double)3;
System.out.println(y);
double x = 7.0 / 3;
int rounded = (int)(x + 0.5);
System.out.println("7.0/3 = " + x);
System.out.println("7/3 truncated: " + (int)x );
System.out.println("7.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
// in newer versions of Java
Integer a = 2;
Double b = 3.5;
System.out.println(a);
System.out.println(b)
System.out.println(Math.abs(-2));
System.out.println(Math.pow(19, 6));
System.out.println(Math.sqrt(500));
Unit 3 - Boolean Expressions 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
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
For loop
- for each thing in a sequence it will loop
- will run for each thing that is true
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
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
- blueprints for instantiating objects
- model real world objects in code
Anatomy of a Class
- Object
- instance of a class
- Class
- defines an abstract type
- Methods
- behaviors you can get the object to do
- Constructors
- creates the object
- special method for object instantiation
- default constructor has no arguments
- Main method
- test the class
Accessor Methods
- Get of methods or getters
- allows you to get values of variables
- return a copy
- Non-void Methods
- value of specific data type returned
- no parameteres
Mutator Methods
- Set of methods or setters
- allows you to change the value of the variable
- Void Methods
- no value returned
- will take parameters
Writing Classses
- Method defintion
- Method Signature
- Method body
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