General Notes

Primitives are predefined, lowercase, "Primitives", which cannot call methods, are a value, and can be different sizes based on what primitive it is. Non primitives are defined by the coder, are uppercase, "Reference types", which can call methods, can be null, and are all the same size


OOP or object oriented programming is a type of programming language. Classes are a template or blueprint from which objects are created. Objects are instances of a class. Methods are a set of code that perform a specific task. Classes in java can hace data members, methods, constructors, nested classes, and interfaces.


If/else statements allow you to create a code segment that has checks. If/ElseIf/Else statments can be used to decide different program outputs depending on what the user inputted. Switch statements are also an alternative method to if statements.


Iteration is worth 25% of the test so its pretty important. While loops, for loops, and recursion loops are types of loops. Another example is nested iteration, which can put a loop inside another loop.


Classes are a blueprint for instantiating objects. An object is an istance of a class, a class defines an abstract type, methods are the behaviors you get objects to perform. Constructors create the object, are a special method for object instantiation, default constructor has no arguements. Accessor methods are getters and setters, which allow you to get values of variables and return a copy, or edit a variable.


Common errors when making arrays include bound errors, unfilled and uninitialized arrays, when you allocate, or assign a single array variable, but not the whole array. You can use for loops to iterate through an array, by using array.length.

Terms

Casting(Division) -

this can be done by added a parenthesis in front of the value that you are trying to cast(which changes the type of that value/variable).

int num = 5;
int denom = 7;
double d = num / denom;
System.out.println("Without Cast: "+d);
double d = ((double) num) / denom;
System.out.println("With cast: "+d);
Without Cast: 0.0
With cast: 0.7142857142857143

Casting(Truncating or Rounding)-

examples of this include turning a double into an int.

double exampleDouble = 9.78d;
System.out.println("Not Rounded: " + exampleDouble);
int exampleInt = (int) exampleDouble;
System.out.println("Rounded: " + exampleInt);
Not Rounded: 9.78
Rounded: 9

Wrapper Classes -

these are classes in java where primitive data types can be used as objects. For examples, int to Integer, double to Double, char to Character. These are useful in cases such as arraylists, where they can only interact with objects, and not primitive types, that's why you would want to wrap an int, or a double

ArrayList<Integer> myNumbers = new ArrayList<Integer>(); // This example works because the arraylist recognizes the Integer object
ArrayList<int> myNumbers = new ArrayList<int>(); // This example doesn't work because the arraylist doesn't recognize the primitive type int

Integer intVariable = 456;
Double doubVaribale = 456.456;
|   ArrayList<int> myNumbers = new ArrayList<int>();
unexpected type
  required: reference
  found:    int

|   ArrayList<int> myNumbers = new ArrayList<int>();
unexpected type
  required: reference
  found:    int

Concatenation

the process of combining two or more strings to form a new string. It can also be referred in terms of combining values or variables

String word1 = "hello ";
String word2 = "world";

System.out.println(word1+word2);

word2 = word2+ " 10";

System.out.println(word1+word2);

word2 += " 20";

System.out.println(word1+word2);

//can also concat variables and string

int x = 40;
System.out.println(x+" adding words");
hello world
hello world 10
hello world 10 20
40 adding words

Java Math Class -

the java math class contains operations that can be used in code. It also contains values such as pi. For example Math.pi. Other functions include trig functions, log functions, and the random function.

import java.lang.Math;//must be imported at start of program
double rand = Math.random();
System.out.println("random: " + rand);
System.out.println("Cos: " + Math.cos(rand));
System.out.println("Log: " + Math.log(rand));
System.out.println("Square Root: " + Math.sqrt(rand));
random: 0.3938634112760282
Cos 0.9234333367381666
Log -0.931751101677047
Square Root 0.6275853816621514

Compound Boolean Expression

int x = 2;
int y = 3;
System.out.println("First Expression");
if(x > 0 && (y / x) == 3){
   System.out.println("True");
}else{
   System.out.println("False");
}
First Expression
False

Truth Table

boolean function that is a mathematical function that maps arguments to a value, where the allowable values of range (the function arguments) and domain (the function value) are just one of two values— true and false (or 0 and 1) NOT(x)= 1 if x is 0 0 if x is 1

XOR(x,y) 1 if x and y are different 0 otherwise

De Morgan’s Law

De Morgan's Law is a concept that shows that concepts in mathematics are related through their opposites. For example: not (A and B) -- not A or not B not (A or B) -- not A and not B

!(a && b) == (!a || !b)
!(a || b) == (!a && !b)

if(!(true)&&(true==false)){
    System.out.println("Demonstrates De Morgans law");
}

Comparing Numbers

comparing numbers is done through the == operator

int x = 2;
int y = 3;
int z = 2;

if(x == y){
    System.out.println("true");
}else{
    System.out.println("false");
}

if(x == z){
    System.out.println("true");
}else{
    System.out.println("false");
}
false
true

Comparing Strings

comparing strings is done through the .equals operator

String word1 = "world";
String word2 = "hello";
String word3 = "hello";

if(word1.equals("world")){
    System.out.println("true");
}else{
    System.out.println("false");
}

if(word2.equals(word1)){
    System.out.println("true");
}else{
    System.out.println("false");
}
true
false

Comparing Objects

comparing objects is done through ==

Integer x = new Integer(2);
Integer y = new Integer(2);
Integer z = new Integer(3);
x=y;

if(x == y){
    System.out.println("true");
}else{
    System.out.println("false");
}

if(z == 2){
    System.out.println("true");
}else{
    System.out.println("false");
}
true
false

Constructors

Since constructor can only return the object to class, it's implicitly done by java runtime and we are not supposed to add a return type to it.

Access Modifiers

Access modifiers help to restrict the scope of a class, constructor, variable, method, or data member. The different types of access modifiers are the default, private, protected, and public.

void HelloWorldDefault(){
    System.out.println("Hello World");
}

public void HelloWorldPublic(){
    System.out.println("Hello World");
}

private void HelloWorldPrivate(){
    System.out.println("Hello World");
}

protected void HelloWorldProtected(){
    System.out.println("Hello World");
}

This Keyword

the "this" keyword implies that the object passed in as a member variable of the class is the value passed in when you create an object of that class. Additionally, this can also be used to invoke current class constructor, invoke current class method, return the current class object, pass an argument in the method call, pass an argument in the constructor call

public class Example{
    int ex;

    // Constructor with a parameter
    public Example(int ex) {
      this.ex = ex;
    }

}

Inheritance and Extends with Examples of Subclass and Super Keyword

Inheritance in Java is the method to create a hierarchy between classes by inheriting from other classes.

public class Animal {

	private String eats;

	private int numLegs;

	public Animal(){}

	public Animal(String food, int legs){
		this.eats = food;
		this.numLegs = legs;
	}

	public String getEats() {
		return eats;
	}

	public void setEats(String eats) {
		this.eats = eats;
	}

	public int getNumLegs() {
		return numLegs;
	}

	public void setNumLegs(int numLegs) {
		this.numLegs = numLegs;
	}
}
public class Cat extends Animal{

	private String color;

	public Cat( String food, int legs) {
		super(food, legs);
		this.color="White";
	}

	public Cat(String food, int legs, String color){
		super(food, legs);
		this.color=color;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

}

Method Overloading

Feature that allows classes to have more than one method with the same name, but different parameters

public class Addition {
    int add(int first, int second){
        int sum = first + second;
        return sum;
    }

    int add(int first, int second, int third){
        int sum = first + second + third;
        return sum;
    }
}
//no errors when running

Standard Methods

The standard methods of classes are the methods that you can use on objects in that class