Unit 1

Casting (Division)

  • Division with double will truncate unless casted
System.out.println(1/2);
System.out.println((double)1/(double)2);
0
0.5

Casting (Truncating and Rounding)

  • casting to an integer from a double will truncate
System.out.println(1.5);
System.out.println((int)1.5);
1.5
1

Wrapper Classes, why wrap int, double

  • To use primitive data types as objects
  • Useful because Collection objects can only store objects
Integer a = new Integer(8);
System.out.println(a);
8
ArrayList<int> nums = new ArrayList<int>();
|   ArrayList<int> myNumbers = new ArrayList<int>();
unexpected type
  required: reference
  found:    int

|   ArrayList<int> myNumbers = new ArrayList<int>();
unexpected type
  required: reference
  found:    int
ArrayList<Integer> nums = new ArrayList<Integer>(); // with wrapper class
nums.add(8);
nums.add(new Integer(9));
System.out.println(nums);
[8, 9]

Unit 2

Concatenation

  • combine string using +
  • String + object will make object use the .toString() method
  • for primitives it is just converted
public class Classs {
    public String toString() {
        return "string1";
    }
}

String a = "string2";
String b = "string3";
int c = 0;
Classs classs = new Classs();

System.out.println(a+b);
System.out.println(a+c);
System.out.println(a+classs);
string2string3
string20
string2string1

Math Class

  • Used for non built in math functions like sqrt
  • Can be used to generate random numbers
for (int i = 0; i < 3; i++) {
    int num = (int)(Math.random()*10 + 1);
    System.out.println(num);
    System.out.println(Math.sqrt(num));
}
2
1.4142135623730951
3
1.7320508075688772
8
2.8284271247461903

Comparing Numbers

  • Use ==
System.out.println(1==1);
System.out.println(1==0);
true
false

Comparing Strings/Objects

  • Use .equals() String objects have
String str1 = new String("string");
String str2 = new String("string1");
System.out.println("string".equals("string"));
System.out.println("string".equals("sTring"));
System.out.println(str1.equals(str1));
System.out.println(str1.equals(str2));
true
false
true
false

Unit 3

Compound Boolean Expression

  • A lot of boolean operators and parentheses
  • && is and, || is or, ! is not, etc
boolean t = true;
boolean f = false;
System.out.println((t||f)||(f&&!t)&&(f||!(!f&&t)));
true

Truth Tables

  • To see what operation boolean operators do

truth tables

De Morgan's law

From Wikipedia:

  • The negation of a disjunction is the conjunction of the negations
  • The negation of a conjunction is the disjunction of the negations or
  • The complement of the union of two sets is the same as the intersection of their complements
  • The complement of the intersection of two sets is the same as the union of their complements

NOT(A or B) is the same as NOT(A) and NOT(B) NOT(A and B) is the same as NOT(A) or NOT(B)

De Morgan's Law

boolean a = true;
boolean b = false;
System.out.println("A is " + a);
System.out.println("B is " + b);
System.out.println("NOT(A or B): " + Boolean.toString(!(a||b)));
System.out.println("NOT(A) and NOT(B): " + Boolean.toString((!a)&&(!b)));
System.out.println("NOT(A and B): " + Boolean.toString(!(a&&b)));
System.out.println("NOT(A) or NOT(B): " + Boolean.toString((!a)||(!b)));
if ((!(a||b)) == ((!a)&&(!b))) {
    System.out.print("NOT(A or B) is the same as NOT(A) and NOT(B)\n");
}
if ((!(a&&b)) == ((!a)||(!b))) {
    System.out.print("NOT(A and B) is the same as NOT(A) or NOT(B)");
}
A is true
B is false
NOT(A or B): false
NOT(A) and NOT(B): false
NOT(A and B): true
NOT(A) or NOT(B): true
NOT(A or B) is the same as NOT(A) and NOT(B)
NOT(A and B) is the same as NOT(A) or NOT(B)

Unit 4

For loop/Enhanced for loop

  • For loops iterats through an index
  • Enhanced for loops iterate through an iterable
for(int i = 0; i<5; i++) {
    System.out.print(i);
}
System.out.println();
int[] arr = {1,2,3,4,5};
for (int i: arr) {
    System.out.print(i);
}
01234
12345

While loop versus Do While loop

  • While loop runs when condition is true after being checked before
  • Do while loop runs when condition is true after being checked after
int i = 0;
while (i<0) {
    System.out.print(i);
    i++;
}
System.out.println();
int i = 0;
do {
    System.out.print(i);
    i++;
} while (i<0)
0

nested Loops

  • loop inside loops
  • useful for certain situations like 2d array
int[][] arr = {
    {0,1},
    {2,3}
};
for (int i = 0; i<arr.length; i++) {
    for (int j = 0; j<arr[i].length; j++) {
        System.out.print(arr[i][j]);
    }
}
0123

Unit 5

Creating a Class, describe Naming Conventions

  • class keyword then class name with first letter capitalized

Constructor, describe why there is no return

  • Used when creating an instance of the class
  • Same name as the class
  • No return because it returns the reference id of the object

Accessor methods, relationship to getter

  • To get the value of an attribute
  • getVar()

Mutator methods, relationship to setter, describe void return type

  • To set the value of attributes
  • Relation to setter is that it sets the value of an attribute
  • void return type means it doesn't return anything

Static variables and methods, Class variables

  • Belongs to the class, not instance
  • Built once throughout all instances

Access modifiers: Public, Private, Protected

  • public allows it to be accessed freely
  • private allows it to only be accessed within the same class
  • protected allows it to only be accessed within the same package and outside of a child class

this Keyword

  • refers to the existing class

main method, tester methods

  • used for testing purposes

Inheritance, extends

  • Gets all methods from the superclass

Subclass constructor, super Keyword

  • The subclass constructor is used when instantiating the object
  • super refers to the superclass

Overloading a method, same name different parameters

  • two methods with the same name but different parameters allows a method to be called with different data types
  • makes code clean and readable and gives flexibility

Overriding a method, same signature of a method

  • subclass has same method as super class and the subclass method is called rather than the super class's

Abstract Class, Abstract Method

  • Abstract methods are defined in the subclasses and having abstract methods require superclass to be abstract as well. Objects cannot be created from an abstract class, they can only be extended. This is useful when you want to create a class that can be extended but not instantiated.

Standard methods: toString(), equals(), hashCode()

  • ToString is a standard method that is used to convert an object into a string. Equals is useful for comparing two objects to see if they are equal. HashCode is useful for getting a unique hash code for an object.

Late binding of object, referencing superclass object, ie Animal a = new Chicken(); Animal b = new Goat();

  • allows the compiler to determine which method to use at runtime instead of compile time

Polymorphism: any of overloading, overriding, late binding

  • See above definitions

Big O notation for Hash map, Binary Search, Single loop, Nested Loop

  • Big O notation is used to describe time complexity
  • A hashmap operation takes O(1) to run
  • Binary search takes O(log n)
  • Single loop takes O(n)
  • Nested loop takes O(n^k) where k is how many nested loops there are
/*
 * Creator: Nighthawk Coding Society
 * Mini Lab Name: Fibonacci sequence, featuring a Stream Algorithm
 * 
 */

import java.util.ArrayList;  
import java.util.HashMap;
import java.util.stream.Stream;

/* Objective will require changing to abstract class with one or more abstract methods below */
abstract class Fibo {
    String name;  // name or title of method
    int size;  // nth sequence
    int hashID;  // counter for hashIDs in hash map
    ArrayList<Long> list;   // captures current Fibonacci sequence
    HashMap<Integer, Object> hash;  // captures each sequence leading to final result

    /*
     Zero parameter constructor uses Telescoping technique to allow setting of the required value nth
     @param: none
     */
    public Fibo() {
        this(20); // telescope to avoid code duplication, using default as 20
    }

    /*
     Construct the nth fibonacci number
     @param: nth number, the value is constrained to 92 because of overflow in a long
     */
    public Fibo(int nth) {
        this.size = nth;
        this.list = new ArrayList<>();
        this.hashID = 0;
        this.hash = new HashMap<>();
        //initialize fibonacci and time mvc
        this.init();
    }

    /*
     This Method should be "abstract"
     Leave method as protected, as it is only authorized to extender of the class
     Make new class that extends and defines init()
     Inside references within this class would change from this to super
     Repeat process using for, while, recursion
     */
    protected abstract void init();

    /*
     Number is added to fibonacci sequence, current state of "list" is added to hash for hashID "num"
     */
    public void setData(long num) {
        list.add(num);
        hash.put(this.hashID++, list.clone());
    }

    /*
     Custom Getter to return last element in fibonacci sequence
     */
    public long getNth() {
        return list.get(this.size - 1);
    }

    /*
     Custom Getter to return last fibonacci sequence in HashMap
     */
    public Object getNthSeq(int i) {
        return hash.get(i);
    }

    /*
     Console/Terminal supported print method
     */
    public void print() {
        System.out.println("Init method = " + this.name);
        System.out.println("fibonacci Number " + this.size + " = " + this.getNth());
        System.out.println("fibonacci List = " + this.list);
        System.out.println("fibonacci Hashmap = " + this.hash);
        for (int i=0 ; i<this.size; i++ ) {
            System.out.println("fibonacci Sequence " + (i+1) + " = " + this.getNthSeq(i));
        }
        System.out.println();
    }
}
public class FibF extends Fibo {
    public void init() {
        super.name = "For";
        int count = super.size;
        long[] previous = new long[] {0, 1};
        for (int i = 0; i < count; i++) {
            super.setData(previous[0]);
            previous = new long[] {previous[1], previous[0] + previous[1]};
        }
    }
    static public void main(String[] args) {
        long start = System.nanoTime();
        FibF fibF = new FibF();
        long end = System.nanoTime();
        fibF.print();
        System.out.println("Time: " + (end-start) + " ns");
    }
}
FibF.main(null);
package com.nighthawk.spring_portfolio.mvc.calendar;

// Prototype Implementation

public class APCalendar {

    /** Returns true if year is a leap year and false otherwise.
     * isLeapYear(2019) returns False
     * isLeapYear(2016) returns True
     */          
    public static boolean isLeapYear(int year) {
        // implementation not shown
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            return true;
        }
        return false;
        }
        
    /** Returns the value representing the day of the week 
     * 0 denotes Sunday, 
     * 1 denotes Monday, ..., 
     * 6 denotes Saturday. 
     * firstDayOfYear(2019) returns 2 for Tuesday.
    */
    public static int firstDayOfYear(int year) {
        // implementation not shown
        if (year > 1752) {
            return (year + numberOfLeapYears(1, year - 1))%7;
        }
        return (year + (year-1)/4 + 5)%7;
        }


    /** Returns n, where month, day, and year specify the nth day of the year.
     * This method accounts for whether year is a leap year. 
     * dayOfYear(1, 1, 2019) return 1
     * dayOfYear(3, 1, 2017) returns 60, since 2017 is not a leap year
     * dayOfYear(3, 1, 2016) returns 61, since 2016 is a leap year. 
    */ 
    public static int dayOfYear(int month, int day, int year) {
        // implementation not shown
        int count = 0;
        if (month < 8) {
            count = 31*((month)/2) + 30*((month-1)/2) + day;
        } else {
            count = 214 + 31*((month-7)/2) + 30*((month-8)/2) + day;
        }
        if (month > 2) {
            if (!isLeapYear(year)) {
                count = count - 2;
            } else {
                count--;
            }
        }
        return count;
        }

    /** Returns the number of leap years between year1 and year2, inclusive.
     * Precondition: 0 <= year1 <= year2
    */ 
    public static int numberOfLeapYears(int year1, int year2) {
         // to be implemented in part (a)
        int count = 0;
        for (int i = year1; i <= year2; i++) {
            if (isLeapYear(i)) {
                count++;
            }
        }
        return count;
        }

    /** Returns the value representing the day of the week for the given date
     * Precondition: The date represented by month, day, year is a valid date.
    */
    public static int dayOfWeek(int month, int day, int year) { 
        // to be implemented in part (b)
        return (firstDayOfYear(year) + dayOfYear(month, day, year) - 1) % 7;
        }

    /** Tester method */
    public static void main(String[] args) {
        // Private access modifiers
        System.out.println("numberOfLeapYears 2000-2022: " + APCalendar.numberOfLeapYears(2000, 2022));
        System.out.println("firstDayOfYear 1700 (1): " + APCalendar.firstDayOfYear(1700));
        System.out.println("firstDayOfYear 1701 (3): " + APCalendar.firstDayOfYear(1701));
        System.out.println("firstDayOfYear 1752 (3): " + APCalendar.firstDayOfYear(1752));
        System.out.println("firstDayOfYear 1753 (1): " + APCalendar.firstDayOfYear(1753));
        System.out.println("firstDayOfYear 1800 (3): " + APCalendar.firstDayOfYear(1800));
        System.out.println("firstDayOfYear 1801 (4): " + APCalendar.firstDayOfYear(1801));
        System.out.println("firstDayOfYear 2019 (2): " + APCalendar.firstDayOfYear(2019));
        System.out.println("dayOfYear 3/1/2017: " + APCalendar.dayOfYear(3, 1, 2017));
        System.out.println("dayOfYear 3/1/2016: " + APCalendar.dayOfYear(3, 1, 2016));

        // Public access modifiers
        System.out.println("isLeapYear 1600: " + APCalendar.isLeapYear(1600));
        System.out.println("isLeapYear 1800: " + APCalendar.isLeapYear(1800));
        System.out.println("isLeapYear 2019: " + APCalendar.isLeapYear(2019));
        System.out.println("isLeapYear 2024: " + APCalendar.isLeapYear(2024));
        System.out.println("numberOfLeapYears 2000-2022: " + APCalendar.numberOfLeapYears(2000, 2022));
        System.out.println("dayOfWeek 7/5/2022: " + APCalendar.dayOfWeek(7, 5, 2022));
        System.out.println("dayOfWeek 8/5/2022: " + APCalendar.dayOfWeek(8, 5, 2022));
        System.out.println("dayOfWeek 11/18/2022: " + APCalendar.dayOfWeek(11, 18, 2022));
    }

}package com.nighthawk.spring_portfolio.mvc.calendar;

// Prototype Implementation

public class APCalendar {

    /** Returns true if year is a leap year and false otherwise.
     * isLeapYear(2019) returns False
     * isLeapYear(2016) returns True
     */          
    public static boolean isLeapYear(int year) {
        // implementation not shown
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            return true;
        }
        return false;
        }
        
    /** Returns the value representing the day of the week 
     * 0 denotes Sunday, 
     * 1 denotes Monday, ..., 
     * 6 denotes Saturday. 
     * firstDayOfYear(2019) returns 2 for Tuesday.
    */
    public static int firstDayOfYear(int year) {
        // implementation not shown
        if (year > 1752) {
            return (year + numberOfLeapYears(1, year - 1))%7;
        }
        return (year + (year-1)/4 + 5)%7;
        }


    /** Returns n, where month, day, and year specify the nth day of the year.
     * This method accounts for whether year is a leap year. 
     * dayOfYear(1, 1, 2019) return 1
     * dayOfYear(3, 1, 2017) returns 60, since 2017 is not a leap year
     * dayOfYear(3, 1, 2016) returns 61, since 2016 is a leap year. 
    */ 
    public static int dayOfYear(int month, int day, int year) {
        // implementation not shown
        int count = 0;
        if (month < 8) {
            count = 31*((month)/2) + 30*((month-1)/2) + day;
        } else {
            count = 214 + 31*((month-7)/2) + 30*((month-8)/2) + day;
        }
        if (month > 2) {
            if (!isLeapYear(year)) {
                count = count - 2;
            } else {
                count--;
            }
        }
        return count;
        }

    /** Returns the number of leap years between year1 and year2, inclusive.
     * Precondition: 0 <= year1 <= year2
    */ 
    public static int numberOfLeapYears(int year1, int year2) {
         // to be implemented in part (a)
        int count = 0;
        for (int i = year1; i <= year2; i++) {
            if (isLeapYear(i)) {
                count++;
            }
        }
        return count;
        }

    /** Returns the value representing the day of the week for the given date
     * Precondition: The date represented by month, day, year is a valid date.
    */
    public static int dayOfWeek(int month, int day, int year) { 
        // to be implemented in part (b)
        return (firstDayOfYear(year) + dayOfYear(month, day, year) - 1) % 7;
        }

    /** Tester method */
    public static void main(String[] args) {
        // Private access modifiers
        System.out.println("numberOfLeapYears 2000-2022: " + APCalendar.numberOfLeapYears(2000, 2022));
        System.out.println("firstDayOfYear 1700 (1): " + APCalendar.firstDayOfYear(1700));
        System.out.println("firstDayOfYear 1701 (3): " + APCalendar.firstDayOfYear(1701));
        System.out.println("firstDayOfYear 1752 (3): " + APCalendar.firstDayOfYear(1752));
        System.out.println("firstDayOfYear 1753 (1): " + APCalendar.firstDayOfYear(1753));
        System.out.println("firstDayOfYear 1800 (3): " + APCalendar.firstDayOfYear(1800));
        System.out.println("firstDayOfYear 1801 (4): " + APCalendar.firstDayOfYear(1801));
        System.out.println("firstDayOfYear 2019 (2): " + APCalendar.firstDayOfYear(2019));
        System.out.println("dayOfYear 3/1/2017: " + APCalendar.dayOfYear(3, 1, 2017));
        System.out.println("dayOfYear 3/1/2016: " + APCalendar.dayOfYear(3, 1, 2016));

        // Public access modifiers
        System.out.println("isLeapYear 1600: " + APCalendar.isLeapYear(1600));
        System.out.println("isLeapYear 1800: " + APCalendar.isLeapYear(1800));
        System.out.println("isLeapYear 2019: " + APCalendar.isLeapYear(2019));
        System.out.println("isLeapYear 2024: " + APCalendar.isLeapYear(2024));
        System.out.println("numberOfLeapYears 2000-2022: " + APCalendar.numberOfLeapYears(2000, 2022));
        System.out.println("dayOfWeek 7/5/2022: " + APCalendar.dayOfWeek(7, 5, 2022));
        System.out.println("dayOfWeek 8/5/2022: " + APCalendar.dayOfWeek(8, 5, 2022));
        System.out.println("dayOfWeek 11/18/2022: " + APCalendar.dayOfWeek(11, 18, 2022));
    }

}
package com.nighthawk.spring_portfolio.mvc.calendar;

/** Simple POJO 
 * Used to Interface with APCalendar
 * The toString method(s) prepares object for JSON serialization
 * Note... this is NOT an entity, just an abstraction
 */
class Year {
   private int year;
   private boolean isLeapYear;

   private int year4;
   private int firstDayOfYear;

   private int year1;
   private int year2;
   private int numberOfLeapYears;

   private int month;
   private int day;
   private int year3;
   private int dayOfWeek;

   private int month1;
   private int day1;
   private int year5;
   private int dayOfYear;

   // zero argument constructor
   public Year() {} 

   /* year getter/setters */
   public int getYear() {
      return year;
   }
   public void setYear(int year) {
      this.year = year;
      this.setIsLeapYear(year);
   }


   /* year getter/setters */
   public int getYear4() {
      return year4;
   }
   public void setFirst(int year) {
      this.year4 = year;
      this.setFirstDayOfYear(year);
   }

   
   /* year getter/setters */
   public int getYear1() {
      return year1;
   }
   public int getYear2() {
      return year2;
   }
   public void setNum(int year1, int year2) {
      this.year1 = year1;
      this.year2 = year2;
      this.setNumberOfLeapYears(year1, year2);
   }

   /* year getter/setters */
   public int getMonth() {
      return month;
   }
   public int getDay() {
      return day;
   }
   public int getYear3() {
      return year3;
   }
   public void setWeek(int month, int day, int year) {
      this.month = month;
      this.day = day;
      this.year3 = year;
      this.setDayOfWeek(month, day, year);
   }

   public int getMonth1() {
      return month1;
   }
   public int getDay1() {
      return day1;
   }
   public int getYear5() {
      return year5;
   }
   public void setDay(int month, int day, int year) {
      this.month1 = month;
      this.day1 = day;
      this.year5 = year;
      this.setDayOfYear(month, day, year);
   }   

   /* isLeapYear getter/setters */
   public boolean getIsLeapYear(int year) {
      return APCalendar.isLeapYear(year);
   }
   private void setIsLeapYear(int year) {  // this is private to avoid tampering
      this.isLeapYear = APCalendar.isLeapYear(year);
   }

   /* isLeapYearToString formatted to be mapped to JSON */
   public String isLeapYearToString(){
      return ( "{ \"year\": "  +this.year+  ", " + "\"isLeapYear\": "  +this.isLeapYear+ " }" );
   }	

   /* isLeapYear getter/setters */
   public int getFirstDayOfYear(int year) {
      return APCalendar.firstDayOfYear(year);
   }
   private void setFirstDayOfYear(int year) {  // this is private to avoid tampering
      this.firstDayOfYear = APCalendar.firstDayOfYear(year);
   }

   /* isLeapYearToString formatted to be mapped to JSON */
   public String firstDayOfYearToString(){
      return ( "{ \"year\": "  +this.year4+  ", " + "\"firstDayOfYear\": "  +this.firstDayOfYear+ " }" );
   }	

   /* isLeapYear getter/setters */
   public int getNumberOfLeapYears(int year1, int year2) {
      return APCalendar.numberOfLeapYears(year1, year2);
   }
   private void setNumberOfLeapYears(int year1, int year2) {  // this is private to avoid tampering
      this.numberOfLeapYears = APCalendar.numberOfLeapYears(year1, year2);
   }

   /* isLeapYearToString formatted to be mapped to JSON */
   public String numberOfLeapYearsToString(){
      return ( "{ \"year1\": "  +this.year1+  ", " + "\"year2\": "  +this.year2+  ", " + "\"numberOfLeapYears\": "  +this.numberOfLeapYears+ " }" );
      }	

   /* isLeapYear getter/setters */
   public int getDayOfWeek(int month, int day, int year) {
      return APCalendar.dayOfWeek(month, day, year);
   }
   private void setDayOfWeek(int month, int day, int year) {  // this is private to avoid tampering
      this.dayOfWeek = APCalendar.dayOfWeek(month, day, year);
   }

   /* isLeapYearToString formatted to be mapped to JSON */
   public String dayOfWeekToString(){
      return ( "{ \"month\": "  +this.month+  ", " + "\"day\": "  +this.day+  ", " + "\"year\": "  +this.year3+  ", " + "\"dayOfWeek\": "  +this.dayOfWeek+ " }" );
   }

   public int getDayOfYear(int month, int day, int year) {
      return APCalendar.dayOfYear(month, day, year);
   }
   private void setDayOfYear(int month, int day, int year) {  // this is private to avoid tampering
      this.dayOfYear = APCalendar.dayOfYear(month, day, year);
   }

   /* isLeapYearToString formatted to be mapped to JSON */
   public String dayOfYearToString(){
      return ( "{ \"month\": "  +this.month1+  ", " + "\"day\": "  +this.day1+  ", " + "\"year\": "  +this.year5+  ", " + "\"dayOfYear\": "  +this.dayOfYear+ " }" );
   }	

   /* standard toString placeholder until class is extended */
   public String toString() { 
      return isLeapYearToString(); 
   }

   public static void main(String[] args) {
      Year year = new Year();
      year.setYear(2020);
      System.out.println(year);
   }
}

Past code with classes:

  1. Fibonacci
  2. FRQ1
  3. FRQ2