AP CSA Units 1-9 Notes

Unit 1: Primitive Types

Basic Data Types:

  • int (integer), double (decimal numbers), boolean (true/false), char (single character)

Declaring Variables:

  • Example: int age = 21;

Mathematical Operations:

  • Add (+), Subtract (-), Multiply (*), Divide (/), Modulus (% - remainder)
  • Operator precedence: follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction)

Type Conversion:

  • Widening (e.g., int to double), Narrowing (e.g., double to int, truncates decimals)

Taking Input:

  • Scanner input = new Scanner(System.in);

Displaying Output:

  • System.out.println("Output message");

Unit 2: Using Objects

Object Creation:

  • Using new, e.g., Object obj = new Object();

String Manipulation:

  • Example: String greeting = "Hello";
  • Useful methods:
    • length(): get string length
    • substring(): extract part of the string
    • indexOf(): locate a character or substring
    • charAt(): access character at a specific index

Null Values:

  • Null reference (null) indicates no object is assigned.

Comparing Values:

  • == compares references (memory locations), while .equals() checks actual content equality.

Math Utilities:

  • Examples:
    • Math.pow(a, b) calculates (a) raised to the power (b)
    • Math.sqrt(x) finds the square root of (x)
    • Math.random() generates a random number between 0 and 1.

Unit 3: Boolean Expressions and if Statements

Logical Operators:

  • && (and), || (or), ! (not)

Relational Operators:

  • <, >, <=, >=, ==, != (not equal)

Conditional Statements:

  • if, else if, else blocks for decision-making.

Combining Conditions:

  • Nesting conditions (placing one inside another) for more complex logic.

Advanced Logic:

  • Using multiple boolean expressions within a single condition.

Unit 4: Itterations

Types of Loops:

  • while(condition): executes as long as the condition remains true
  • for(initialization; condition; update): common loop structure for known iterations
  • do { } while(condition);: ensures loop runs at least once.

Breaking Out of Loops:

  • break: stops the loop
  • continue: skips the current iteration.

Nested Loops:

  • A loop inside another loop, useful for matrix or grid traversal.

Loop Use Cases:

  • Traversing arrays, summing values, searching for elements.

Unit 5: Writing Classes

Structure of a Class:

  • Fields (attributes), methods (behaviors), and constructors.

Constructors:

  • Initializes an object’s state, e.g.,

    public ClassName(type param) { this.field = param; }

    Getters and Setters

    Accessor methods (getField()) retrieve values, while mutator methods (setField()) modify them.

Static Members

Variables or methods shared by all instances of a class.

Encapsulation

Restricting access using private fields and exposing controlled access via public methods.


Unit 6: Arrays

Array Declaration

Syntax: type[] arr = new type[length];

Accessing Array Elements

By index, e.g., arr[0].

Array Traversal

Loops (for or while) used to iterate through array elements.

Common Array Tasks

  • Calculate sum
  • Find the minimum or maximum value
  • Compute averages

Array Boundaries

Avoid out-of-bounds errors with careful index handling (ArrayIndexOutOfBoundsException).


Unit 7: ArrayList

Defining an ArrayList

Example: ArrayList<String> list = new ArrayList<>();

Useful Methods

add(), remove(), set(), get(), size() for adding/removing, accessing, and getting the size of elements.

Dynamic Size

ArrayLists resize automatically when adding/removing elements.

Looping Through ArrayLists

Use either a for-each loop or a standard for loop.

Autoboxing/Unboxing

Automatic conversion between primitives and their wrapper classes (e.g., intInteger).


Unit 8: Two-Dimensional Arrays

Declaring 2D Arrays

Example: int[][] matrix = new int[rows][columns];

Accessing Elements

By row and column indices, e.g., matrix[0][1].

Traversing 2D Arrays

Typically with nested loops (one for rows, one for columns).

Common Uses

  • Grid or matrix representation
  • Image processing
  • Game boards

Algorithms for 2D Arrays

  • Row-wise and column-wise processing for tasks like summing rows or finding the maximum element.

Unit 9: Inheritance

Concept of Inheritance

A subclass inherits properties and behaviors from a superclass, e.g.,

class Subclass extends Superclass { }

Overriding Methods

A subclass can modify or extend the functionality of methods from the superclass.

super Keyword

Refers to the superclass’s constructor or methods.

Polymorphism

The ability to refer to objects of different subclasses through a single superclass reference.

Object Class

All classes inherit from Object, the root class in Java.