Unit 5: Arrays and ArrayLists

Arrays

  • Array: A fixed-size data structure that stores elements of the same data type.
  • Declaration: Create an array and specify its type and size.
    int[] numbers = new int[5];
    
  • Initialization: Set values for array elements.
    numbers[0] = 1;
    numbers[1] = 2;
    
  • Accessing Elements: Access elements by their index.
    int firstNumber = numbers[0];
    

ArrayLists

  • ArrayList: A dynamic-size data structure that stores objects of a specified data type.
  • Declaration and Initialization: Create an ArrayList and add elements.
    ArrayList<String> names = new ArrayList<>();
    names.add("Alice");
    names.add("Bob");
    
  • Accessing Elements: Get elements by index or use iteration.
    String firstPerson = names.get(0);
    for (String name : names) {
        // Code to process each name
    }
    

Unit 6: Object-Oriented Programming

Classes and Objects

  • Class: A blueprint for creating objects, defining attributes and methods.
    class Car {
        String make;
        int year;
        // Methods and constructors
    }
    
  • Object: An instance of a class created using the new keyword.
    Car myCar = new Car();
    myCar.make = "Toyota";
    myCar.year = 2022;
    

Constructors

  • Constructor: Special method to initialize objects when created.
    class Car {
        String make;
        int year;
    
        Car(String make, int year) {
            this.make = make;
            this.year = year;
        }
    }
    

Inheritance

  • Inheritance: A mechanism where a subclass can inherit attributes and methods from a superclass.
    class Animal {
        void eat() {
            // Code for eating
        }
    }
    
    class Dog extends Animal {
        void bark() {
            // Code for barking
        }
    

Polymorphism

  • Polymorphism: Allows objects of different classes to be treated as objects of a common superclass. It supports method overriding.
    class Animal {
        void makeSound() {
            // Default animal sound
        }
    }
    
    class Dog extends Animal {
        @Override
        void makeSound() {
            // Dog's sound
        }