Units 6 - 10
// Unit 7
import java.util.*;
public class hack1{
public static void main (String[] args){
ArrayList<String> cool = new ArrayList();
System.out.println(cool.size());
}
}
hack1.main(null);
import java.util.*;
ArrayList<String> color = new ArrayList();
color.add("red apple");
color.add("green box");
color.add("blue water");
color.add("red panda");
for(int i = 0; i <= color.size(); i++){
if(color.get(i).contains("red")){
color.remove(i);
}
}
System.out.println(color)
public class Test {
public static void main(String[] args) {
String[][] arr = { // two indexes, to help store data in a 2D model
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
String longest = arr[0][0]; //defining the longest array at the moment
for(int row = 0; row < arr.length; row++) { //incrementing through each part to find largest string
for(int column = 0; column < arr[row].length; column++) {
if (arr[row][column].length() > longest.length()) {
longest = arr[row][column];
}
}
}
System.out.println(longest);
// Use nested for loops to find the longest or shortest string!
System.out.println("Use nested for loops to find the longest or shortest string!");
}
}
Test.main(null);
public class Animal {
protected double mass;
protected double lifespan;
public Animal(double mass, double lifespan) {
this.mass = mass;
this.lifespan = lifespan;
}
public void sound() {
System.out.println("sound");
}
public void die(boolean a) {
System.out.println("the cat is dead: " + a); //method to be overloaded
}
public class Dog extends Animal {
protected String color;
public Dog(double mass, double lifespan, String color) {
super(mass, lifespan);
this.color = color;
}
@Override
public void sound() {
System.out.println("woof");
}
}
public class Cat extends Animal { //make another subclass for Cat that extends off of Animal
protected String breed;
public Cat(double mass, double lifespan, String breed) {
super(mass, lifespan);
this.breed = breed;
}
@Override
public void sound() { //overrides the sound
System.out.println("meow");
}
public void die(boolean a, boolean b) { //method overloading
System.out.println("the cat is dead: " + a + b);
}
}
}
Unit 9 Vocab
- Inheritance/extends takes aspects of an existing class and replicates it within another class using the extends method, simplifying code by a lot.
- Subclass/Super - A subclass constructor forms after extending a previous class, and using the super(); method will take a method and replicate it in the new class.
- Overloading - When you use the same method name in a class each with different parameters.
- Override - When a subclass has the same name and parameters as the parent class
- Polymorphism - any of overloading, overriding, late bindingMultiple methods with the same name but different parameters
No code for lesson 10
- Recursion - method calling itself again, similar to a loop
- Base case - the condition passed that is needed to be reached to end the loop
- Binary search - Split the array in half and then continue doing so until the value is found
- Selection - finds minimum element from unsorted part and puts it at end of sorted part
- Merge - splits array into 2, calls it self into two sorted halves, and then merges all the havles back into arrayList