AP Computer Science A Study Guide


1. Introduction to AP Computer Science A

AP Computer Science A (CSA) is a rigorous course that teaches students fundamental computer science concepts using Java programming. Topics include object-oriented programming, data structures, algorithms, and the fundamentals of software development.

  • Exam Format:
    • Multiple-choice questions: Test your understanding of Java programming concepts, data structures, and algorithm analysis.
    • Free-response questions: These require you to write Java code to solve problems, demonstrating your ability to implement algorithms and manipulate data structures.

2. Java Basics

Java Syntax:

  • Java is a case-sensitive, object-oriented programming language.
  • Statements in Java end with a semicolon (;).
  • Java programs are organized into classes, with each program having a main method, which is the entry point for execution.

Data Types:

  • Primitive Types: int, double, char, boolean
  • Wrapper Classes: Integer, Double, Character, Boolean (allow primitive types to be treated as objects)

Variables:

  • Declare variables with a type, e.g., int x = 5;

Constants:

  • Declared using final, e.g., final double PI = 3.14159;

Operators:

  • Arithmetic: +, -, *, /, %
  • Relational: ==, !=, >, <, >=, <=
  • Logical: &&, ||, !

3. Control Structures

Conditionals:

Switch Statement: A way to handle multiple conditions.

switch (day) {
    case 1: System.out.println("Monday"); break;
    case 2: System.out.println("Tuesday"); break;
    default: System.out.println("Invalid day");
}

Else If Statement: Provides an alternative if the first condition is false.

if (x > 5) {
    System.out.println("x is greater than 5");
} else {
    System.out.println("x is less than or equal to 5");
}

If Statement: Executes a block of code if a condition is true.

if (x > 5) {
    System.out.println("x is greater than 5");
}

Loops:

Do-While Loop: Executes the loop at least once, then repeats while the condition is true.

do {
    System.out.println(x);
    x++;
} while (x < 5);

While Loop: Repeats a block of code as long as a condition is true.

while (x < 5) {
    System.out.println(x);
    x++;
}

For Loop: Repeats a block of code a fixed number of times.

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

4. Object-Oriented Programming (OOP)

Classes and Objects:

  • A class is a blueprint for creating objects (instances of that class).
  • An object is an instance of a class.
public class Car {
    String color;
    String model;

    public void startEngine() {
        System.out.println("The engine is running.");
    }
}

Creating an Object:

Car myCar = new Car();
myCar.color = "red";
myCar.model = "Toyota";
myCar.startEngine();

Encapsulation:

  • Use access modifiers (private, public) to control access to class members.
  • Getters and Setters are methods used to access and modify private variables.

Inheritance:

  • A class can inherit fields and methods from another class, enabling code reuse.
class Animal {
    public void makeSound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    public void makeSound() {
        System.out.println("Bark");
    }
}

Polymorphism:

  • Polymorphism allows one method or object to take many forms.
    • Example: A superclass reference can point to a subclass object, and the appropriate method is called based on the object type.
Animal myDog = new Dog();
myDog.makeSound();  // Outputs: Bark

Abstraction:

  • Abstract classes or interfaces define methods that are implemented by subclasses.
  • Abstract class: Cannot be instantiated and may contain abstract methods (methods without a body).
abstract class Animal {
    abstract void makeSound();
}

5. Data Structures

Arrays:

  • An array stores multiple values in a single variable.
int[] numbers = {1, 2, 3, 4};
System.out.println(numbers[2]);  // Outputs: 3

ArrayLists:

  • ArrayList is a resizable array that allows adding, removing, and accessing elements dynamically.
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
System.out.println(list.get(0));  // Outputs: apple

Strings:

  • Strings are objects in Java and provide various methods for manipulation, such as .length(), .charAt(), .substring(), .indexOf(), etc.
String greeting = "Hello, world!";
int length = greeting.length();  // 13
char firstLetter = greeting.charAt(0);  // 'H'

6. Algorithms

Sorting Algorithms:

  • Bubble Sort: Compares adjacent elements and swaps them if necessary, repeatedly passing through the list.
  • Selection Sort: Finds the minimum (or maximum) element and swaps it with the first unsorted element.
  • Insertion Sort: Builds the sorted list one item at a time by inserting each element into its correct position.

Binary Search:

  • A more efficient searching algorithm compared to linear search, used to find an element in a sorted list. It works by dividing the list in half and repeatedly narrowing down the search range.
int binarySearch(int[] arr, int target) {
    int low = 0;
    int high = arr.length - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (arr[mid] == target) return mid;
        else if (arr[mid] < target) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}

7. Recursion

Recursion:
A recursive function calls itself to solve smaller instances of the same problem.

Example: Factorial function using recursion:

public int factorial(int n) {
    if (n == 0) return 1;
    return n * factorial(n - 1);
}