Summary-Quick notes

Recursion

  • Definition: A method calls itself to solve smaller subproblems of the original problem.
  • Base Case: Stops the recursion; prevents infinite loops (e.g., when size = 1).
  • Recursive Call: Solves the smaller part of the problem (e.g., factorial(n-1)).
  • Call Stack: Each recursive call is added to the stack; unwinds once the base case is hit.
  • Key Example: Factorial → n! = n * factorial(n-1), with base case factorial(1) = 1.
  • Array Processing: Pass an index to process arrays recursively (e.g., sum elements).
  • Binary Search: Divide-and-conquer → Search left or right in a sorted array recursively.
  • Recursion vs Iteration: Recursion uses function calls; iteration uses loops.
  • Common Errors: Infinite recursion from missing or unreachable base cases.
  • Advantages: Simplifies problems like tree traversal or divide-and-conquer algorithms.

Methods and Control Structures

  • Basic Syntax + Structure: Focus on logic over syntax; readers will overlook small mistakes like missing semicolons or indentation. Know how to call/write methods, handle return values, and use parameters.
  • Arrays: Use for storing/accessing values. Syntax: int[] arr = new int[10];. Access elements with arr[index] and loop with for loops.
  • ArrayLists: Dynamic arrays with methods like add(), remove(), and size(). Syntax: ArrayList<Type> list = new ArrayList<Type>();.
  • Strings: Use .equals() for comparison (not ==). Use + for concatenation or StringBuilder for loops.
  • Control Flow: Master conditionals like if, else if, else.
  • Libraries: Assume all required classes and methods are pre-imported and simple to use (no complex APIs).

Classes

  • Class Header: Always declare the class as public unless the question specifies otherwise. Example: public class ClassNameHere.

  • Instance Variables: Must be private and initialized within the class. Example: private String message;. Avoid using static for instance variables unless explicitly stated.

  • Constructors:
    • The constructor’s header must match the scoping of the class header (usually public).
    • Initialize instance variables using this.variable = parameter;.
  • Methods:
    • Declare methods as public unless otherwise noted.
    • Use appropriate return types, such as void, int, or String.
  • Null Cases: Handle null or empty cases explicitly, as these often provide free points on FRQs. Example: Return null if a string is empty.

  • Formatting Tips: Always match the exact method and constructor names provided in the prompt to avoid losing points.

  • Number of Lines Example: Calculate using message.length() and divide by the width. Include a check for any remaining characters to add an extra line.

  • String Methods: Use .substring() and concatenation (+) for constructing or manipulating strings in methods like getLines().

  • Loops in Methods: Use for loops to iterate through strings or arrays based on problem requirements (e.g., constructing multi-line messages).

  • Inheritance: Use extends for inheritance and implements for interfaces if required. Ensure class headers include these keywords correctly.

  • Common Errors:
    • Forgetting private for instance variables.
    • Missing public in class or method headers.
    • Incorrect initialization of instance variables.

Array/ArrayList

  • Understand the method signature (purpose, return type, access modifier).
  • Pay attention to input type (array vs. ArrayList).
  • Use loops with accurate bounds.
  • Handle null or empty cases.
  • Verify return type and syntax for arrays/ArrayLists before submitting.

2d Array

  • Remember there is some penalties for using some of the shortcuts ### Common Tasks:
    • Sum all elements: Use a nested loop to accumulate values.
    • Count specific values: Add conditions inside the loop.
    • Find max/min: Compare current values during traversal.

    ### Null Cases:

    • Check for null arrays or rows to avoid runtime errors.

    ### Array Dimensions:

    • Use array.length for the number of rows and array[0].length for columns.

    ### Edge Cases:

    • Handle edge cases like empty arrays, single-row arrays, or jagged arrays.