AP Computer Science A · Skill map

Unit 1: Using Objects and Methods

15 topics 28 goals 15-25% of the multiple-choice section
Quick lookGoals onlyWith examples
1.1

Introduction to Algorithms, Programming, and Compilers

I can describe an everyday process as an algorithm, with clear steps in order.

  • int score = 10;
    score = score * 2;
    score = score + 5;
    System.out.println(score);
    Output
    25

I can explain what happens between writing code and running it.

  • System.out.println("Line 1");
    System.out.println("Line 2")
    The compiler says
    error: ';' expected

I can tell a syntax error, a logic error, and a run-time error apart.

  • Syntax error

    System.out.println("Hi);
    The compiler says
    error: unclosed string literal
  • Logic error

    int a = 90;
    int b = 80;
    double average = a + b / 2;
    System.out.println(average);
    Output
    130.0
  • Run-time error

    int slices = 8;
    int people = 0;
    System.out.println(slices / people);
    Output
    Crashes with ArithmeticException
1.2

Variables and Data Types

I can choose whether a value needs a primitive type or a reference type.

  • int count = 3;          // primitive: a whole number
    double gpa = 3.7;       // primitive: a decimal number
    boolean done = true;    // primitive: true or false
    String name = "Ana";    // reference: an object

I can declare variables that store numbers and true/false values.

  • The three primitive types

    int age = 16;
    double price = 4.99;
    boolean isRaining = false;

    Not on the AP exam: The other five primitive types: long, short, byte, float, and char.

  • Changing a variable

    int lives = 3;
    lives = 2;
    System.out.println(lives);
    Output
    2
1.3

Expressions and Output

I can write code that prints output, and predict exactly what it shows.

  • System.out.print("Hello");
    System.out.print("World");
    System.out.println("!");
    System.out.println("Done");
    Output
    HelloWorld!
    Done

I can use string literals, including escape sequences, and predict what they print.

  • Literals

    System.out.println(42);
    System.out.println(2.5);
    System.out.println(true);
    System.out.println("Hi there");
    Output
    42
    2.5
    true
    Hi there
  • Escape sequences

    System.out.println("She said \"hi\"");
    System.out.println("C:\\Users\\me");
    System.out.println("line 1\nline 2");
    Output
    She said "hi"
    C:\Users\me
    line 1
    line 2

I can work out the value of an arithmetic expression, and whether it is an int or a double.

  • int or double?

    System.out.println(7 + 2);
    System.out.println(7 + 2.0);
    System.out.println(3 * 1.5);
    Output
    9
    9.0
    4.5

    Not on the AP exam: Math that produces special double values, like infinity or NaN.

  • Integer division

    System.out.println(7 / 2);
    System.out.println(7 / 2.0);
    System.out.println(2 / 5);
    Output
    3
    3.5
    0
  • Remainder with %

    System.out.println(17 % 5);
    System.out.println(10 % 2);
    System.out.println(3 % 8);
    Output
    2
    0
    3

    Not on the AP exam: A negative number on the left of %, or zero or a negative number on the right.

  • Order of operations

    System.out.println(2 + 3 * 4);
    System.out.println((2 + 3) * 4);
    System.out.println(20 / 4 * 2);
    System.out.println(10 - 7 % 3);
    Output
    14
    20
    10
    9
  • Dividing by zero

    int total = 50;
    int count = 0;
    System.out.println(total / count);
    Output
    Crashes with ArithmeticException

    Not on the AP exam: Dividing by zero when a double is involved.

1.4

Assignment Statements and Input

I can trace assignment statements and say what each variable holds.

  • Using a variable before it has a value

    int total;
    System.out.println(total);
    The compiler says
    error: variable total might not have been initialized
  • Assigning and reassigning

    int x = 5;
    int y = x + 1;
    x = x * 3;
    System.out.println(x);
    System.out.println(y);
    Output
    15
    6

    Watch out: = is not "equals" like in math. It means "store this value in that variable."

    Not on the AP exam: An assignment inside another expression, like a = b = 4;.

  • An expression's type

    double result = 5 / 2;
    System.out.println(result);
    Output
    2.0

I can write code that reads input from the keyboard.

  • import java.util.Scanner;
    
    Scanner input = new Scanner(System.in);
    System.out.print("Your name? ");
    String name = input.nextLine();
    System.out.println("Hi " + name);
    If you type Sam
    Your name? Sam
    Hi Sam

    Not on the AP exam: Reading input in any particular way. You will use Scanner in your own programs, but exam questions do not depend on it.

1.5

Casting and Range of Variables

I can cast between int and double, and predict the value that comes out.

  • Casting

    double d = 7.9;
    int n = (int) d;
    System.out.println(n);
    System.out.println((double) 3);
    Output
    7
    3.0
  • Casting cuts off the decimal

    System.out.println((int) 3.99);
    System.out.println((int) 7.5 * 2);
    System.out.println((int) (7.5 * 2));
    Output
    3
    14
    15

    Watch out: A cast applies only to the value right after it. To cast a whole expression, put the expression in parentheses.

  • Putting an int into a double

    int count = 10;
    double total = count;
    System.out.println(total);
    Output
    10.0
  • Rounding

    double a = 4.6;
    double b = -4.6;
    System.out.println((int) (a + 0.5));
    System.out.println((int) (b - 0.5));
    Output
    5
    -5

I can explain when an int calculation goes out of range.

  • The largest and smallest int

    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.MIN_VALUE);
    Output
    2147483647
    -2147483648
  • Overflow

    int big = Integer.MAX_VALUE;
    big = big + 1;
    System.out.println(big);
    Output
    -2147483648

I can explain why math with double values can come out slightly wrong.

  • System.out.println(0.1 + 0.2);
    Output
    0.30000000000000004

    Not on the AP exam: Special decimal types, like BigDecimal, that avoid round-off errors.

1.6

Compound Assignment Operators

I can trace compound assignment operators and ++/--, and say what the variable ends up holding.

  • +=, -=, *=, /=, %=

    int points = 10;
    points += 5;
    points *= 2;
    points %= 7;
    System.out.println(points);
    Output
    2
  • ++ and --

    int count = 0;
    count++;
    count++;
    count--;
    System.out.println(count);
    Output
    1

    Not on the AP exam: The prefix form (++x), and using ++ or -- inside another expression, like arr[x++].

1.7

Application Program Interface (API) and Libraries

I can read documentation to find what a class stores and what it can do.

  • // From the Java Quick Reference: class String
    int length()                        // how many characters
    String substring(int from, int to)  // part of the string
    int indexOf(String str)             // where str first appears
1.8

Documentation with Comments

I can use comments to explain code, and describe a method using its precondition and postcondition.

  • Comments

    // a one-line comment
    /* a block comment
       can cover several lines */
    /** a Javadoc comment */
    System.out.println("Comments never run");
    Output
    Comments never run
  • Preconditions and postconditions

    /**
     * Precondition: count > 0
     * Postcondition: returns the average, total / count
     */
    public static double average(int total, int count)
    {
        return (double) total / count;
    }
    System.out.println(average(170, 2));
    Output
    85.0
1.9

Method Signatures

I can use a method's signature to pick the right method to call.

  • public static void sayHi()                     // sayHi()
    public static void printStars(int n)           // printStars(int)
    public static double area(double w, double h)  // area(double, double)

I can call methods correctly, and trace what happens when they run.

  • void methods

    int x = System.out.println("hi");
    The compiler says
    error: incompatible types: void cannot be converted to int
  • Methods that return a value

    int d = Math.abs(-7);                  // stored
    System.out.println(Math.abs(-3) + d);  // used in an expression
    Math.abs(-5);                          // runs, but the 5 is lost
    Output
    10
  • Arguments are copies

    public static void addTen(int n)
    {
        n = n + 10;
        System.out.println("inside: " + n);
    }
    int x = 5;
    addTen(x);
    System.out.println("outside: " + x);
    Output
    inside: 15
    outside: 5
  • Overloaded methods

    public static void show(int n)
    {
        System.out.println("int " + n);
    }
    
    public static void show(double d)
    {
        System.out.println("double " + d);
    }
    
    public static void show(String s, int n)
    {
        System.out.println(s + n);
    }
    show(4);
    show(4.0);
    show("Lesson ", 3);
    Output
    int 4
    double 4.0
    Lesson 3
  • The order things run in

    public static void middle()
    {
        System.out.println("B");
    }
    System.out.println("A");
    middle();
    System.out.println("C");
    Output
    A
    B
    C
1.10

Calling Class Methods

I can call class methods and figure out what they return.

  • public class Temperature
    {
        public static double toFahrenheit(double celsius)
        {
            return celsius * 9 / 5 + 32;
        }
    }
    double f = Temperature.toFahrenheit(100.0);
    System.out.println(f);
    Output
    212.0
1.11

Math Class

I can use Math methods in expressions, including random numbers in a range, and predict the value produced.

  • abs, pow, and sqrt

    System.out.println(Math.abs(-8));
    System.out.println(Math.abs(-2.5));
    System.out.println(Math.pow(2, 3));
    System.out.println(Math.sqrt(49));
    Output
    8
    2.5
    8.0
    7.0

    Watch out: Math.pow and Math.sqrt always return a double, even when the answer is a whole number.

  • Random numbers in a range

    int die = (int) (Math.random() * 6) + 1;   // 1 to 6
    int coin = (int) (Math.random() * 2);      // 0 or 1
    double temp = Math.random() * 10 + 20;     // 20.0 up to, not including, 30.0
1.12

Objects: Instances of Classes

I can explain the relationship between a class and an object.

  • Two objects, one class

    String a = "cat";
    String b = "horse";
    System.out.println(a.length());
    System.out.println(b.length());
    Output
    3
    5
  • Superclasses and subclasses

    // Object
    // └── Vehicle          has a speed, can move()
    //     ├── Car          gets speed and move() from Vehicle
    //     └── Bike         gets speed and move() from Vehicle

    Not on the AP exam: Designing and writing your own superclasses and subclasses. You only need to know what the words mean.

I can declare variables that hold objects.

  • String a = "hi";   // a holds the address of the "hi" object
    String b = a;      // b gets a copy of that address: same object, not a new one
1.13

Object Creation and Storage (Instantiation)

public class Book
{
    private String title;
    private int pages;

    public Book(String t)
    {
        title = t;
        pages = 0;
    }

    public Book(String t, int p)
    {
        title = t;
        pages = p;
    }

    public int getPages()
    {
        return pages;
    }
}

I can create an object by calling a constructor.

  • Creating an object with new

    Book b = new Book("Wonder", 310);
    System.out.println(b.getPages());
    Output
    310
  • Arguments in the wrong order

    Book c = new Book(310, "Wonder");
    The compiler says
    error: incompatible types: int cannot be converted to String
  • The order things run in

    public class Greeter
    {
        public Greeter()
        {
            System.out.println("B");
        }
    }
    System.out.println("A");
    Greeter g = new Greeter();
    System.out.println("C");
    Output
    A
    B
    C

I can declare a variable of the right type to hold an object.

  • Book next = null;   // no Book yet
    next = new Book("Holes");

I can use its signature to identify which constructor is being called.

  • Book a = new Book("Holes");
    Book b = new Book("Wonder", 310);
    System.out.println(a.getPages());
    System.out.println(b.getPages());
    Output
    0
    310
1.14

Calling Instance Methods

public class Counter
{
    private int count;

    public Counter()
    {
        count = 0;
    }

    public void click()
    {
        count++;
    }

    public int getCount()
    {
        return count;
    }
}

I can call methods on an object, and figure out what those calls do.

  • Each object has its own values

    Counter a = new Counter();
    Counter b = new Counter();
    a.click();
    a.click();
    b.click();
    System.out.println(a.getCount());
    System.out.println(b.getCount());
    Output
    2
    1
  • Calling a method on null

    Counter c = null;
    c.click();
    Output
    Crashes with NullPointerException
1.15

String Manipulation

I can create and combine strings, and predict the result.

  • Two ways to make a String

    String a = "hello";
    String b = new String("hello");
  • Strings are immutable

    String a = "Hi";
    String b = a;
    a = a + "!";
    System.out.println(a);
    System.out.println(b);
    Output
    Hi!
    Hi
  • Joining strings

    String msg = "Score: " + 7;
    msg += "!";
    System.out.println(msg);
    System.out.println("1" + 2 + 3);
    System.out.println(1 + 2 + "3");
    Output
    Score: 7!
    123
    33

    Watch out: Java works left to right. Numbers before the first string are added as numbers. Everything after a string is joined as text.

  • Joining an object to a string

    public class Point
    {
        private int x;
        private int y;
    
        public Point(int x0, int y0)
        {
            x = x0;
            y = y0;
        }
    
        public String toString()
        {
            return "(" + x + ", " + y + ")";
        }
    }
    Point p = new Point(3, 4);
    System.out.println("At " + p);
    Output
    At (3, 4)

    Not on the AP exam: Writing your own toString method.

I can call String methods and predict what they return.

  • Indexes and out of bounds

    String s = "code";
    System.out.println(s.substring(3, 4));
    System.out.println(s.substring(4, 5));
    Output
    e
    Crashes with StringIndexOutOfBoundsException
  • length

    String s = "banana";
    System.out.println(s.length());
    Output
    6
  • substring

    String s = "banana";
    System.out.println(s.substring(1, 4));
    System.out.println(s.substring(2));
    Output
    ana
    nana

    Watch out: substring(from, to) stops before to, so the result is always to - from characters long.

  • indexOf

    String s = "banana";
    System.out.println(s.indexOf("an"));
    System.out.println(s.indexOf("x"));
    Output
    1
    -1
  • equals

    String s = "banana";
    System.out.println(s.equals("Banana"));
    System.out.println(s.equals("banana"));
    Output
    false
    true

    Watch out: Compare strings with equals, not ==.

    Not on the AP exam: Using equals to compare a String with something that is not a String.

  • compareTo

    String s = "banana";
    System.out.println("apple".compareTo(s));
    System.out.println("banana".compareTo(s));
    System.out.println("cherry".compareTo(s));
    Output
    -1
    0
    1
  • One character at a time

    String s = "java";
    System.out.println(s.substring(0, 1));
    System.out.println(s.substring(3, 4));
    Output
    j
    a