AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/1/1-7)
Unit 1 · Topic 1.7
1.7 Application Program Interface (API) and Libraries
Programmers rarely build everything from scratch. They use libraries of classes other people have written, and they learn how to use those classes by reading the documentation. This topic is about reading a class's documentation to find what its objects know and what they can do.
Key terms
- library
- API
- package
- class
- attribute
- behavior
Libraries, packages and APIs
A library is a collection of classes that are ready to use. Java comes with a huge standard library, with classes for text, math, lists, files and much more.
Classes in a library are grouped into packages, which are like folders of related classes. A few you'll meet this year:
java.lang: basic classes such asString,Math,IntegerandDouble. These are available automatically.java.util: utility classes such asArrayListandScanner. You need an import statement to use them.java.io: classes for files, such asFileandIOException. These need an import too.
Import statements
An import statement at the top of the file tells Java which library class you mean:
import java.util.ArrayList;
public class Roster
{
private ArrayList<String> names = new ArrayList<String>();
}
Without the import, the compiler doesn't know what ArrayList is and reports an error.
An API (application programming interface) specification is the documentation for a library. It lists each class's constructors and methods, what they need and what they do. The Java Quick Reference you get on the exam is a small API: it lists exactly the library methods the exam can use.
Classes, attributes and behaviors
Each class defines a new reference type. Once a class exists, you can create objects of that type, even if you never see the code inside it.
A class describes two things about its objects:
- Attributes: the data each object stores, kept in variables. A
Playlistmight store its name and its songs. - Behaviors: what an object can do, or what you can do with it, written as methods. A
Playlistmight let you add a song or ask for its length.
Reading a method listing
Documentation describes each method with a header such as int getLength() or void addSong(String title). Read it left to right: the return type (what you get back, or void for nothing), the name, then the parameters in parentheses (what you must pass in, and their types).
For example, the Quick Reference lists int length() for String. That tells you length takes no arguments and gives back an int, so int n = word.length(); is a correct way to use it.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Using a class from its documentation
A
Playlistclass is documented like this: a constructorPlaylist(String name);void addSong(String title), which adds a song;int getLength(), which returns the number of songs; andString getName(), which returns the name. Name the attributes and behaviors, then work out what this code prints.Playlist mix = new Playlist("Road Trip"); mix.addSong("Highway Song"); mix.addSong("Open Road"); System.out.println(mix.getName() + ": " + mix.getLength());Show the solutionHide the solution
- Step 1: Attributes are the data a playlist keeps track of: its name and its list of songs. You can tell from what the methods return and change.
- Step 2: Behaviors are the methods:
addSong,getLengthandgetName. - Step 3: Line 1 creates a playlist named Road Trip. Lines 2 and 3 add two songs.
- Step 4: Line 4 calls
getName(), which returns"Road Trip", andgetLength(), which returns 2. They're joined with a colon and a space. - Step 5: You never saw how
Playliststores its songs, and you didn't need to. The documentation was enough.
Answer: Attributes: the name and the songs. Behaviors:
addSong,getLengthandgetName. The code printsRoad Trip: 2.
Common mistakes
- Mixing up attributes and behaviors. Attributes are data (nouns, stored in variables); behaviors are actions (verbs, written as methods).
- Forgetting the import for
ArrayList,ScannerorFile. Classes injava.lang, likeStringandMath, don't need one. - Ignoring the return type in documentation. A
voidmethod gives nothing back, so you can't store its result in a variable.
On the exam
- Free-response questions describe the classes you need in the same style as API documentation. Read every method header carefully and call methods exactly as documented, with the right number and types of arguments.
- Keep the Java Quick Reference open during the exam. If a method isn't on it or in the question, you probably don't need it.
Connected topics
Videos
Check yourself
3 questions on 1.7 Application Program Interface (API) and Libraries. Pick an answer to see if you got it, and why.
A programmer is designing a PlaylistSong class for a music app. Which of the following is best modeled as a behavior (a method) rather than an attribute?
A student wants to use a class from a Java library that she didn't write. Which of the following is the main thing its API documentation tells her?
Which of the following statements about Java libraries is true?
0 of 3 answered