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);Output25
I can explain what happens between writing code and running it.
-
System.out.println("Line 1"); System.out.println("Line 2")The compiler sayserror: ';' expected
I can tell a syntax error, a logic error, and a run-time error apart.
Syntax error
System.out.println("Hi);The compiler sayserror: unclosed string literal
Logic error
int a = 90; int b = 80; double average = a + b / 2; System.out.println(average);Output130.0
Run-time error
int slices = 8; int people = 0; System.out.println(slices / people);OutputCrashes withArithmeticException
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, andchar.Changing a variable
int lives = 3; lives = 2; System.out.println(lives);Output2
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");OutputHelloWorld! 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");Output42 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");OutputShe 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);Output9 9.0 4.5
Not on the AP exam: Math that produces special
doublevalues, like infinity orNaN.Integer division
System.out.println(7 / 2); System.out.println(7 / 2.0); System.out.println(2 / 5);Output3 3.5 0
Remainder with %
System.out.println(17 % 5); System.out.println(10 % 2); System.out.println(3 % 8);Output2 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);Output14 20 10 9
Dividing by zero
int total = 50; int count = 0; System.out.println(total / count);OutputCrashes withArithmeticExceptionNot on the AP exam: Dividing by zero when a
doubleis involved.
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 sayserror: 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);Output15 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);Output2.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 SamYour name? Sam Hi Sam
Not on the AP exam: Reading input in any particular way. You will use
Scannerin your own programs, but exam questions do not depend on it.
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);Output7 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));Output3 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);Output10.0
Rounding
double a = 4.6; double b = -4.6; System.out.println((int) (a + 0.5)); System.out.println((int) (b - 0.5));Output5 -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);Output2147483647 -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);Output0.30000000000000004
Not on the AP exam: Special decimal types, like
BigDecimal, that avoid round-off errors.
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);Output2
++ and --
int count = 0; count++; count++; count--; System.out.println(count);Output1
Not on the AP exam: The prefix form (
++x), and using++or--inside another expression, likearr[x++].
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
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");OutputComments 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));Output85.0
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 sayserror: 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 lostOutput10
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);Outputinside: 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);Outputint 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");OutputA B C
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);Output212.0
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));Output8 2.5 8.0 7.0
Watch out:
Math.powandMath.sqrtalways return adouble, 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
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());Output3 5
Superclasses and subclasses
// Object // └── Vehicle has a speed, can move() // ├── Car gets speed and move() from Vehicle // └── Bike gets speed and move() from VehicleNot 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
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());Output310
Arguments in the wrong order
Book c = new Book(310, "Wonder");The compiler sayserror: 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");OutputA 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());Output0 310
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());Output2 1
Calling a method on null
Counter c = null; c.click();OutputCrashes withNullPointerException
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);OutputHi! Hi
Joining strings
String msg = "Score: " + 7; msg += "!"; System.out.println(msg); System.out.println("1" + 2 + 3); System.out.println(1 + 2 + "3");OutputScore: 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);OutputAt (3, 4)
Not on the AP exam: Writing your own
toStringmethod.
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));Outpute
Crashes withStringIndexOutOfBoundsExceptionlength
String s = "banana"; System.out.println(s.length());Output6
substring
String s = "banana"; System.out.println(s.substring(1, 4)); System.out.println(s.substring(2));Outputana nana
Watch out:
substring(from, to)stops beforeto, so the result is alwaysto - fromcharacters long.indexOf
String s = "banana"; System.out.println(s.indexOf("an")); System.out.println(s.indexOf("x"));Output1 -1
equals
String s = "banana"; System.out.println(s.equals("Banana")); System.out.println(s.equals("banana"));Outputfalse true
Watch out: Compare strings with
equals, not==.Not on the AP exam: Using
equalsto compare aStringwith something that is not aString.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));Outputj a