Units 6-10 Review
Grades
Unit | Grade | Homework |
---|---|---|
6: Arrays | 0.9/1 | ArrayMethods |
7: ArrayList | 0.9/1 | ArrayList sort |
8: 2D Array | 0.9/1 | Class and methods |
9: Inheritance | 0.83/1 | Notebook |
10: Recursion | 1/1 | Review ticket |
Total | 4.53/5 |
int[] arrayTest = {1, 2, 3, 4, 5, 6}; // how to initialize
for (int i = 0; i < arrayTest.length; i++) { // iteration
System.out.println(arrayTest[i]);
}
ArrayList<String> books = new ArrayList<String>(); // creating arraylist
books.add("Great Expectations");
books.add("The Brothers Karamazov");
books.add("Frankenstein");
books.add("Fahrenheit 451");
books.add("The Great Gatsby");
books.add("Hamlet");
Collections.sort(books, Collections.reverseOrder());
System.out.println(books); // showing sorted books in descending order
import java.util.ArrayList;
public class tester {
public static void main (String[] args) {
ArrayList<String> words = new ArrayList<String>(Arrays.asList("piano", "tenor", "tempo"));
System.out.println(words);
words.add("woodwind");
System.out.println(words);
}
}
tester.main(null);
String[][] test = {{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="},
{"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\"},
{"a", "s", "d", "f", "g", "h", "j", "k", "l"},
{"z", "x", "c", "v", "b", "n", "m", ",", ".", "/"}};
for(int i = 0;i<test.length;i++){
for(int j = 0; j < test[i].length;j++){ //nested for loops
System.out.print(test[i][j]+" ");
}
System.out.println(" ");
}
public void printValues() { // arbitrary values
for (int a = 0; a<numbers.length; a++) {
for (int b = 0; b<numbers[a].length; b++) {
numbers[a][b] = a + b; // spacing
System.out.println(numbers[a][b] + " "); // spacing
}
System.out.println();
}
}
Unit 9: Inheritance
Summary:
- Subclasses inherit properties of parent classes
- Use extends to create subclasses from base class
- Overload is instance of two methods with same name having different functions/parameters
- Override is used when there is no parent/child method conflict, overwrites parent method
public class Fruits {
public int cost;
public int season;
public Fruits (int cost, int season) {
this.cost = cost; // using this. to access base variables
this.season = season;
}
public String toString () {
return "Price: $" + this.cost + ", Season: " + this.season;
}
}
public class Mango extends Fruits { // mango is subclass of fruit
public Fruits (int cost, int season) {
super (cost, season); // super keyword
}
}
public class School {
private String model = "Boeing 737";
private int age = 3;
public void engineBurr() {
System.out.println("brrrrrr");
}
}
private class Jettison extends Plane {
public Jettison(String model, int age, String color) {
super(model, age);
this.color = color;
@Override
public void engineBurr() {
System.out.println("BRRRRRRRRR");
}
}
public static void test(int n) {
if (n > 0) {
test(n-1);
}
}
Integer a = 10;
System.out.println(a.toString()); // printing a as a string
String hw = "hello world";
System.out.println(hw.hashCode());
public static int binaryRecursive(int search, int[] array, int start, int end){
int middle = (start + end)/2; // setting the starting boundaries for search by dividing array in 2
if (end < start) { // error handling
return -1;
}
if (search < array[middle]) {
return binaryRecursive(search, array, start, middle - 1); // lowering range of index values if search is less than the new middle
}
if (search > array[middle]) {
return binaryRecursive(search, array, middle + 1, end); // narrowing range of index values to upper range of array
}
if (search == array[middle]) { // success case
return middle;
}
return -1;
}