AP CSA EXAM PREP

Introduction

  • Host: Harley, a former computer science teacher with experience in teaching AP Computer Science A (AP CSA).
  • Objective: Provide a comprehensive review of AP CSA material in one go, using a code editor for live examples.

Unit 1: Introduction to Java

  • Hello World Example: First program usually taught.

  • System.out.println vs. System.out.print:

    • println adds a new line after printing, while print does not.
    • Example: System.out.print("Hello"); System.out.print("World"); results in HelloWorld on the same line, whereas System.out.println will result in:
    • Hello
    • World
  • Syntax Errors: Mistakes in the format/structure of code (e.g., typos).

  • Logic Errors: Code runs but does not produce the expected result.

  • Comments: Use // for single-line comments; /* ... */ for multi-line comments. Comments do not affect code execution.

  • String Literal: Text enclosed in quotes.

  • Main Method: public static void main(String[] args), the entry point of any Java application.

    • Code execution begins from the main method.
  • Variables:

    • Three parts: type, name, value.
    • Example: int x = 5;
    • Must match variable type with assigned value (e.g., no int x = "5";).
    • Common variable types include: int, double, boolean, String.

Unit 2: Classes and Objects

  • Classes and Objects: Classes define data types; objects are instances of classes.
  • Attributes (instance variables) store object characteristics; methods define behaviors.
  • Example of a class definition:
  public class Person {
      private String name;  
      private int age;  
      // Constructor  
      public Person(String name, int age) {
          this.name = name;
          this.age = age;
      }
      // Method  
      public void sayHi() {
          System.out.println("Hello, my name is " + name);
      }
  }
  • Constructors: Initialize new objects and set initial attribute values.
  • Accessing Attributes: Public variables can be accessed outside the class; private variables cannot.

Unit 3: If Statements

  • If Statements: Control structures to execute code based on conditions.
    • Syntax:
  if (condition) {
      // code to execute if condition is true
  } else {
      // code to execute if condition is false
  }
  • Boolean Expressions: == for equality, != for inequality.
  • Common pitfalls include confusing single (=) and double (==) equals signs when comparing values.

Unit 4: Loops

  • While Loop: Repeats code as long as a condition is true.
    • Syntax:
  while (condition) {
      // code
  }
  • For Loop: Commonly used to iterate over an array or range.
    • Syntax:
  for (int i = 0; i < limit; i++) {
      // code
  }
  • Nested loops are allowed, but avoid infinite loops by ensuring the loop condition eventually becomes false.

Unit 5: More on Classes

  • Similar structure to Unit 2. Focus on writing methods and using getters and setters for private variables.
  • Getters and Setters: Methods to retrieve and update private attributes.
    • Syntax example for getter:
  public String getName() {
      return name;
  }

Unit 6: Arrays

  • Arrays: Fixed-size data structure to store multiple values of the same type.
    • Example: int[] scores = {90, 80, 70};
    • Access elements using index: scores[0] gives 90.
    • Length: scores.length gives the number of items in the array.

Unit 7: ArrayLists

  • ArrayLists: Dynamic arrays that can grow in size.
    • Use ArrayList<Type> for declaration, add items using add(), access using get(index).
    • Syntax example:
  ArrayList<String> fruits = new ArrayList<>();
  fruits.add("Apple");

Unit 8: 2D Arrays

  • 2D Arrays: Arrays of arrays. Typically used for matrices.
    • Example: int[][] matrix = {{1, 2}, {3, 4}};
    • Accessing: matrix[0][1] gives 2.

Unit 9: Inheritance

  • Basic principle of inheritance in classes. Subclasses inherit attributes and methods from parent classes.
  • Keywords: extends to denote inheritance.
  • Example:
  public class Car extends Vehicle {
      // Car-specific properties and methods
  }

Unit 10: Recursion

  • Recursion: Method calls itself to solve problems.
  • Base case: Condition under which the recursion stops.
  • Example: Calculating factorial using recursion.
    • factorial(n) calls factorial(n-1) until base case is reached.