Skip to main content
Logo image

Section 3.2 APIs and Libraries

Earlier in this book, we used code like System.out.println("hello, world!") to print to the screen. When we include that code in a program, we don’t have to worry about exactly how it works; we just need to know what it does. Namely, it causes the text β€œhello, world!” to appear on the screen.
A collection of code written by other programmers that we can use is called a library and the details of how to use a library are described in its Application Programming Interface, or API for short. System.out.println() is part of the Java API, a large set of code that comes with Java.
APIs and libraries are essential to programming because they allow us to focus on the specific task we are trying to accomplish without having to rewrite the same bits of functionality over and over.
Library, API, package, documentation
Figure 3.2.1. Library and API

Subsection 3.2.1 The Turtle Library: Classes, Attributes, and Behaviors

One of the great things about Java is the number of libraries written by other programmers that we can use. One such library is the Turtle Java library, written by one of the author’s of this book, Dr. Barbara Ericson and built into this book. It allows us to create drawings using animated turtles that move around the screen. Try clicking run below to see a turtle in action.
The Turtle library is named after its main class, Turtle. Classes are the main building blocks of Java programs and libraries. We’ll discuss writing our own classes in ChapterΒ 8Β Classes, but in this unit, we just want to focus on using classes that other programmers have written.
Each class defines a new kind of value similar to how each primitive typeβ€”int, double, and booleanβ€”define a particular kind of value, such as a 32-bit integer quantity for an int or a 64-bit floating point value for a double. These new kinds of values, defined by classes, are called objects which is where object-oriented programming gets its name. The values defined by the Turtle class represent turtles that can move around the screen, drawing lines. Objects are also called instances of their class.
Classes define the structure of objects in terms of attributes and behaviors. Attributes are the data that makes up the object, each stored in a variable defined by the class. Objects can be more complex than primitive values since they can consist of an arbitrary number of attributes unlike primitive values which are always just one value. And unlike primitive values, which can only be operated on with a fixed set of operators like + and *, objects can have an arbitrary number of behaviors that describe what we can do with those objects. These behaviors are defined in the class as methods.
The following picture of a turtle shows some of the attributes every Turtle object has, such as a color, xPos, yPos, and heading as well as behaviors implemented as methods like forward(), backward(), turnLeft(), and turnRight() shown in the picture written around the turtle.
Turtle attributes and behaviors
Figure 3.2.2. Turtle Attributes and Behaviors
While the class Turtle defines what attributes each turtle has, every turtle object has its own value for each attribute. So we can have one blue turtle at position 200,100 and one red turtle at position 50,75, just like we can have one int whose value is 2 and another whose value is 10. The actual Turtle class defines several more attributes than the ones shown in the image but these are the main ones we need to know about to understand drawing with turtles.
Similarly, the behaviors defined by the Turtle class are shared among all turtle objects, but their exact effects will usually depend on the attributes of the specific turtle object the method is called on. For instance, the forward() method, as you’ll see in the next exercise, causes a turtle to move forward and draw a line. Where the line is drawn will depend on the turtle’s position and heading attributes and the color of the line will be determined by the turtle’s color. Calling forward() on a different turtle value would still draw a line but it would likely be in a different position and might be a different color. Calling a method may also change an object’s attributesβ€”after we called forward() the turtle’s position will have changed but its heading and color will stay the same.
To see an example of this, look at the the Java program in the exercise below. It creates a Turtle object and stores a reference to it in the variable yertle. The following two lines of code are used to tell the turtle to move forward and then turn right.
yertle.forward();
yertle.turnRight();
The period (.) after yertle in both those lines is called the dot operator and it allows us to access the attributes and methods of an object. In the first line the dot says get the method named forward() defined in the Turtle class and call it on the turtle object named by the variable yertle. The exact behavior of the method will depend on the current values of yertle’s attributes such as its color, its x and y position, and its heading. For instance, calling forward() will cause the turtle to draw a line from its current x and y position to a new position 100 pixels away in whatever direction the turtle is facing. The color of the line will be determined by the turtle’s color.

Activity 3.2.1.

Try clicking the run button below to see what the following program does. Then add 1 more line of code on line 16 to make yertle go forward again.
If the code below does not work or is too slow in your browser, you can also see the Turtle code in action at this replit link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.)

Activity 3.2.2.

Based on running the code in the previous exercise, which way does a turtle face when it is first created?
  • North
  • Turtles start off facing north which is toward the top of the page.
  • South
  • Which way does yertle first move in the example above?
  • Which way does yertle first move in the example above?
  • Which way does yertle first move in the example above?

Activity 3.2.3.

What does Turtle refer to in the program above?
  • class
  • Yes, Turtle is a class that defines the attributes and behaviors for all turtles.
  • object
  • There is an object here: the variable yertle holds a reference to an object of type Turtle. But Turtle is not the name for a specific object.
  • attribute
  • An attribute is one part the definition of a class like Turtle.
  • behavior
  • A behavior is something an object can do, defined as a method in the class. In the code above forward and turnRight are the names of methods defined in the class Turtle.

Activity 3.2.4.

What does turnRight refer to in the program above?
  • object
  • In the code above the variable yertle holds a reference to an object.
  • class
  • A class defines the data and behavior for all objects of that type. turnRight names just one thing defined in the class Turtle
  • attribute
  • An attribute is something the object knows about itself. The name turnRight refers to something a Turtle object can do.
  • method
  • Yes. A method describes some behavior an object can do like turning right. Code outside the class can cause that behavior to happen by calling the method.

Activity 3.2.5.

Every turtle has a position. What kind of program element stores information like a turtle’s position?
  • object
  • An object has data and behavior.
  • class
  • A class defines the data and behavior for all objects of that type.
  • attribute
  • An attribute holds information about an object such as its position.
  • method
  • A method is something an object can do like turn right.

Activity 3.2.6.

What are attributes of a class?
  • Tasks performed by methods defined in a class.
  • Incorrect. Attributes are not methods.
  • Data stored in variables defined in a class.
  • Correct! Attributes are data related to the class stored in variables.
  • The arguments of a class
  • Incorrect. Classes do not have arguments.
  • Packages that contain the class
  • Incorrect. Attributes are not packages.

Activity 3.2.7.

The following program uses a turtle to draw a sideways capital L as seen in the image, but the lines are mixed up. The program should do all necessary set-up. Then it should ask the turtle to turn right, go forward, turn left, and then go forward 50 pixels. Next, it should ask the habitat to show itself. Drag the needed blocks of statements from the left column to the right column and put them in the right order. There are 2 extra blocks that are not needed in a correct solution. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order or are the wrong blocks.

Subsection 3.2.2 Packages

While classes are the main building blocks of Java programs, most programs and libraries are made up of multiple classes that need to be used together. In Java, related classes can be grouped together into a package. You can think of a package like a folder where you store related documents. Classes that work together will likely be grouped into a single package just to keep things organized. But more importantly, when we want to use a class defined in a package in our own program, we can import it and then use it as if we had written it in our own program.
Because related classes are usually collected into the same package, Java programmers tend to use the terms library, API, and package somewhat interchangeably. But to be precise, a library is a collection of classes that you can use, the library’s API describes how you use it, and the classes in the library are probably organized into one or more Java packages.
Later on, we will learn to import classes from Java packages to use in our code, but first let’s talk about a package whose classes are always available in any Java program: java.lang.
The java.lang package contains key classes that are fundamental to the Java programming language, such as String and System. Like all packages that are part of Java, java.lang is documented in Javadocs, Java’s version of online API documentation. Learning to read and navigate Javadocs is a key part of being a Java programmer.
The Javadocs for a class will document all the publicly available attributes (called fields in Javadocs) and behaviors (called methods). How can you tell fields and methods apart when you look at library documentation? Well, for one thing, they’re in different sections. And another easy way is that methods will always have parentheses after them, for example forward() or println(). Sometimes there’s nothing between the parentheses, but they are still necessary. Other times they contain data that the method needs to do its job, for example what to print. Since attributes, a.k.a. fields, are just variables they will not have parentheses after the name.

Activity 3.2.8.

Activity 3.2.9.

Activity 3.2.10.

What is the purpose of APIs and libraries in programming?
  • To write new code from scratch
  • Incorrect. APIs and libraries allow you to use code written by others.
  • To use code written by others
  • Correct! APIs and libraries let you use code written by others.
  • To create programming languages
  • Incorrect. APIs and libraries are are defined in a programming language.
  • To compile code
  • Incorrect. APIs and libraries are not for compiling code.

Subsection 3.2.3 Coding Challenge: Turtle Drawing

Make yertle the Turtle draw a shape. For example, have it draw a square or a zigzag shape or a block letter by calling the forward method and a turn method multiple times. We encourage you to work in pairs for this challenge. In the next lessons, we will draw more complicated shapes. Here are some simple turtle methods that you could use or look at the Turtle Javadocs for the complete list.

Project 3.2.11.

Have yertle draw a shape, for example a square or a zigzag shape or a block letter by calling the forward method and a turn method multiple times.

Subsection 3.2.4 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 them) and are defined by methods.
You have attempted of activities on this page.