• Hacks & Practice Tasks
  • References
  • 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()

    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)


    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

    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.


    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
    

    Hacks & Practice Tasks

    Popcorn Hack #1: Using Documentation

    [!TIP] Look up Math.pow() and ArrayList.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:

    1. Use Math.pow() to calculate 3^4
    2. Use Math.sqrt() to find square root of 64
    3. Create an ArrayList of Strings
    4. Add 3 colors to the ArrayList
    5. 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

    [!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):

    1. Constructor to set all attributes
    2. displayInfo() - print all book info
    3. isLong() - 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:

    Link to Homework

    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.

    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.

    All Java lessons