πŸ““1.7: APIs & Libraries

Table of Contents


πŸ“– This page is a condensed version of CSAwesome Topic 1.7


We have already been using System.out.println() to print text to the screen. That method belongs to the Java API – the Application Programming Interface for Java’s standard libraries and packages.

APIs, Libraries, Packages

πŸ“š Library
A collection of prewritten code (classes) that you can reuse in your own program.
πŸ“¦ Package
A group of related classes that can be imported into a program, like a folder of classes in a library's file directory.
πŸ‘‰ Method
A block of code that performs a specific task, like instructions for a process. Classes often contain many methods.
πŸ–₯️ API
Application Programming Interface refers mainly to a library's documentation that tells the programmer how to use its classes and their methods.

APIs and libraries are essential to programming because they allow you to use code that has already been written by others. This saves you time and allows you to focus on the specific task you are trying to accomplish.

The terms library, API, and package are often used interchangeably to mean similar things.

πŸ’¬ Discuss: Did we use any libraries last year in Web Development for either HTML, CSS, or JavaScript? If so, how did we learn how to use someone else’s code?

The java.lang Package

The java.lang package contains built-in classes and interfaces that are fundamental to the Java programming language, such as the String class and the System class which we use in System.out.println.

Take a look at the summary for the java.lang package here: Oracle – Java Documentation.

Can you find the System class in the documentation?

πŸ“– Documentation found in API specifications and libraries is essential to understanding the attributes and behaviors of a class defined by the API.

The System class has an object called out that is type PrintStream, and the PrintStream class has a method called println() that we use to print to the screen.

Check Your Understanding:

  1. How many distinct println(...) methods are listed in the PrintStream class official documentation?
  2. Try finding the same documentation on DevDocs, an unofficial collection of documentation (linked in the top right corner of this website).

    Would you prefer using the official docs from Oracle, or DevDocs?

  3. Try asking an AI bot like ChatGPT the original question (β€œhow many println methods does Java have?”) and determine if it produces a correct answer.

Turtle Library

🐒 The Turtle library (by Dr. Barbara Ericson) lets you draw with an animated turtle that moves, turns, and draws lines. A class like Turtle defines the data (attributes/fields) and behaviors/methods shared by all turtle-type objects you create.

The Turtle class is not tested on the AP Exam, but working with its visual interface is helpful when learning about object-oriented programming concepts.

Attributes/Fields
Data the object β€œknows” (e.g., name, height, width, position
Behaviors/Methods
Actions the object β€œdoes” (e.g., forward(), turnLeft()) or what can be done to it.

We will learn more about classes, objects, attributes, and methods in the next lessons, but this lesson introduces them to explore APIs.

image-small

This diagram of a turtle shows some of the generic Turtle attributes like name, width, height, color in the body of the turtle and its methods like forward(), backward(), written around the turtle.

πŸ“£ You CALL (activate/run) methods with the dot operator (.), just like System.out.println():

myrtle.forward();
myrtle.turnRight();

Assume myrtle is a Turtle object. The code above calls two methods on that turtle: first, forward() then turnRight(). These methods β€œbelong” to the general Turtle class definition, but are being used on that specific object (an instance of the class).

Reading API Documentation

A quick tip to tell methods vs attributes in docs:

  • Methods always have parentheses: forward(), println(...).
    • Sometimes these are empty, but always necessary.
    • And sometimes they contain data that the method needs to do its job (for example: what to print).
  • Attributes/fields do not: out, length, width.

Explore the docs for a simple Turtle: πŸ“– SimpleTurtle Documentation

CSAwesome Activities: Turtle Class

🐒 To explore the concepts from this lesson in code, we’ll play around with the Turtle class on the CSAwesome website instead of taking notes.

πŸ‘‰ GO TO: , SIGN IN to your account, and complete all the turtle-related coding activities/challenges with a partner.


Summary

  • (AP 1.7.A.1) Libraries are collections of classes written by other programmers.

  • (AP 1.7.A.1) An Application Programming Interface (API) specification informs the programmer how to use classes in a library.

  • (AP 1.7.A.1) Documentation found in API specifications and libraries is essential to understanding the attributes and behaviors of a class defined by the API.

  • (AP 1.7.A.1) Classes in the APIs and libraries are grouped into packages that can be imported into a program.

  • (AP 1.7.A.1) A class defines a specific reference type and is the building block of object-oriented programming. Existing classes and class libraries can be utilized to create objects.

  • (AP 1.7.A.2) Attributes refer to the data related to the class and are stored in variables.

  • (AP 1.7.A.2) Behaviors refer to what instances of the class can do (or what can be done with it) and are defined by methods.


Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.