AP CSA · Unit 1 Part B · Lessons 1.9–1.15

Methods, Objects & Strings Review

Built around the two study methods with the strongest research behind them: testing yourself and spacing it out. Take the test first, then use the notes to fix what you missed.

Tonight's plan

  1. Take the test cold. ~30 min
    Don't read the notes first. Work each question on paper before tapping, especially the tracing ones. Getting answers wrong now is part of how this works: trying to recall before you look makes the right answer stick better.
  2. Fix only what you missed. ~20 min
    Each question links to its lesson notes. For every miss, say out loud why the right answer is right. Skip rereading the lessons you already got right.
  3. Retry your misses. ~10 min
    Hit Retry missed. The page remembers which ones you got wrong in this browser.
  4. Write one FRQ on paper. ~15 min
    Pick the one that covers what you missed most, write it by hand, then grade yourself with the rubric. Do the others tomorrow if you have time.
  5. Stop and sleep.
    Sleep is when the brain locks in what you studied. An extra hour of cramming costs more than it gives.
  6. Tomorrow morning: retry missed once more. ~10 min
    A short second round after a gap (spacing) beats one long session.

Why this order: a large review of study techniques (Dunlosky et al., 2013) rated practice testing and spaced practice as the two most effective methods, and rated rereading and highlighting as low value. The test also mixes lessons together instead of going in order, because the real test won't tell you which lesson a question comes from.

✓ Mixed practice test

32 questions, lessons mixed. Tap an answer to check it.

✎ Free response

Write the code yourself. Do it on paper if your test is on paper; typing with autocomplete hides mistakes. Then open the sample solution and check off each rubric point you earned. Any correct solution counts, even if it doesn't match the sample.

1.9 Method Signatures

TrapChanging only the return type is not overloading. int f(int x) and double f(int x) have the same signature, so that's a compile error.

1.10 Calling Class Methods

1.11 The Math Class

Math is in java.lang, so no import is needed. All its methods are static: Math.method().

MethodReturnsExample
Math.abs(int) / Math.abs(double)Absolute value, same type as inputMath.abs(-3) → 3
Math.pow(double, double)double: first to the power of secondMath.pow(2, 3) → 8.0
Math.sqrt(double)double: positive square rootMath.sqrt(16) → 4.0
Math.random()double ≥ 0.0 and < 1.0never exactly 1.0

Random int from min to max (inclusive):

(int)(Math.random() * range) + min      // range = max - min + 1
(int)(Math.random() * 6) + 5            // 5 to 10  (range = 10 - 5 + 1 = 6)
(int)(Math.random() * 20) + 10          // 10 to 29 (biggest is 19 + 10)
Trappow and sqrt always return a double, so they print with .0 and can't be stored in an int without a cast. Also: the parentheses in (int)(Math.random() * 6) matter. (int) Math.random() * 6 casts first, which is always 0.

1.12 Objects: Instances of Classes

Trap"An instance of the pet class is Dog" has it backwards. The object is an instance of the class. Read these answer choices slowly.

1.13 Creating & Initializing Objects

public Cat()                                 // no-arg
public Cat(String c, String b, boolean h)    // 3 args

new Cat();                          // ✓
new Cat("orange", "Tabby", false);  // ✓
new Cat("Shorthair", true);         // ✗ no 2-arg constructor
Cat("orange", "Tabby", false);      // ✗ missing new

1.14 Calling Instance Methods

Class (static) methodInstance method
Headerpublic static void go()public void go()
CallClassName.go()obj.go()
Needs an object?NoYes

1.15 Strings heavy

Method (on Quick Reference)Example with s = "computer"Result
length()s.length()8
substring(from, to)
to is not included
s.substring(2, 5)"mpu"
substring(from)s.substring(3)"puter"
substring(i, i + 1)s.substring(0, 1)"c"
indexOf(str)s.indexOf("t") / s.indexOf("z")5 / -1
equals(other)s.equals("Computer")false
compareTo(other)"apple".compareTo("banana")negative (a comes first)
// index:  c=0 o=1 m=2 p=3 u=4 t=5 e=6 r=7
1 + 2 + "3" + 4 + 5     // 3 → "33" → "334" → "3345"
"1" + 2 + 3             // "12" → "123"
"Java".substring(4)     // "" (empty; from == length is allowed)
"Java".substring(1, 5)  // IndexOutOfBoundsException (5 > length)
How to do substring questionsWrite the word out and number every letter, starting at 0, before you answer. substring(a, b) gives b - a characters.