Topic 1.7: Application Program Interface (API) and Libraries

Link to Homework

College Board Requirements

Essential Knowledge 1.7.A.1

  • Libraries = collections of classes
  • API = tells you how to use classes
  • Packages = organize classes
  • Documentation = explains attributes and behaviors

Essential Knowledge 1.7.A.2

  • Attributes = data (variables)
  • Behaviors = actions (methods)

1. Libraries = Collections of Classes

A library is a collection of pre-written classes that you can use in your programs.

Think of it like a toolbox:

  • Instead of building every tool from scratch, you use tools that already exist
  • Java provides many libraries with useful classes
  • You can create objects from these classes to solve problems

Example: Java’s standard library includes:

  • String class - for working with text
  • ArrayList class - for storing lists of items
  • Math class - for mathematical operations
  • Random class - for generating random numbers
// Using classes from Java's library

public class 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);
    }
}

LibraryDemo.main(null);
Hello, CSA!
Square root of 16: 4.0

2. API = Tells You How to Use Classes

An API (Application Programming Interface) is like an instruction manual.

Restaurant Analogy:

  • Menu (API) shows what dishes are available
  • Menu describes each dish
  • You order from the menu without knowing how to cook

In Java:

  • API documentation shows what classes are available
  • API describes methods you can call
  • You use classes without knowing how they work internally

Example: String API tells us:

  • length() - returns number of characters
  • toUpperCase() - converts to uppercase
  • substring(start, end) - extracts part of string
// Using methods from String API

public class 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));
    }
}

APIDemo.main(null);
Original: Computer Science
Length: 16
Uppercase: COMPUTER SCIENCE
First 8 chars: Computer

3. Packages = Organize Classes

Packages organize classes into groups (like folders organize files).

Common Java Packages:

Package Description Example Classes
java.lang Fundamental classes String, Math, Integer
java.util Utility classes ArrayList, Scanner, Random
java.io Input/Output File, FileReader

Important Rule:

  • java.lang classes don’t need import statements
  • All other packages need import statements
// Demonstrating packages

import java.util.ArrayList;  // Need import for java.util

public class 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);
    }
}

PackageDemo.main(null);
String: Java
Absolute value: 5
ArrayList: [Item 1]

4. Documentation = Explains Attributes and Behaviors

API Documentation is essential for understanding classes.

What Documentation Shows:

  1. Constructors - How to create objects
  2. Methods - What the class can do
  3. Parameters - What information methods need
  4. Return types - What methods give back

Example: ArrayList Documentation

Common constructors:

  • ArrayList() - creates empty list

Common methods:

  • add(element) - adds to end
  • get(index) - returns element at position
  • size() - returns number of elements
  • remove(index) - removes element
// Using ArrayList based on documentation

import java.util.ArrayList;

public class 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()
    }
}

DocumentationDemo.main(null);
Scores: [95, 87, 92]
First score: 95
Total scores: 3

🍿 Popcorn Hack #1: Using Documentation (5 minutes)

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
// Popcorn Hack #1: Complete this code

import java.util.ArrayList;

public class 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
        
    }
}

// PopcornHack1.main(null);

5. Attributes = Data (Variables)

Attributes are what an object HAS (its data/state).

Key Points:

  • Attributes are variables inside a class
  • They store data about the object
  • Usually declared as private

Example: Student Class

A Student has:

  • name (String)
  • grade (int)
  • gpa (double)
// Demonstrating Attributes

public class Student {
    // ATTRIBUTES - what the student HAS
    private String name;
    private int grade;
    private double gpa;
    
    // Constructor to set attributes
    public Student(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);
    }
}

// Create a student object
Student alice = new Student("Alice", 11, 3.8);
alice.showInfo();
Name: Alice
Grade: 11
GPA: 3.8

6. Behaviors = Actions (Methods)

Behaviors are what an object DOES (its actions).

Key Points:

  • Behaviors are methods inside a class
  • They perform actions or calculations
  • They can modify attributes or return values

Example: Student Class Behaviors

A Student can:

  • displayInfo() - show student data
  • updateGPA() - change GPA
  • promoteGrade() - move to next grade
  • isHonorRoll() - check if GPA >= 3.5

Attributes vs. Behaviors Summary:

Concept Definition Example
Attributes What object HAS name, grade, gpa
Behaviors What object DOES displayInfo(), updateGPA()
// Demonstrating Behaviors

public class Student {
    // Attributes
    private String name;
    private int grade;
    private double gpa;
    
    public Student(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;
    }
}

// Testing behaviors
Student bob = new Student("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

🍿 Popcorn Hack #2: Attributes and Behaviors (5 minutes)

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.

// Popcorn Hack #2: Complete the Book class

public class Book {
    // TODO: Add 3 attributes (title, author, pages)
    
    // TODO: Add constructor
    
    // TODO: Add displayInfo() method
    
    // TODO: Add isLong() method (returns true if pages > 300)
    
}

// TODO: Create a Book object and test all methods
// Example: Book myBook = new Book("Java Basics", "John Doe", 350);


Summary: College Board Essentials

1.7.A.1 - Libraries and APIs

Libraries = collections of classes you can use
API = tells you how to use those classes
Packages = organize classes into groups
Documentation = explains attributes and behaviors

1.7.A.2 - Attributes vs. Behaviors

Attributes = data (variables) - what object HAS
Behaviors = actions (methods) - what object DOES

Key Reminders:

  • java.lang classes don’t need imports (String, Math, Integer)
  • Other packages need import statements (ArrayList, Scanner, Random)
  • Always check API documentation when using new classes
  • Use existing library classes instead of writing code from scratch!

Resources