AP CSA Unit 1: Using Objects and Methods - Complete Study Guide
🎓 AP CSA Unit 1: Using Objects and Methods
Master Java fundamentals with video tutorials, hands-on coding practice, comprehensive notes, and exam-style practice problems.
📊 15–25% of AP Exam | ~32–34 Class PeriodsIntroduction to Algorithms, Programming, and Compilers
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
What is an Algorithm?
An algorithm is a step-by-step process for solving a problem. Algorithms can be represented using written language, diagrams, or pseudocode before being translated into program code.
Sequencing
Sequencing defines the order in which steps are completed. In programming, statements execute one at a time, from top to bottom.
Programs and Compilers
Java is a compiled language. You write source code in .java files, then the compiler translates it into bytecode that runs on the Java Virtual Machine (JVM).
Types of Programming Errors
| Error Type | Description | When Detected |
|---|---|---|
| Syntax Error | Rules of the language not followed | Compile time |
| Logic Error | Program runs but produces wrong results | Testing |
| Run-time Error | Error during execution (dividing by zero) | Execution |
| Exception | A type of run-time error | Execution |
- Missing semicolon at end of statement
- Mismatched parentheses or braces
- Misspelled keywords (
systeminstead ofSystem)
Variables and Data Types
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
What is a Variable?
A variable is a named storage location in memory. Every variable has a type, name, and value.
Data Type Categories
- Primitive types - Store actual values (int, double, boolean)
- Reference types - Store references to objects (String, Scanner)
Primitive Data Types (AP CSA Required)
| Type | Description | Example |
|---|---|---|
int |
Whole numbers | int age = 16; |
double |
Decimal numbers | double gpa = 3.75; |
boolean |
true or false | boolean isStudent = true; |
// Declaration with initialization int score = 100; double price = 19.99; boolean isPassing = true;
Expressions and Output
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
Printing Output
System.out.print("Hello"); // No newline after
System.out.println("World"); // Newline after
Escape Sequences
| Escape | Meaning |
|---|---|
\n |
Newline |
\" |
Double quote |
\\ |
Backslash |
Arithmetic Operators
| Operator | Operation | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 5 / 3 |
1 |
% |
Remainder | 5 % 3 |
2 |
5 / 2 = 2 (not 2.5!)
5 / 2 // 2 (integer division) 5.0 / 2 // 2.5 (floating-point division) 5 / 2.0 // 2.5
Operator Precedence
Java follows standard math order: parentheses first, then * / %, then + -
int result = 2 + 3 * 4; // 14 (not 20!) int result2 = (2 + 3) * 4; // 20
Assignment Statements and Input
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
The Assignment Operator (=)
The = operator means assignment, not equality. It stores the value on the right into the variable on the left.
int x = 5; // x now holds 5 x = x + 3; // x now holds 8
Variable Initialization
A variable must be assigned a value before it can be used.
The null Literal
Reference type variables can hold null, meaning they don't refer to any object.
Reading Input with Scanner
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.print("Enter your age: ");
int age = input.nextInt();
Common Scanner Methods
| Method | Returns |
|---|---|
nextInt() |
int |
nextDouble() |
double |
next() |
String (one word) |
nextLine() |
String (entire line) |
Casting and Range of Variables
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
What is Casting?
Casting converts a value from one type to another.
- Implicit (automatic) - int → double (widening)
- Explicit (manual) - double → int (narrowing)
double d = 5; // 5 becomes 5.0 automatically int n = (int) 5.8; // n = 5 (truncated!)
(int) 5.9 // = 5 (not 6!) (int) 5.1 // = 5 (int) -3.7 // = -3
Rounding with Casting
// For positive numbers: add 0.5 before casting int rounded = (int)(5.7 + 0.5); // = 6 // For negative numbers: subtract 0.5 int roundedNeg = (int)(-5.7 - 0.5); // = -6
Integer Range and Overflow
-
Integer.MIN_VALUE= -2,147,483,648 -
Integer.MAX_VALUE= 2,147,483,647
int max = Integer.MAX_VALUE; System.out.println(max + 1); // -2147483648 (wraps!)
Compound Assignment Operators
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
Compound Assignment Operators
| Operator | Example | Equivalent To |
|---|---|---|
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
%= |
x %= 5 |
x = x % 5 |
Increment and Decrement Operators
| Operator | Example | Equivalent To |
|---|---|---|
++ |
x++ |
x = x + 1 |
-- |
x-- |
x = x - 1 |
int count = 0; count++; // count = 1 count++; // count = 2 count--; // count = 1
Application Program Interface (API) and Libraries
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
What is an API?
An API (Application Programming Interface) is documentation describing how to use a library of classes, including what methods are available, their parameters, and return types.
Libraries and Packages
| Package | Contains | Import Needed? |
|---|---|---|
java.lang |
String, Math, Integer, Object | No (automatic) |
java.util |
Scanner, ArrayList, Random | Yes |
Attributes and Behaviors
- Attributes - Data stored in variables
- Behaviors - Actions defined by methods
Documentation with Comments
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
Types of Comments
// Single-line comment /* Multi-line comment */ /** * Javadoc comment for documentation */
Preconditions and Postconditions
Preconditions describe what must be true BEFORE a method is called.
Postconditions describe what will be true AFTER a method executes.
/** * Calculates the square root of a number. * Precondition: x >= 0 * Postcondition: returns the square root of x */
Method Signatures
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
What is a Method?
A method is a named block of code that only runs when called. Procedural abstraction allows using methods without knowing their implementation.
Anatomy of a Method Signature
public static int max(int a, int b) │ │ │ │ └── Parameters │ │ │ └────── Method name │ │ └────────── Return type │ └───────────────── static keyword └──────────────────────── Access modifier
Parameters vs Arguments
- Parameters - Variables in the method definition
- Arguments - Values passed when calling the method
void vs Non-void Methods
| Type | Returns | How to Call |
|---|---|---|
| void | Nothing | methodName(); |
| Non-void | A value | var = methodName(); |
Method Overloading
Methods are overloaded when multiple methods have the same name but different parameter lists.
Call by Value
Java uses call by value - the method receives a copy of the argument's value.
Calling Class Methods
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
What are Class Methods?
Class methods (also called static methods) belong to the class itself, not to any specific object. They're marked with the static keyword.
How to Call Class Methods
// Syntax: ClassName.methodName(arguments) double result = Math.sqrt(25); // 5.0 double power = Math.pow(2, 3); // 8.0 int absolute = Math.abs(-42); // 42
-
Class methods:
ClassName.methodName() -
Instance methods:
objectName.methodName()
Math Class
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
Math Class Methods (On AP Quick Reference)
| Method | Description | Example |
|---|---|---|
Math.abs(int x) |
Absolute value (int) |
Math.abs(-5) → 5 |
Math.abs(double x) |
Absolute value (double) |
Math.abs(-3.7) → 3.7 |
Math.pow(base, exp) |
Power |
Math.pow(2, 3) → 8.0 |
Math.sqrt(x) |
Square root |
Math.sqrt(25) → 5.0 |
Math.random() |
Random [0.0, 1.0) | 0.73... |
Using Math.random()
Math.random() returns a double where 0.0 ≤ value < 1.0
Random Integer Formulas:
// Random int from 0 to n-1 int r = (int)(Math.random() * n); // Random int from 1 to n (like a die) int r = (int)(Math.random() * n) + 1; // Random int from min to max (inclusive) int r = (int)(Math.random() * (max - min + 1)) + min;
// Roll a die (1-6) int die = (int)(Math.random() * 6) + 1; // Random 0 or 1 (coin flip) int flip = (int)(Math.random() * 2);
Math.random() NEVER returns exactly 1.0!
Objects: Instances of Classes
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
Objects and Classes
- Class - A blueprint/template that defines attributes and behaviors
- Object - A specific instance of a class with actual values
Class: Cookie cutter (template)
Object: Individual cookies (instances)
Reference Variables
Reference variables hold the memory address of an object, not the object itself:
int age = 16; // Primitive: holds actual value String name = "Alice"; // Reference: holds address of object
Inheritance (Conceptual)
- Superclass - Parent class
- Subclass - Child class that inherits from superclass
- All classes inherit from the
Objectclass
Object Creation and Storage (Instantiation)
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
Creating Objects with new
// Syntax: ClassName objectName = new ClassName(arguments);
Scanner input = new Scanner(System.in);
Random rand = new Random();
String greeting = new String("Hello");
Constructors
Constructors are special methods that create and initialize objects:
- Have the same name as the class
- Don't have a return type
- Can be overloaded
null References
String name = null; // Doesn't reference any object // Danger: Using null causes NullPointerException! String text = null; int len = text.length(); // CRASH!
Calling Instance Methods
Watch & Practice
🎥 Video Tutorial
💻 Try It Yourself
Instance Methods vs Class Methods
| Instance Methods | Class Methods | |
|---|---|---|
| Called on | An object | The class itself |
| Syntax | object.method() |
Class.method() |
| Example | str.length() |
Math.sqrt(16) |
// Create an object first String message = "Hello World"; // Call instance methods using dot operator int len = message.length(); // 11 String upper = message.toUpperCase(); // "HELLO WORLD"
String text = null; int len = text.length(); // CRASH!
📝 Unit 1 Quiz (15 Questions)
Test your understanding of all Unit 1 concepts!
Question 1: What is printed?
int a = 5; int b = 2; double result = a / b; System.out.println(result);
Question 2: Which correctly creates a Scanner?
Question 3: Value of x after: int x = 10; x += 3; x -= 4;
Question 4: Random integer 1-6 (die roll)?
Question 5: "APCSA".substring(1, 4)?
Question 6: Compare Strings for equal content?
Question 7: What prints? String s = "hello"; s.toUpperCase(); System.out.println(s);
Question 8: Result of (int) 7.9?
Question 9: Value of 17 % 5?
Question 10: Primitives vs Objects?
Question 11: Error detected by compiler?
Question 12: "programming".indexOf("gram")?
Question 13: Math.sqrt(Math.pow(3,2) + Math.pow(4,2))?
Question 14: Valid method signature?
Question 15: "banana".compareTo("apple")?
📝 Ready for the Full Unit 1 Exam?
30 multiple-choice questions and 2 free-response questions!
Take Unit 1 Exam →