Unit Score
6 0.9/1
7 0.95/1
8 0.9 /1
9 1/1
10 1/1
Total Score 4.75/5

Unit 6: Arrays

Unit 6 Blog

Unit 7: Array List

Unit 7 Blog

Unit 8: 2D Arrays

Unit 8 Blog

Unit 9: Inheritance

Unit 9 Blog

Unit 10: Recursion

Unit 10 Blog

Revised Vocab with Examples:

Casting for Division

When you divide a double by a double the output is not always a double and when you divide an integer by an integer the output is not always an integer. Casting can be used in the dominator to fix this.

int x = 3/2;
System.out.println(x);

// vs casting with double
double y = 3/(double)2;
System.out.println(y);
1
1.5

Casting for Truncating or Rounding

Double values can be rounded to the nearest integer by casting (int)

double x = 4.0 / 3;
int rounded = (int)(x + 0.5);
System.out.println("4.0/3 = " + x);
System.out.println("4/3 truncated: " + (int)x );
System.out.println("4.0/3 rounded to nearest int: " + rounded);
4.0/3 = 1.3333333333333333
4/3 truncated: 1
4.0/3 rounded to nearest int: 1

Wrapper Classes, why wrap int, double. Show examples

A wrapper is a class whose object contains primitive data types. Wrapper classes allow us to wrap a primitive object into the class object.

Integer a = 1;
Double b = 1.5;

System.out.println(a);
System.out.println(b)
1
1.5

Concatenation, explain or illustrate rules on mixed type Concatenation

Concatenation is defined as the joining of data and some examples include string concatenation as well as string and object concatenation with toString() method.

Math class, specifically Random usage

The math class contains static methods from the java.lang package

  • Ex: math.random
System.out.println(Math.abs(-2));
System.out.println(Math.pow(19, 6));
System.out.println(Math.sqrt(500));
2
4.7045881E7
22.360679774997898

Compound Boolean Expression

A boolean expression is a logical statement that can either be evaluated to true or false. A compound boolean expression involves two boolean expression, with the second boolean expression only being evaluated if the first boolean expression evaluates to true.

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.

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.

Comparing Numbers

The 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), returns a value less than zero if (x < y) and returns a value greater than zero if (x > y).

Comparing Strings

Ff (string1 > string2) it returns a positive value, if (string1 == string2) it returns 0, and if (string1 < string2) it returns a negative value

Comparing Objects

== and != operators

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));
-1

For loop, enhanced for loop

A for loop will keep running for each time a given condition is true. The enhanced for loop is also known as the for each loop and is used to iterate through elements of arrays and collections.

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);

While loop versus do while loop

While a specific condition is true, the loop will run. A while loop will check the condition first before running whereas a do while loop will run and then check the condition afterwards.

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);

Nested loops

Nested loops are loops within loops.

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);
Week: 1
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7
Week: 2
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7
Week: 3
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7

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 parameters

Mutator Methods

  • Set of methods or setters
    • allows you to change the value of the variable
  • Void Methods
    • no value returned
  • will take parameters

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);
is the light on? true
is the light on? false

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

Subclass constructor, super Keyword

  • 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 constructor

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