AP Computer Science A: Java Review & Free-Response Questions

AP COMPUTER SCIENCE 2020 CHEAT SHEET – WILLIAM KONG

1) Strings

A string is a line of text, an object in Java, defined by the String class.

String concatenation – Use the + operator to add multiple strings or numbers.

A string is an array of characters.

Methods of the String Class

  • int length() – Returns the length of the string.
  • String substring(int from, int to) – Returns a substring from the specified starting index (inclusive) to the ending index (exclusive).
  • String substring(int from) – Returns a substring from the specified starting index to the end of the string.
  • int indexOf(String str) – Returns the index of the first occurrence of the specified string. Returns -1 if not found.
  • char charAt(int index) – Returns the character at the given index.
  • int compareTo – Compares two strings lexicographically.

Example

int[] vals = {5,4,2};
String s = "Hervaeus";
String s2 = "";
for(int i = 0; i < s.length(); i++) {
     for(int j = 0; j < vals[i % vals.length]; j++) {
          s2 += s.charAt(i);
     }
}
System.out.println(s2);

2) 1-D Arrays

Arrays are objects used to group and organize data of the same type. They are a sequence of values, called array elements.

In Java, the array itself is an object.

Example

for (int index = 0; index < array.length; index++) {
                        // do something here
                    }

3) ArrayLists

ArrayLists dynamically change size depending on what is needed. They allow you to specialize the ArrayList type, such as ArrayList<String> or ArrayList<Integer>.

The Wrapper Class

Each primitive data type in Java has a corresponding wrapper class (e.g., Integer for int, Double for double). Wrapper classes allow you to use primitive types in places where objects are required, such as in collections like Map.

Example

import java.util.List;
import java.util.ArrayList;

public class Test
{
    public static void main(String[] args)
    {
        List<String> names = new ArrayList<String>();
        String[] friends = {"Sam", "Jessica", "Mark", "Alexis"};
        for (int i = 0; i <= friends.length; i++)
        {
            names.add(friends[i]);
        }
        System.out.println(names);
    }
}

4) Classes

In computer science, a variable is an identifier for a memory location whose value can be modified.

Primitive Data Types

  • int: Integer values (e.g., -2, 147, 9)
  • double: Floating-point numbers with twice the memory of float
  • boolean: True or false values
  • char: Single characters

Java Math Class

The Math class provides methods for complex mathematical operations.

  • Math.abs(int num): Returns the absolute value of a number.
  • Math.pow(double num, double power): Returns a number raised to the specified power.
  • Math.sqrt(double num): Returns the square root of a positive number.
  • Math.random(): Returns a random number between 0.0 and 1.0.

Random Generator

Random generator = new Random();
generator.nextInt(n); // Returns a random integer between 0 (inclusive) and n (exclusive)
generator.nextDouble(); // Returns a random double between 0.0 (inclusive) and 1.0 (exclusive)

AP Free-Response Questions

Example Question

The LightBoard class models a two-dimensional display of lights, where each light is either on or off (represented by a Boolean value). Implement a constructor to initialize the display and a method to evaluate a light.

Write the constructor for the LightBoard class, which initializes lights so that each light is set to on with a 40% probability. The notation lights[r][c] represents the array element at row r and column c.