1.07 Application Program Interface (API) and Libraries
Call methods from pre-built Java classes like Math and String.
1. Reference Guide
Libraries, APIs, and Packages
| Term | Meaning | Example |
|---|---|---|
| Library | A collection of pre-written classes | Java’s standard library |
| API | Documentation that tells you how to use a class | String API lists length(), substring(), etc. |
| Package | A folder-like grouping of related classes | java.util groups ArrayList, Scanner, Random |
Common Java Packages
| Package | Description | Example Classes | Import needed? |
|---|---|---|---|
java.lang |
Fundamental classes | String, Math, Integer |
No |
java.util |
Utility classes | ArrayList, Scanner, Random |
Yes |
java.io |
Input/Output | File, FileReader |
Yes |
Attributes vs. Behaviors
| Concept | Definition | Example |
|---|---|---|
| Attributes | What object HAS — data stored in variables | name, grade, gpa |
| Behaviors | What object DOES — actions defined by methods | displayInfo(), updateGPA() |
2. LxD Cycle Process
Empathize: I noticed students frequently try to write their own sorting logic, their own text-manipulation loops, or their own random-number generators from scratch, even though Java already ships with tested classes (Arrays, String, Random) that do exactly this. They treat the API documentation as optional reading rather than as the primary tool for finding out what a class can do.
Define:
- POV: CSA students need to be comfortable reading API documentation because real Java development (and the AP Exam’s use of provided classes) assumes you can use a class correctly without ever reading its source code.
- Learning Goal: Students will identify the attributes and behaviors of a class from its API/documentation, explain the relationship between libraries, APIs, and packages, and distinguish attributes (data) from behaviors (methods).
Ideate:
- HMW Question: How might we teach students to trust and read documentation instead of reinventing functionality that already exists?
- Activity: Walking through real classes (
String,Math,ArrayList) the same way you would read a restaurant menu — figuring out what you can order (call) without needing to know how the kitchen (implementation) works.
Prototype & Test: In a trial run, students conflated “API” with “library” and “package” as if they were interchangeable. I added an explicit Reference Guide table that separates the three terms side-by-side, which resolved the confusion.
3. College Board Requirements
Topic 1.7, Application Program Interface (API) and Libraries, is required content on the AP Computer Science A Exam. The College Board’s official course framework states:
“Libraries are collections of classes. An application programming interface (API) specification informs the programmer how to use those classes. Documentation found in API specifications and libraries is essential to understanding the attributes and behaviors of a class defined by the API… Classes in the APIs and libraries are grouped into packages.” (College Board, 2025, p. 39)
It also defines the two vocabulary words this lesson centers on:
“Attributes refer to the data related to the class and are stored in variables. Behaviors refer to what instances of the class can do (or what can be done with them) and are defined by methods.” (College Board, 2025, p. 39)
4. Lesson Plan
Learning Objective: By the end of this lesson, you will be able to identify the attributes and behaviors of a class found in a library’s API, explain how packages organize classes, and use existing library classes instead of writing equivalent code from scratch.
Success Criteria: You can read documentation for an unfamiliar class (like ArrayList or Math) and correctly call its constructors and methods, and you can label a class member as either an attribute or a behavior.
Tech Talk & Introduction (5 minutes)
A library is a collection of pre-written classes that you can use in your programs — a toolbox, not something you build from scratch. An API (Application Programming Interface) is the instruction manual for that toolbox: like a restaurant menu, it tells you what you can order (which constructors and methods exist) without requiring you to know how the kitchen works internally. Packages organize related classes into groups, the way folders organize files.
Why do we do this? Every professional Java program is built on existing libraries. Learning to read an API’s documentation for its attributes (data) and behaviors (methods) is what lets you use a class you have never seen before, correctly, on the first try.
5. Code Examples
A. Simple: Using a Class from Java’s Library
Instead of writing your own square-root algorithm, you call the one already provided by the Math library class.
// CODE_RUNNER: Library Demo
// Run it, then change a value and predict the new output
public class CodeRunner1_LibraryDemo {
public static void main(String[] args) {
// Using String class from library
String message = "Hello, CSA!";
// Using Math class from library
double result = Math.sqrt(16);
System.out.println(message);
System.out.println("Square root of 16: " + result);
}
}
Hello, CSA!
Square root of 16: 4.0
B. Intermediate: Reading the String API
The String API tells us what we can call — length(), toUpperCase(), substring(start, end) — without needing to know how String is implemented internally.
// CODE_RUNNER: API Demo with String Methods
// Run it, then change a value and predict the new output
public class CodeRunner2_APIDemo {
public static void main(String[] args) {
String text = "Computer Science";
// Methods from String API
System.out.println("Original: " + text);
System.out.println("Length: " + text.length());
System.out.println("Uppercase: " + text.toUpperCase());
System.out.println("First 8 chars: " + text.substring(0, 8));
}
}
Original: Computer Science
Length: 16
Uppercase: COMPUTER SCIENCE
First 8 chars: Computer
C. Intermediate: Packages and Imports
java.lang classes (String, Math) never need an import. Everything else, like ArrayList from java.util, does.
// CODE_RUNNER: Package Demo
// Run it, then change a value and predict the new output
import java.util.ArrayList; // Need import for java.util
public class CodeRunner3_PackageDemo {
public static void main(String[] args) {
// java.lang - no import needed
String name = "Java";
int num = Math.abs(-5);
// java.util - import needed
ArrayList<String> list = new ArrayList<>();
list.add("Item 1");
System.out.println("String: " + name);
System.out.println("Absolute value: " + num);
System.out.println("ArrayList: " + list);
}
}
String: Java
Absolute value: 5
ArrayList: [Item 1]
D. Complex: Following Documentation for ArrayList
Documentation tells you the constructors, methods, parameters, and return types a class provides. Here we use only what the ArrayList documentation says exists: ArrayList(), add(element), get(index), and size().
// CODE_RUNNER: Documentation Demo with ArrayList
// Run it, then change a value and predict the new output
import java.util.ArrayList;
public class CodeRunner4_DocumentationDemo {
public static void main(String[] args) {
// Constructor from documentation
ArrayList<Integer> scores = new ArrayList<>();
// Methods from documentation
scores.add(95); // add(element)
scores.add(87);
scores.add(92);
System.out.println("Scores: " + scores);
System.out.println("First score: " + scores.get(0)); // get(index)
System.out.println("Total scores: " + scores.size()); // size()
}
}
Scores: [95, 87, 92]
First score: 95
Total scores: 3
E. Complex: Attributes of a Class
A Student object’s attributes are the data it HAS — name, grade, gpa — stored as instance variables.
// CODE_RUNNER: Student Attributes Demo
// Run it, then change a value and predict the new output
public class CodeRunner5_StudentAttributes {
// ATTRIBUTES - what the student HAS
private String name;
private int grade;
private double gpa;
// Constructor to set attributes
public CodeRunner5_StudentAttributes(String name, int grade, double gpa) {
this.name = name;
this.grade = grade;
this.gpa = gpa;
}
// Method to display attributes
public void showInfo() {
System.out.println("Name: " + name);
System.out.println("Grade: " + grade);
System.out.println("GPA: " + gpa);
}
public static void main(String[] args) {
// Create a student object
CodeRunner5_StudentAttributes alice = new CodeRunner5_StudentAttributes("Alice", 11, 3.8);
alice.showInfo();
}
}
Name: Alice
Grade: 11
GPA: 3.8
F. Complex: Behaviors of a Class
A Student object’s behaviors are what it DOES — displayInfo(), updateGPA(), promoteGrade(), isHonorRoll() — defined as methods.
// CODE_RUNNER: Student Behaviors Demo
// Run it, then change a value and predict the new output
public class CodeRunner6_StudentBehaviors {
// Attributes
private String name;
private int grade;
private double gpa;
public CodeRunner6_StudentBehaviors(String name, int grade, double gpa) {
this.name = name;
this.grade = grade;
this.gpa = gpa;
}
// BEHAVIORS - what the student DOES
public void displayInfo() {
System.out.println(name + " - Grade " + grade + " - GPA: " + gpa);
}
public void updateGPA(double newGPA) {
this.gpa = newGPA;
System.out.println(name + "'s GPA updated to " + gpa);
}
public void promoteGrade() {
grade++;
System.out.println(name + " promoted to grade " + grade);
}
public boolean isHonorRoll() {
return gpa >= 3.5;
}
public static void main(String[] args) {
// Testing behaviors
CodeRunner6_StudentBehaviors bob = new CodeRunner6_StudentBehaviors("Bob", 10, 3.6);
bob.displayInfo();
bob.updateGPA(3.8);
bob.promoteGrade();
System.out.println("Honor roll: " + bob.isHonorRoll());
}
}
Bob - Grade 10 - GPA: 3.6
Bob's GPA updated to 3.8
Bob promoted to grade 11
Honor roll: true
6. Hacks & Practice Tasks
Submission Safety Rules (Read First)
[!IMPORTANT] To avoid grading errors, follow these rules exactly:
- Run every code cell and leave the output visible before submitting.
- Use only classes and methods that appear in the Reference Guide above (or official Java documentation) — do not hand-roll your own sort/search/random logic.
- Add an
importstatement whenever you use a class outsidejava.lang.- Clearly label which lines are attributes and which are behaviors when asked.
Popcorn Hack #1: Using Documentation (5 minutes)
[!TIP] Look up
Math.pow()andArrayList.add()in the API before you start typing — you should not need to guess a method’s name.
Task: Use the Math class and ArrayList class based on their documentation.
Complete the following:
- Use
Math.pow()to calculate 3^4 - Use
Math.sqrt()to find square root of 64 - Create an ArrayList of Strings
- Add 3 colors to the ArrayList
- Print the ArrayList size
// CODE_RUNNER: Popcorn Hack #1
// Run it, then complete the TODOs and predict the new output
import java.util.ArrayList;
public class CodeRunner7_PopcornHack1 {
public static void main(String[] args) {
// TODO: Use Math.pow() to calculate 3^4
// TODO: Use Math.sqrt() to find square root of 64
// TODO: Create ArrayList of Strings
// TODO: Add 3 colors ("red", "blue", "green")
// TODO: Print the size
}
}
Popcorn Hack #2: Attributes and Behaviors (5 minutes)
[!TIP] Write your attribute list first, then design each behavior as a method that reads or changes one of those attributes.
Task: Create a Book class with attributes and behaviors.
Requirements:
Attributes (3):
title(String)author(String)pages(int)
Behaviors (3 methods):
- Constructor to set all attributes
displayInfo()- print all book infoisLong()- return true if pages > 300
Test: Create a Book object and call all methods.
// CODE_RUNNER: Popcorn Hack #2
// Run it, then complete the TODOs and predict the new output
public class CodeRunner8_PopcornHack2 {
// TODO: Add 3 attributes (title, author, pages)
// TODO: Add constructor
// TODO: Add displayInfo() method
// TODO: Add isLong() method (returns true if pages > 300)
public static void main(String[] args) {
// TODO: Create a Book object and test all methods
// Example: Book myBook = new Book("Java Basics", "John Doe", 350);
}
}
Homework Hack
Once you have completed and run both Popcorn Hacks above, continue to the full homework assignment:
Task: Choose one class from java.util that we have not covered in class (for example HashMap, Scanner, or Collections). Read its official API documentation, then write a short program that correctly calls at least three of its methods. In a comment above each method call, note whether you are using an attribute or a behavior, and cite the exact line of the documentation that told you the method existed.
Grading Plan (1 Point Total)
Classroom Rubric
-
0.2 points: Popcorn completion Both Popcorn Hacks are completed, run, and produce correct output using only documented methods.
-
0.8 points: Homework completion
- 0.3 Correct API usage: All method calls match the class’s real documented signature (correct parameter count/types).
- 0.3 Attribute/behavior labeling: Each call is correctly labeled as using an attribute or a behavior.
- 0.2 Documentation citation: Each method call cites the specific documentation line that justified it.
Quick Validation Checklist
- Present: all code cells executed with visible output
- Present: at least one class from
java.utilnot covered in the lesson - Present: an
importstatement for every non-java.langclass used - Absent: hand-written logic duplicating functionality the library already provides
7. Lesson Revisions & Feedback Evidence
Feedback Received: Early feedback noted that students could recite “library, API, package” as separate vocabulary words but could not explain how they relate to one another, and the original lesson never distinguished an attribute from a behavior until the very end.
Revision Made: I moved the Attributes vs. Behaviors table into the Reference Guide alongside the Libraries/APIs/Packages table so both vocabulary pairs are visible side by side from the start, instead of being introduced six sections apart.
Additional Refinement: The original Popcorn Hacks were embedded between unrelated topic sections. I grouped both Popcorn Hacks and the Homework Hack into a single Hacks & Practice Tasks section with explicit submission safety rules, matching the structure students already see in other Unit 1 lessons.
8. References
Reference List
Bloch, J. (2006). How to design a good API and why it matters. In Companion to the 21st ACM SIGPLAN Symposium on Object-Oriented Programming Systems, Languages, and Applications (OOPSLA ‘06) (pp. 506–507). Association for Computing Machinery. https://doi.org/10.1145/1176617.1176622
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description.pdf
Outside Academic Reference
Why documentation-first thinking matters beyond the AP Exam is argued directly by Joshua Bloch, longtime lead designer of the Java Collections Framework (the source of ArrayList in this lesson): “Public APIs are forever — one chance to get it right” (Bloch, 2006, p. 506). A well-designed, well-documented API — like the ones we practiced reading today — is meant to be used correctly by someone who has never seen its source code, which is exactly the skill this lesson builds.
Submit Assignment
Need to update a submission later? Open the submissions dashboard.