1.07 Application Program Interface (API) and Libraries
Use library classes like Math, String, and ArrayList by reading their API documentation, import classes from packages, and tell a class's attributes from its behaviors.
1. Reference Guide
| Term | Definition / Example | College Board Requirement |
|---|---|---|
| Library | A collection of classes someone already wrote and tested: Java’s standard library | Unit 1, Topic 1.7, p. 39 |
| API | The documentation that says how to use a library’s classes: the String API lists length() and substring(int, int) |
Unit 1, Topic 1.7, p. 39 |
| Package | A named group of related classes: java.util holds ArrayList and Scanner |
Unit 1, Topic 1.7, p. 39 |
import |
Lets you use a class from another package by its short name: import java.util.ArrayList; |
— |
| Attribute | Data an object has, stored in variables: a book’s title and pages |
Unit 1, Topic 1.7, p. 39 |
| Behavior | What an object can do, defined by methods: a book’s isLong() |
Unit 1, Topic 1.7, p. 39 |
- Classes in
java.lang, likeStringandMath, never need animport - An API entry gives the method’s name, parameter types, and return type. That is everything you need to call it correctly
Common Java Packages
| Package | Description | Example Classes | Import needed? |
|---|---|---|---|
java.lang |
Core classes | String, Math, Integer |
No |
java.util |
Utility classes | ArrayList, Scanner, Random |
Yes |
java.io |
Input and output | File, FileReader |
Yes |
Reading an API Entry
| API Entry | Return Type | Call It Like This |
|---|---|---|
static double sqrt(double a) |
double |
double r = Math.sqrt(64); |
static double pow(double a, double b) |
double |
double p = Math.pow(3, 4); |
int length() |
int |
int n = text.length(); |
String substring(int beginIndex, int endIndex) |
String |
String s = text.substring(0, 8); |
boolean add(E e) (ArrayList) |
boolean |
list.add("red"); |
int size() (ArrayList) |
int |
int n = list.size(); |
Attributes vs. Behaviors
| Concept | Definition | Example |
|---|---|---|
| Attributes | What an object HAS: data stored in variables | name, grade, gpa |
| Behaviors | What an object DOES: actions defined by methods | displayInfo(), updateGPA() |
Picking the Right Call
- The entry says
static→ call it on the class name:Math.sqrt(64) - The entry has no
static→ call it on an object:text.length() - The return type is not
void→ store or use the result - The class is not in
java.lang→ add animportat the top of the file
College Board Requirements
AP CSA Unit 1, Topic 1.7 Application Program Interface (API) and Libraries. Quoted from the course and exam description (College Board, 2025, p. 39):
- “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.”
- “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.”
Reference: College Board, AP Computer Science A Course and Exam Description (2025), Unit 1, Topic 1.7, p. 39.
2. Real-Life Application
Eleven Lines That Broke Thousands of Projects
In March 2016, a developer removed a tiny library called left-pad from npm, the package registry for JavaScript. It had about eleven lines of code that added spaces to the left of a string. Thousands of projects, including the build tools behind big websites, used it, so their builds started failing within hours. Everyone guess: why would huge projects depend on a library that small instead of writing those eleven lines themselves?
| Real Case | What Happened | Java Idea in This Lesson |
|---|---|---|
left-pad (2016) |
Projects used a tiny library through its API, so removing it broke them all at once | Using a library means depending on its API |
| Square roots | Nobody writes their own square root routine | Math.sqrt(64) is already written and tested |
| Lists that grow | Java’s ArrayList handles resizing for you |
import java.util.ArrayList;, then call add and size |
| A school app | A student record has data and actions | Attributes like gpa, behaviors like updateGPA() |
The takeaway: libraries save you from rewriting tested code, and the API tells you exactly how to call them. Read the documentation before you write your own version.
3. Tech Talk
Learning Objective: Use classes from Java’s libraries by reading their API documentation, import classes from packages, and name the attributes and behaviors of a class.
Success Criteria: You can call a method you have never used before from its API entry alone, add the import it needs, and label each member of a class as an attribute or a behavior.
A library is a toolbox. Its API is the label on each tool: it tells you what you can call, without showing how the tool is built. Packages group related tools.
double root = Math.sqrt(64); // java.lang: no import
String word = "Computer";
int n = word.length(); // 8
import java.util.ArrayList; // goes at the top of the file
ArrayList<String> colors = new ArrayList<>();
colors.add("red"); // documented as add(E e)
ArrayList is covered in depth in Unit 4. Here it is just an example of reading documentation for a class outside java.lang.
Code Examples
A. Library classes without an import. The LibraryDemo class calls Math and String from their API entries
Every call below comes from an API entry in the Reference Guide. Math methods are static, so they are called on the class name. String methods are called on a String object:
Math.sqrt(16) // static double sqrt(double a)
text.substring(0, 8) // String substring(int beginIndex, int endIndex)
Terminology:
java.lang is the package Java imports for you, which is why String and Math need no import line.
Code Runner Challenge
Predict each line from its API entry, then run it
View IPYNB Source
// CODE_RUNNER: Predict each line from its API entry, then run it
public class LibraryDemo {
public static void main(String[] args) {
// Math: static methods, called on the class name
System.out.println("Square root of 16: " + Math.sqrt(16));
System.out.println("3 to the 4th: " + Math.pow(3, 4));
System.out.println("Absolute value of -5: " + Math.abs(-5));
// String: methods called on a String object
String text = "Computer Science";
System.out.println("Length: " + text.length());
System.out.println("First 8 chars: " + text.substring(0, 8));
System.out.println("Index of Science: " + text.indexOf("Science"));
}
}
LibraryDemo.main(null);
B. Attributes, behaviors, and an import. The StudentRecord class keeps its courses in an ArrayList
StudentRecord has three attributes and four behaviors. One attribute is an ArrayList, which lives in java.util, so the file needs an import. Every ArrayList call (add, size, get) comes from its API.
import java.util.ArrayList; // without this line, ArrayList does not compile
Terminology:
Attributes are the variables an object keeps. Behaviors are the methods that read or change them.
Code Runner Challenge
Label each member as an attribute or a behavior, then run it
View IPYNB Source
// CODE_RUNNER: Label each member as an attribute or a behavior, then run it
import java.util.ArrayList;
public class StudentRecord {
// Attributes: what a student record HAS
private String name;
private double gpa;
private ArrayList<String> courses;
public StudentRecord(String name, double gpa) {
this.name = name;
this.gpa = gpa;
this.courses = new ArrayList<>();
}
// Behaviors: what a student record DOES
public void enroll(String course) {
courses.add(course); // ArrayList API: boolean add(E e)
}
public void updateGPA(double newGPA) {
gpa = newGPA;
}
public boolean isHonorRoll() {
return gpa >= 3.5;
}
public void displayInfo() {
System.out.println(name + " - GPA " + gpa + " - " + courses.size() + " courses"); // int size()
System.out.println("First course: " + courses.get(0)); // E get(int index)
}
public static void main(String[] args) {
StudentRecord bob = new StudentRecord("Bob", 3.4);
bob.enroll("AP CSA");
bob.enroll("Physics");
bob.updateGPA(3.8);
bob.displayInfo();
System.out.println("Honor roll: " + bob.isHonorRoll());
}
}
StudentRecord.main(null);
Hacks & Practice Tasks
Lesson: CSA 1.7 Application Program Interface (API) and Libraries
MCQ 1.7: <paste the copied result line, such as 3/4 | answers: B,C,A,D>
Popcorn: Math.pow, Math.sqrt, and an imported ArrayList of 3 colors, prints and runs (yes/no); predicted 3 to the 4th = <p>; actual = <a>
Homework Hack 1: both hypotenuse checks print true (yes/no)
Homework Hack 2: Book attributes and behaviors labeled, all three checks print true (yes/no)
Homework Hack 3: playlist checks print true (yes/no); API entries cited = <the three entries you used>
Submission Safety Rules (Read First)
- One class per cell, ending with
ClassName.main(null);. - Label the homework hacks 1 through 3.
- Use only methods that appear in the Reference Guide or in the official Java documentation.
- Add an
importfor every class outsidejava.lang. - Run each Java cell and leave the output showing.
- Include your MCQ score.
- Use
##headings or smaller.
Popcorn Hack (In-Class)
3-minute challenge: use the API entries in the Reference Guide. Do not guess method names.
- Use
Math.powto print 3 to the 4th power - Use
Math.sqrtto print the square root of 64 - Create an
ArrayListof Strings (theimportis already there) - Add
"red","blue", and"green" - Print the list’s size with
size() - Predict the
Math.powline in the Popcorn checker under the runner before you run. When every check passes, copy its line into your submission notes
Code Runner Challenge
Complete the TODOs from the API entries, predict the pow line, then run it
View IPYNB Source
// CODE_RUNNER: Complete the TODOs from the API entries, predict the pow line, then run it
// Practice #1 - Try it yourself first!
import java.util.ArrayList;
public class UsingDocumentation {
public static void main(String[] args) {
// TODO: print "3 to the 4th: " followed by Math.pow(3, 4)
// TODO: print "Square root of 64: " followed by Math.sqrt(64)
// TODO: create an ArrayList of Strings named colors
// TODO: add "red", "blue", and "green" to colors
// TODO: print "Colors: " followed by colors.size()
}
}
UsingDocumentation.main(null);
Before you press Run, predict this output. Check the return type in the API entry.
4. MCQ Check
4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your submission notes.
5. Homework Hack
Task: Do all three hacks. Each starter compiles and runs, so you can run the initial version and compare its incorrect output with the expected output. Change only what the TODO asks for, and keep going until every check prints true.
Solution Skeleton:
Hack 1: Use the Math API (5 minutes)
Calling library methods. The hypotenuse of a right triangle is the square root of a squared plus b squared. Write hypotenuse using only Math.pow and Math.sqrt from the Reference Guide.
Code Runner Challenge
Write hypotenuse with Math.pow and Math.sqrt so both checks print true.
View IPYNB Source
// CODE_RUNNER: Write hypotenuse with Math.pow and Math.sqrt so both checks print true.
public class MathApiHack {
// TODO: return the square root of (a squared + b squared), using Math.pow and Math.sqrt
public static double hypotenuse(double a, double b) {
return 0.0;
}
public static void main(String[] args) {
System.out.println("3, 4 -> " + hypotenuse(3, 4));
System.out.println(hypotenuse(3, 4) == 5.0);
System.out.println(hypotenuse(5, 12) == 13.0);
}
}
MathApiHack.main(null);
Hack 2: Attributes and Behaviors (5 minutes)
Designing a class. Book should have three attributes (title, author, pages) and these behaviors: a constructor that sets all three, getTitle(), isLong() (true when pages is more than 300), and displayInfo(). Finish the TODOs, and put an // attribute or // behavior comment on each member.
Code Runner Challenge
Finish Book and label each member so all three checks print true.
View IPYNB Source
// CODE_RUNNER: Finish Book and label each member so all three checks print true.
public class BookHack {
static class Book {
private String title;
private String author;
// TODO: add the third attribute, pages
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
// TODO: store pages
}
public String getTitle() {
return title;
}
// TODO: return true when pages is more than 300
public boolean isLong() {
return false;
}
public void displayInfo() {
System.out.println(title + " by " + author); // TODO: also print the pages
}
}
public static void main(String[] args) {
Book big = new Book("Java Basics", "John Doe", 350);
Book small = new Book("Quick Start", "Ana Ruiz", 120);
big.displayInfo();
System.out.println(big.getTitle().equals("Java Basics"));
System.out.println(big.isLong() == true);
System.out.println(small.isLong() == false);
}
}
BookHack.main(null);
Hack 3: Follow the Documentation (10 minutes)
Combined. These ArrayList API entries are new to you. Use them to turn the playlist [Intro, Verse, Chorus] into [Opening, Verse, Bridge]. Above each call, write a comment citing the API entry you used.
| API Entry | What It Does |
|---|---|
void add(int index, E element) |
Inserts element at index and shifts later items right |
E set(int index, E element) |
Replaces the item at index |
E remove(int index) |
Removes the item at index and shifts later items left |
Code Runner Challenge
Use add(index, element), set, and remove so both checks print true.
View IPYNB Source
// CODE_RUNNER: Use add(index, element), set, and remove so both checks print true.
import java.util.ArrayList;
public class PlaylistHack {
public static void main(String[] args) {
ArrayList<String> playlist = new ArrayList<>();
playlist.add("Intro");
playlist.add("Verse");
playlist.add("Chorus");
// TODO 1: replace "Intro" with "Opening" (cite the API entry in a comment)
// TODO 2: insert "Bridge" at index 2, before "Chorus" (cite the API entry)
// TODO 3: remove "Chorus", which is now the last item (cite the API entry)
System.out.println(playlist);
System.out.println(playlist.toString().equals("[Opening, Verse, Bridge]"));
System.out.println(playlist.size() == 3);
}
}
PlaylistHack.main(null);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | Math.pow and Math.sqrt print their results, an ArrayList of three colors prints its size, and the cell runs. The Popcorn checker line is in the notes. |
| MCQ | 0.2 | 4 correct. 0.15 for 2 or 3, 0.1 if every question was answered. |
| Homework Hack 1 | 0.15 | hypotenuse uses only Math.pow and Math.sqrt, and both checks print true. |
| Homework Hack 2 | 0.25 | pages is stored, isLong and displayInfo work, every member is labeled attribute or behavior, and all three checks print true. |
| Homework Hack 3 | 0.2 | The playlist changes using set, add(index, element), and remove, each with a comment citing its API entry, and both checks print true. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - Popcorn checker line in the notes.
- MCQ score in the notes.
- An
importfor every class outsidejava.lang. - No hand-written code that repeats what a library method already does.
- Every homework check prints
true.
6. LxD Cycle Process
Empathize: Students write their own sorting, text, or random-number code from scratch, even though Java already has tested classes that do it. They treat API documentation as optional reading instead of as the main way to learn what a class can do, and they use “library,” “API,” and “package” as if they meant the same thing.
Define:
- POV: CSA students need to be comfortable reading API documentation, because real Java work and the AP exam’s provided classes assume you can use a class without reading its source code.
- Learning Goal: Students will call library methods from their API entries, explain how libraries, APIs, and packages relate, and tell a class’s attributes from its behaviors.
Ideate:
- HMW Question: How might we teach students to trust and read documentation instead of rewriting what already exists?
- HMW Question: How might we make the return type in an API entry something students check before they call a method?
- Activity: Read API entries like a menu, predict a call’s output from its return type, then use new entries to finish a class and edit a list.
Prototype:
- A reference guide with an API-entry table, runnable library and attributes-and-behaviors examples, a documentation Popcorn Hack with a checker, an MCQ knowledge check, three scaffolded homework hacks with self-checking output, and a grading rubric.
- A trial run showed students mixing up “API,” “library,” and “package,” so the three terms sit side by side in the Reference Guide, next to the attributes and behaviors table.
- Excellence means calling an unfamiliar method correctly from its documentation on the first try and citing the entry used.
Test:
- Ask peers in peer review to complete the practice without additional explanation.
- Observe whether peers look up the API entry before calling a method, and whether they predict
81.0rather than81forMath.pow. - Compare your lesson with another that is posted.
- Use the findings to revise any instruction or rubric criterion that did not guide students clearly.
- On submission, collect evidence from runner output, MCQ results, AI grading and student explanations.
- After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.
7. References/Citations
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 [Effective fall 2025]. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf
Oracle. (2023). Java Platform, Standard Edition 21 API specification. https://docs.oracle.com/en/java/javase/21/docs/api/index.html
Submit Assignment
Need to update a submission later? Open the submissions dashboard.