Scores

Unit Number My Score
Unit 6 1/1
Unit 7 0.9/1
Unit 8 1/1
Unit 9 1/1
Unit 10 0.95/1
Total 4.85/5

Links To HWs and Notebooks for each lesson

General Notes

  • Building on from previous units... 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 have data members, methods, constructors, nested classes, and interfaces.


If/else statements allow you to create a code segment that has checks. If/ElseIf/Else statements 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 instance 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 arguments. 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.

Sorting Algorithms

Bubble Sort

void sort(int[] array) {

    boolean isInOrder = false;
    while (isInOrder == false) {
        isInOrder = true;
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i + 1] < array[i]) {
                int temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
                isInOrder = false;
            }
        }
    }
}

Bogo Sort

void sort(int[] array) {
    Random rand = new Random();
    boolean isInOrder = false;
    while (isInOrder == false) {
        isInOrder = true;
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i + 1] < array[i]) {
                int rand1 = rand.nextInt(array.length);
                int rand2 = rand.nextInt(array.length);
                int temp = array[rand1];
                array[rand1] = array[rand2];
                array[rand2] = temp;
                isInOrder = false;
            }
        }
    }
}

Sorting Algorithms

public static int linearSearch(String[] words, String value) {
    //using a for loop to find the value
    // in the array. Return the location in the array
    // where the value was found.
    // If the value is not found in the array, return -1.

    for (int i = 0; i < words.length; i++) {
        if (words[i] == value) {
            return i;
        }
    }
    return -1;
}
public static int binarySearch(int[] array, int start, int end, int value) {
    // if end is greater than or equal to start, then
    // do code below
    if (end >= start) {
        // integer called mid and set it equal
        // to the half way point between start and end
        int mid = (start + end) / 2;
        // 4. if the array element at mid is equal to value
        // then return mid
        if (mid < array.length && array[mid] == value) {
            return mid;
        }
        // if the array element at mid is greater than value
        // then return the value returned from a call to the
        // binarySearch method. Pass in start and mid - 1
        // for the end variable.
        if (mid < array.length && array[mid] > value) {
            return binarySearch(array, start, mid - 1, value);
        }
        // return the value returned from a call to the binarySearch
        // method. Use mid + 1 as the start, and pass in end.
        return binarySearch(array, mid + 1, end, value);

    }
    // else return -1 because the value was not found
    return -1;
}

Recursion

Multiplication

public static int recursiveMultiplication(int number, int times) {
    //  if times is 1 return number
    if (times == 1) {
        return number;
    } else if (times == 0) {
        return 0;
    } else {

        // else return number + recursionMultiplication(number, times-1)
        return number + recursiveMultiplication(number, times - 1);
    }

}

Division

public static int recursiveDivision(int number, int numberToDivideBy) {
    if (numberToDivideBy> number) {
        return 0;
    } else {
        return 1 + recursiveDivision(number-numberToDivideBy, numberToDivideBy);
    }
}

Power

public static int recursivePower(int number, int power) {
    if (power == 0) {
        return 1;
    } else {
        return number * recursivePower(number, power - 1);
    }

}

Hashmap

// 1. Create a HashMap called roots with Integers for the keys and Doubles for
    // the values.
    HashMap<Integer, Double> roots = new HashMap<Integer, Double>();
    // 2. Using a for-loop, add 500 entries to your HashMap. The key entry will be
    // the
    // current iteration of the loop (i). The value entry will be the square root of
    // i.
    for (int i = 0; i < 37; i++) {
        roots.put(i, Math.sqrt(i));
    }
    // 3. Iterate through all the entries in your HashMap displaying the keys with
    // their respective
    // square roots (values). Use the following format.
    for (int i = 0; i < roots.size(); i++) {
System.out.println("The square root of "+i+"is"+roots.get(i));
    }
    // The square Root of 0 is 0.0
    // The square Root of 1 is 1.0
    // The square Root of 2 is 1.4142135623730951
    // The square Root of 3 is 1.7320508075688772
The square root of 0is0.0
The square root of 1is1.0
The square root of 2is1.4142135623730951
The square root of 3is1.7320508075688772
The square root of 4is2.0
The square root of 5is2.23606797749979
The square root of 6is2.449489742783178
The square root of 7is2.6457513110645907
The square root of 8is2.8284271247461903
The square root of 9is3.0
The square root of 10is3.1622776601683795
The square root of 11is3.3166247903554
The square root of 12is3.4641016151377544
The square root of 13is3.605551275463989
The square root of 14is3.7416573867739413
The square root of 15is3.872983346207417
The square root of 16is4.0
The square root of 17is4.123105625617661
The square root of 18is4.242640687119285
The square root of 19is4.358898943540674
The square root of 20is4.47213595499958
The square root of 21is4.58257569495584
The square root of 22is4.69041575982343
The square root of 23is4.795831523312719
The square root of 24is4.898979485566356
The square root of 25is5.0
The square root of 26is5.0990195135927845
The square root of 27is5.196152422706632
The square root of 28is5.291502622129181
The square root of 29is5.385164807134504
The square root of 30is5.477225575051661
The square root of 31is5.5677643628300215
The square root of 32is5.656854249492381
The square root of 33is5.744562646538029
The square root of 34is5.830951894845301
The square root of 35is5.916079783099616
The square root of 36is6.0

Inheritance

public class WorldCup {
    private char group;

    public WorldCup(char group) {
        this.group = group;
    }

    public char getGroup() {
        return group;
    }

    public class Brazil extends WorldCup {
        private String name;
        private String continent;
        private String jerseyColor;

        public Brazil(char group, String name, String continent, String jerseyColor) {
            super(group);
            this.name = name;
            this.continent = continent;
            this.jerseyColor = jerseyColor;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
                    + "}";
        }

    }

    public class Argentina extends WorldCup {
        private String name;
        private String continent;
        private String jerseyColor;

        public Argentina(char group, String name, String continent, String jerseyColor) {
            super(group);
            this.name = name;
            this.continent = continent;
            this.jerseyColor = jerseyColor;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
                    + "}";
        }

    }

    public class Croatia extends WorldCup {
        private String name;
        private String continent;
        private String jerseyColor;

        public Croatia(char group, String name, String continent, String jerseyColor) {
            super(group);
            this.name = name;
            this.continent = continent;
            this.jerseyColor = jerseyColor;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
                    + "}";
        }

    }

    public class Morocco extends WorldCup {
        private String name;
        private String continent;
        private String jerseyColor;

        public Morocco(char group, String name, String continent, String jerseyColor) {
            super(group);
            this.name = name;
            this.continent = continent;
            this.jerseyColor = jerseyColor;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
                    + "}";
        }

    }

    public class France extends WorldCup {
        private String name;
        private String continent;
        private String jerseyColor;

        public France(char group, String name, String continent, String jerseyColor) {
            super(group);
            this.name = name;
            this.continent = continent;
            this.jerseyColor = jerseyColor;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
                    + "}";
        }

    }

    public static void main(String[] args) {
        WorldCup Qatar = new WorldCup('A');
        Brazil brazilTeam = Qatar.new Brazil('F', "Brazil", "South America", "Yellow");
        Argentina argentinaTeam = Qatar.new Argentina('C', "Argentina", "South America", "Blue");
        Croatia croatiaTeam = Qatar.new Croatia('F', "Croatia", "Europe", "White");
        Morocco moroccoTeam = Qatar.new Morocco('F', "Morocco", "Africa", "Red");
        France franceTeam = Qatar.new France('D', "France", "Europe", "Blue");
        System.out.println(brazilTeam);
        System.out.println(argentinaTeam);
        System.out.println(croatiaTeam);
        System.out.println(moroccoTeam);
        System.out.println(franceTeam);

    }

}

WorldCup.main(null);
Brazil: {group: A continent: South America, jersey color: Yellow}
Argentina: {group: A continent: South America, jersey color: Blue}
Croatia: {group: A continent: Europe, jersey color: White}
Morocco: {group: A continent: Africa, jersey color: Red}
France: {group: A continent: Europe, jersey color: Blue}