Unit 6-10 Lesson Blogs
Lesson Notes + Tangibles
- Table of Scores
- Unit 6: Arrays
- Unit 7 Array List
- Unit 8 2D Arrays
- Unit 9 Inheritance
- Unit 10 Recursion
Unit 6: Arrays
Takeaways:
- Arrays: one type of data storage
- Reference types
- Need import java.util Arrays
- Making Arrays: using constructors or using pre-initialized arrays
- Access elements through arrayName[index]
-
For loop
: examines and iterates over every element the array contains in a fast, effective, and more controllable way. -
While loop
: initialize an array of integers, and traverse the array from start to end using while loop. Start with an index of zero, condition that index is less than the length of array, and increment index inside while loop. -
Mutator methods
: reset the value of a private variable so that other classes can modify a value stored in the variable without direct access
/* for loop */
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {2, 4, 6, 8, 10};
int index = 0;
while (index < numbers.length) {
System.out.println(numbers[index]);
index++;
}
}
}
ArrayExample.main(null);
Unit 7 Array List
Takeaways:
- Different from arrays because array lists are dynamic, part of a framework, a class with many methods, more flexible, store object references, slower than arrays, only used with import statement
-
Primitive Data Types
: boolean, char, double, int -
Wrapper class data types
: boolean, character, double, int - Methods to know:
- size(): # of elements in the list
- add(obj): adds an element at the end
- add(index, object): adds element at specific index
- remove(index): removes element from specific index
- set(index, object): replaces element at index with new object
- get(index): returns element at index
import java.util.ArrayList;
public class hack1 {
public static void main (String[] args) {
ArrayList<String> hetvifavs = new ArrayList<String>(Arrays.asList("ripped jeans", "rap music", "black nail polish"));
System.out.println( "items " + hetvifavs + " Hetvi's favorite things. There are " + hetvifavs.size() + " things in the list" );
//objects you add must be of the same data type
hetvifavs.add("bleached hair");
System.out.println( "items " + hetvifavs + " Hetvi's favorite things. There are " + hetvifavs.size() + " things in the list" );
}
}
hack1.main(null);
Unit 8 2D Arrays
Takeways:
-
Array
: a data structure used to implement a collection (list) of primitive or object reference data -
Element
= a single value in the array -
Index
= the position of the element in the array (starts from 0) -
Array Length
= the number of elements in the array - Is public, so can be accessed in any class
- Is also final, so can’t change it after array has been created
-
Access and update elements
: nameOf2DArray[r][c] -
Nested loops
used to traverse 2D arrays
/* accessing and changing elements */
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
arr[2][0] = "Athens";
System.out.println(arr[2][0]);
}
}
Test.main(null);
/* nested loop */
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g", "l" },
{ "b", "e", "h", "k" },
{ "c", "d", "i", "j" }
};
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 4; col++) {
System.out.print(arr[row][col] + " ");
}
System.out.println(" ");
}
}
}
Test.main(null);
Unit 9 Inheritance
Takeaways:
-
Inheritance
: define a base class like a car class that has methods and attributes common. For example, there can be a car steering method, gas and break method, and attributes like speed -
Extend
: Each car that is created will extend from this base class thus inheriting the methods and attributes defined in the base class- New car classes with have special features
-
Super Class
: Base class that has all the generic methods (base Car Class that has generic methods for all cars)-
protected
: access modifier isn't affected by outside
-
-
Subclass constructor
: inherits all methods and attributes from super class- In car example, TeslaModelS was a subclass
- In personal example, IphoneX and IphoneV are subclasses
-
super Keyword
: superclass objects - used to call superclass methods and access the constructor- use constructors defined in the superclass
- can add specific attributes
- super() is the use of the keywords
-
Overloading
a method (same name different parameters): two methods with the same name but different arguments; static polymorphism or compile time polymorphism -
Overriding
a method (same signature of a method): subclass has specific implementation of a method that the superclass provides -
Polymorphism
: doing one task in many ways through inheritance- gearShift in cars can be used in two different ways in two different classes - two implementations through overriding
Unit 10 Recursion
Takeaways:
-
Recursive method
calls itself -
Base case
: after multiple calls base case is reached and its where recursion is stopped and value is returned --> should be written first to avoid infinite recursion -
Recursive calls
are apart of the method - each have different parameters - All recursions can be written as loops
-
Binary search
: data must be sorted in order, more efficient than linear search -
Linear recursion
: a function that only makes a single call to itself each time the function runs -
Selection sort
: algorithm works by repeatedly finding the minimum element