UNIT 1
Application Program Interface (API) and Libraries
UNIT 1 - TOPIC 1.7
AP Computer Science A
Learning Objective 1.7.A
Identify the attributes and behaviors of a class found in the libraries contained in an API.
What are Libraries and APIs?
Library: A collection of pre-written classes that provide specific functionality
API (Application Programming Interface): A specification that tells programmers how to use library classes
Key Points:
- Libraries contain collections of classes
- APIs provide documentation on how to use those classes
- Classes in APIs and libraries are grouped into packages
- We can use existing classes to create objects
Classes Define Reference Types
What is a Class?
- A class defines a specific reference type
- It serves as a blueprint for creating objects
- Classes have two main components:
1. Attributes (data/variables)
2. Behaviors (methods)
2. Behaviors (methods)
Example: String Class
String message = "Hello World";
// Using String methods (behaviors)
int length = message.length();
String upper = message.toUpperCase();
Attributes vs Behaviors
| Attributes | Behaviors |
|---|---|
|
|
Car Class Example
// Attributes (what a car HAS):
String color
int year
String model
// Behaviors (what a car can DO):
start()
stop()
accelerate()
brake()
API Documentation is Essential
Documentation in API specifications and libraries is essential to understanding the attributes and behaviors of a class defined by the API.
API Documentation tells us:
- Class name and purpose - What the class represents
- Constructor information - How to create objects
- Method signatures - Available behaviors and how to use them
- Parameter requirements - What data methods need
- Return types - What methods give back
- Usage examples - How to properly use the class
Classes are Organized in Packages
What are Packages?
- Packages group related classes together
- Help organize large libraries
- Prevent naming conflicts
- Make code more manageable
Java Package Examples
java.lang.*
- String
- Integer
- Math
java.util.*
- ArrayList
- Scanner
- Random
java.io.*
- File
- FileReader
- BufferedReader
Creating Objects from Library Classes
We can utilize existing classes to create objects:
Examples of Using Library Classes
// Using Scanner class from java.util package
Scanner input = new Scanner(System.in);
// Using ArrayList class from java.util package
ArrayList names = new ArrayList();
// Using Random class from java.util package
Random generator = new Random();
// Using String class from java.lang package
String greeting = new String("Hello!");
Key Point: We don't need to write these classes ourselves - they're already provided in the Java libraries!
Unit 1.7 Summary
Essential Knowledge Recap:
1.7.A.1: Libraries are collections of classes. API specifications inform programmers how to use those classes. Classes define specific reference types and are grouped into packages.
1.7.A.2: Attributes refer to data stored in variables. Behaviors refer to what instances can do and are defined by methods.
Key Takeaways:
- APIs provide the roadmap for using library classes
- Classes have both attributes (data) and behaviors (methods)
- Documentation is essential for understanding how to use classes
- Existing classes save us time and provide tested functionality
- Packages organize related classes together
Ready for Topic 1.8!
1 / ?