Unit 6

//Unit 6 code example (for and enhanced for loop)
int [] values = {2,5,1};
int total = 0;

for(int i = 0; i < values.length; i++) { //adding values of an array through a for loop
    total += values[i];
}

for(int element : values) { //adding values of an array through an enhanced for loop
    total += element;
}

Unit 7

  • ArrayLists are like arrays, but can have vsize changed arrayList
  • many methods which can be used with arrayLists
    • size();
      • Returns the number of elements in the list
    • add(obj);
      • Adds 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
  • can traverse arrayList with for-loop/enhanced for-loop
  • Homework: https://kinerboi.github.io/myFirstRepopart2/java/labs/2022/11/20/Unit7HW.html
// Unit 7 code example
import java.util.ArrayList; //never forget to import

public class methodsArrayList {
    public static void main (String[] args) {
        ArrayList<String> dogs = new ArrayList<String>(Arrays.asList("Sparky", "Duke", "Noodle")); //remeber the <(Variable Type)>
        ArrayList<String> dogs2 = new ArrayList<String>(Arrays.asList("Sparky", "Duke", "Noodle"));
        System.out.println("There are " + dogs.size() + " in the ArrayList"); 
        System.out.println("There are " + dogs2.size() + " in the ArrayList");
        
        //objects you add must be of the same data type
        dogs.add("Peanut"); //using arrayList functions
        System.out.println("There are now " + dogs.size() + " dogs in the ArrayList"); 

        String myDog = dogs.get(2);
        System.out.println("My dog is named " + myDog);
    }
}

//Note: you don't need to declare <String> again after new ArrayList

methodsArrayList.main(null);
There are 3 in the ArrayList
There are 3 in the ArrayList
There are now 4 dogs in the ArrayList
My dog is named Noodle

Unit 8

// Unit 8 code example
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);

Unit 9

  • Inheritance is the order in which different attributes, methods, or strings are called
    • depends on whentheer class is extended or not, if class contains methods, parameters, strings, etc
  • constructor: Where the attributes of a class are defined
  • Overriding: allows a subclass or child class to provide a specific implementation of a method that has already been provided by a super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature, and same return type (or sub-type) as a method in its super-class, then the method in the subclass will override the method in the super-class.
  • Polymorphism: running mutliple things at once with the same name, can be done with different amounts of parameters in method
  • toString method defines and JSONify's data protected is an access modifier so that the attribute isn't affected by outside modifiers.
  • to use constructor of superclass in subclass, need to use super keyword, allowing use of constructors that were made in the superclass
  • Homework: https://samuelwaang.github.io/samuel/cb/2022/12/12/inheritance-lesson.html
  • equals method, checks whether two strings are same, if true returns true or else returns false
// Super class
public class Car {
    protected String brandName;
    protected double range;
    protected double doorNumber;
    protected double maxSpeed;
    
    // Constructor for the attributes present in the superclass
    public Car(String brandName, double range, double doorNumber, double maxSpeed) {
        this.brandName = brandName;
        this.range = range;
        this.doorNumber = doorNumber;
        this.maxSpeed = maxSpeed;
    }
    
    public void gas () {
        System.out.println("Go!");
    }
    
    public void brake () {
        System.out.println("Stop!");
    }
    
    public void gearShift () {
        System.out.println("Use the stick");
    }
    
    public void steer () {
        System.out.println("turning left...");
    }
    
    public void horn () {
        System.out.print("honking... ");
    }
}
// extending super class, overriding example
public class TeslaModelS extends Car {
    // Additional attribute not present in the superclass
    protected String hornSound; 
    
    // Constructor for Subclass
    public TeslaModelS(String brandName, double range, double doorNumber, double maxSpeed, String hornSound) {
        // We use the Superclass constructor for the shared attributes through the keyword "super"
        super(brandName, range, doorNumber, maxSpeed);
        // hornSound is not in the Superclass, so we add it separately in the constructor
        this.hornSound = hornSound;
    }
    
    // We use override to change the functionality in the subclass of an existing method in the superclass
    @Override
    public void gearShift () {
        System.out.println("Use the gear selector next to the wheel");
    }
    public void steer () {
        System.out.println("turning right...");
    }
    
    // Here, we don't fully change the functionality of the existing horn method in the superclass
    // Instead, we take all of the functionality of the superclass method, and then add on to it
    public void horn () {
        super.horn();
        System.out.println(hornSound);
    }
    
    public static void main(String[] args) {
        // 5 argument constructor
        TeslaModelS modelS = new TeslaModelS("Tesla", 396, 4, 200, "eugh");
        // Example of late binding
        Car car = new TeslaModelS("Tesla", 396, 4, 200, "brrr");
        // We can still use the methods from the child class, even though we didn't mention them in the subclass!
        modelS.gas();
        // Using the overridden method
        modelS.gearShift();
        modelS.steer();
        // Using the method we added on to
        modelS.horn();
        car.horn();
    }
    
    
}
TeslaModelS.main(null);
// polymorphism example
public class ToyotaCamry extends Car {
    
    public ToyotaCamry (String brandName, double range, double doorNumber, double maxSpeed) {
        super(brandName, range, doorNumber, maxSpeed);
    }
    
    @Override
    public void gearShift () {
        System.out.println("Manual shift!");
    }
    
    public void turbo (int a) {
        System.out.println("Engaging turbo " + a);
    }
    
    public void turbo (int a, int b) {
        System.out.println("Engaging turbo " + a + " and nitro " + b);
    }
    
        
    public static void main(String[] args) {
        // 4 superclass argument constructor
        ToyotaCamry camry = new ToyotaCamry("Toyota", 348, 4, 145);
        // Using the overridden method
        camry.gearShift();
        // Using the overloaded method
        camry.turbo(1);
        camry.turbo(1, 1);

    }
}
ToyotaCamry.main(null);
// Demonstration of equals method
// Outputs boolean value of true or false
// If one object equals another
public class Student
{
   private String name;

   public Student(String name)
   {
      this.name = name;
   }

   public static void main(String[] args)
   {
      Student student1 = new Student("Bob");
      Student student2 = new Student("Jeff");
      Student student3 = student1;
      Student student4 = new Student("A");
      Student student5 = student4;
      System.out.println(student1.equals(student2));
      System.out.println(student2.equals(student3));
      System.out.println(student1.equals(student3));
      System.out.println(student3.equals(student4));
      System.out.println(student3.equals(student4));
      System.out.println(student5.equals(student4));


   }
}
Student.main(null);

Unit 10

  • Recursion is when a method calls itself repeatedly to solve a problem
  • Contain two parts
    • base case: condition to be reached or returned when conditions are met
    • recursive call: the method being run over and over again
    • uses if and else statement mainly
  • Binary seach algorithm:
    • Data has to be in sorted order
    • Splits array in half multiple times until value is found
  • Selection Sort:
    • finds minimum element from unsorted part and puts it at end of sorted part
  • Merge Sort:
    • splits array into 2, calls it self into two sorted halves, and then merges all the havles back into arrayList
  • Homework: https://github.com/KinerBoi/myFirstRepopart2/blob/master/_notebooks/2022-12-14-Unit10HW.ipynb images
// Unit 10 recursion example
public int fact(int n) // a random number inputed, factorial of which wil be calculated
{
    if (n == 1) // base case 
        return 1; //once n is found to be the same value as 1, the code stops
    else    
        return n*fact(n-1); //if values not equal, then the function keeps calling the else part   
}