UNIT 1: USING OBJECTS AND METHODS · CHEAT SHEET
Using Objects and Methods — the one-page version
Every key term and every exam tip from the 15 topics in this unit. Print it, fold it, read it on the bus.
1.1Introduction to Algorithms, Programming, and Compilers
- Compiler
- translates Java source code into a form the computer can run, reporting syntax errors
- Compile-time error
- an error the compiler detects; the program cannot run
- Run-time error
- an error that occurs during execution, such as an exception
- Logic error
- code that runs but produces incorrect results
Tip: If a question shows code and asks "what happens," check syntax first (missing semicolon, mismatched braces, undeclared variable = won't compile), then look for run-time traps (division by zero, null, bad index), then trace for logic.
1.2Variables and Data Types
- Primitive type
- int, double, boolean — stores the value directly
- Reference type
- a variable that stores the address of an object
- final
- keyword that makes a variable's value unchangeable after initialization
- Overflow
- an int calculation exceeding the representable range wraps around
Tip: Questions on ranges are usually about overflow:
Integer.MAX_VALUE + 1 equals Integer.MIN_VALUE, not an error. And a variable declared but never assigned can't be used — that's a compile error, not a default of 0.1.3Expressions and Output
- Integer division
- int / int drops the fractional part
- Remainder (%)
- the remainder after division; sign follows the left operand
- Concatenation
- joining Strings with +; any + involving a String produces a String
- println
- prints a value followed by a newline
Tip: Look at both operands of every
/. Both int? Truncate. Either double? Decimal result. For the + trap, scan left to right and note the moment a String appears — from then on everything is concatenation.1.4Assignment Statements and Input
- Assignment (=)
- stores the value of the right-side expression in the left-side variable
- Scanner
- a class for reading input from the keyboard or a file
- nextLine()
- reads an entire line of input as a String
- nextInt()
- reads the next integer token, leaving the newline in the buffer
Tip: Questions about Scanner are usually "which method reads a whole line vs. one token" (
nextLine vs next) or the nextInt-then-nextLine trap. Also remember: = assigns; a question with if (x = 5) is a compile error because an int isn't a boolean.1.5Casting and Range of Variables
- Cast
- an explicit conversion, e.g. (int) or (double)
- Truncation
- dropping the fractional part when casting to int
- Widening
- automatic conversion from int to double
- Narrowing
- conversion from double to int, requires a cast
Tip: Ask two things for every cast: what exactly is being cast (the next value only, unless parentheses say otherwise), and is it truncation or rounding (a bare (int) always truncates). Toward zero: (int) -2.9 is -2.
1.6Compound Assignment Operators
- Compound assignment
- operators like += that combine arithmetic with assignment
- Increment (++)
- adds 1 to a variable
- Decrement (--)
- subtracts 1 from a variable
Tip: Rewrite every compound operator as its long form before tracing:
x *= a + b → x = x * (a + b). The parentheses are the part students drop.1.7Application Program Interface (API) and Libraries
- Library
- a collection of pre-written classes available for use
- API
- documentation describing how to use a library's classes and methods
- import
- a statement that makes a class outside java.lang available in a file
- Java Quick Reference
- the list of methods provided on the AP exam
Tip: If a question asks why a program won't compile and it uses Scanner or ArrayList without an import, that's the answer. If it asks what a method returns, look at the header's return type — the exam sometimes gives you a header for a made-up method and expects you to use it correctly from the header alone.
1.8Documentation with Comments
- Precondition
- a condition that must be true before a method is called
- Postcondition
- a condition guaranteed to be true after a method finishes
- Javadoc
- a /** */ comment used to document classes and methods
Tip: Questions ask "which statement is a precondition?" — look for what must be true of the input before calling. Postconditions describe the result. And when an FRQ states a precondition, don't waste time writing code to check it.
1.9Method Signatures
- Method signature
- the method name and its parameter types in order
- Return type
- the type of value a method produces, or void
- Overloading
- multiple methods with the same name but different parameter lists
- Parameter
- a variable in a method header
- Argument
- a value passed to a method when it is called
Tip: To decide which overloaded method a call uses, count the arguments and match their types. When an FRQ gives you a method header to implement, copy it exactly — changing the return type or a parameter name costs points.
1.10Calling Class Methods
- Class method / static method
- a method that belongs to the class and is called with the class name
- Return statement
- ends the method and sends a value back to the caller
Tip: Trace method calls by writing down the line you're returning to, evaluating the method with the argument values, then substituting the return value back. Multiple calls on one line are evaluated left to right.
1.11Math Class
- Math.random()
- returns a double in [0.0, 1.0)
- Math.pow(a, b)
- a raised to b, returned as a double
- Math.abs(x)
- absolute value
- Math.sqrt(x)
- square root as a double
Tip: For "which expression produces a random integer from a to b," check two things: the multiplier equals the count (b − a + 1), and the added value is a. For "what range does this produce," the minimum is the added value and the maximum is that plus the multiplier minus one.
1.12Objects: Instances of Classes
- Class
- a template that defines the attributes and behaviors of a type of object
- Object
- a specific instance of a class
- Attribute
- data stored in an object, held in instance variables
- Behavior
- what an object can do, defined by its methods
Tip: Class vs. object questions are definitional: class = template/type, object = one instance with specific values. "How many objects are created" = count the
new keywords (plus string literals).1.13Object Creation and Storage (Instantiation)
- Instantiation
- creating an object with new
- Constructor
- a special method that initializes a new object; same name as the class, no return type
- Reference
- the address of an object, stored in a reference variable
- Aliasing
- two reference variables referring to the same object
- null
- a reference value meaning 'no object'
- NullPointerException
- the run-time error from calling a method on null
Tip: Draw boxes and arrows. Each
new is a box; each variable is an arrow. Assignment between reference variables re-points an arrow — it never copies a box. If an arrow points to nothing (null) and you call a method through it, the program crashes.1.14Calling Instance Methods
- Instance method
- a method called on an object that operates on that object's data
- Accessor
- a method that returns information about an object without modifying it
- Mutator
- a method that changes an object's state
Tip: When a question gives you a class's method headers and asks what a code segment does, check each call: is it a mutator (state changed for later lines) or an accessor (value used now)? Track object state in a table just like variables.
1.15String Manipulation
- Immutable
- cannot be changed; String methods return new Strings
- substring(a, b)
- characters from index a up to but not including b
- indexOf(str)
- index of the first occurrence, or -1 if not found
- equals
- compares String contents
- compareTo
- returns a negative, zero, or positive int based on alphabetical order
Tip: Three checks on every String question: (1) substring's second index is exclusive — count characters from
from to to - 1; (2) any content comparison must use .equals, never ==; (3) compareTo gives an int, so if (a.compareTo(b)) won't compile — you need < 0, == 0, or > 0.