Application Program Interface (API) and Libraries
Understanding Application Program Interfaces and Java Libraries
- Topic 1.7: Application Program Interface (API) and Libraries
- College Board Requirements
- 1. Libraries = Collections of Classes
- 2. API = Tells You How to Use Classes
- 3. Packages = Organize Classes
- 4. Documentation = Explains Attributes and Behaviors
- 🍿 Popcorn Hack #1: Using Documentation (5 minutes)
- 5. Attributes = Data (Variables)
- 6. Behaviors = Actions (Methods)
- 🍿 Popcorn Hack #2: Attributes and Behaviors (5 minutes)
- Summary: College Board Essentials
- Resources
Topic 1.7: Application Program Interface (API) and Libraries
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:
Stringclass - for working with textArrayListclass - for storing lists of itemsMathclass - for mathematical operationsRandomclass - 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 characterstoUpperCase()- converts to uppercasesubstring(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.langclasses 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:
- Constructors - How to create objects
- Methods - What the class can do
- Parameters - What information methods need
- Return types - What methods give back
Example: ArrayList Documentation
Common constructors:
ArrayList()- creates empty list
Common methods:
add(element)- adds to endget(index)- returns element at positionsize()- returns number of elementsremove(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:
- 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
// 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 dataupdateGPA()- change GPApromoteGrade()- move to next gradeisHonorRoll()- 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):
- Constructor to set all attributes
displayInfo()- print all book infoisLong()- 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.langclasses don’t need imports (String, Math, Integer)- Other packages need
importstatements (ArrayList, Scanner, Random) - Always check API documentation when using new classes
- Use existing library classes instead of writing code from scratch!