Skip to main content
Logo image

Section 1.7 APIs and Libraries

We have been using the System.out.println() method to print to the screen. This method is part of the Java API (Application Programming Interface). A method is a block of code that performs a specific task. APIs are connected to libraries which are collections of classes written by other programmers that provide a set of methods that can be used to perform specific tasks. An API specification tells the programmer how to use those classes with methods in a library.
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.
Classes in the APIs and libraries are grouped into packages. A package is a collection of related classes and interfaces (which are similar to classes) that can be imported into a program to be used. A package is like a folder of classes in a library’s file directory and is used to avoid name conflicts. Later on, we will learn to import other Java packages to use in our code, but there is a package that is already built into Java called java.lang that we are already using.
The terms library, API, and package are often used interchangeably to mean similar things. A library is a collection of classes or code written by other programmers that you can use. An API describes how you use the library. And a package is how Java organizes a library to be used.

Subsection 1.7.1 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 documentation for the java.lang package at https://docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html
 1 
https://docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html
. 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. How many println() methods are there in the PrintStream class documented at https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html
 2 
https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html
? One for each type that it can print!

Activity 1.7.1.

Activity 1.7.2.

Activity 1.7.3.

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 are used to use code written by others.
  • To create programming languages
  • Incorrect. APIs and libraries are not for creating programming languages.
  • To compile code
  • Incorrect. APIs and libraries are not for compiling code.

Subsection 1.7.2 Turtle Library

The Turtle Java library, written by one of the author’s of this book, Dr. Barbara Ericson, allows you to create drawings using an animated turtle that moves around the screen. The turtle can move forward, turn, and draw lines. The turtle library is a collection of classes that can be imported into your code and is built in to this e-book.
Classes are the building blocks of Java and object-oriented programming. A class defines a specific reference type. Programmers often create new classes like Turtle so that it can be used as a new data type. Classes like Turtle and class libraries can be utilized to create objects like a particular turtle named yertle. A class defines the data and behavior for all objects of that type.
The following picture of a turtle shows some of the Turtle attributes like name, width, height, color in the body of the turtle and its methods like forward(), backward(), written around the turtle.
Figure 1.7.1. Turtle Attributes and Behaviors
Attributes (which are sometimes called fields in documentation) refer to the data related to the class and are stored in variables. Behaviors refer to what objects of the class can do (or what can be done with it) and are defined by methods. The Turtle class has attributes like name, height, and width that store data about the turtle, and it has behaviors/methods like forward() and turnLeft() that allow the turtle to move and turn. We will learn more about classes, objects, attributes, and methods in the next lessons, but this lesson introduces them to explore APIs.
How can you tell attributes and methods apart when you look at library documentation? One easy way is that methods will always have parentheses after them, for example forward() or println(). Sometimes these are empty, but still necessary, and sometimes they contain data that the method needs to do its job, for example what to print. Attributes are variables and will not have parentheses after them. Here’s the API documentation that lists all of the Turtle methods (but not the attributes): https://www2.cs.uic.edu/~i101/doc/SimpleTurtle.html
 4 
https://www2.cs.uic.edu/~i101/doc/SimpleTurtle.html
.
Try the Java program below that creates a Turtle object called yertle using the Turtle class. The following code is used to tell the turtle to move forward and then turn right. The dot operator (.) is used to run an object’s methods, just like in the System.out.println() method. (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
 5 
https://replit.com/@BerylHoffman/Java-Swing-Turtle#Main.java
(refresh page after forking and if it gets stuck) or download the files here
 6 
https://github.com/bhoffman0/CSAwesome2/raw/main/_sources/Unit1-Using-Objects-and-Methods/TurtleJavaSwingCode.zip
to use in your own IDE.)
yertle.forward();
yertle.turnRight();

Activity 1.7.4.

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.

Activity 1.7.5.

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?
  • East
  • Which way does yertle first move in the example above?
  • West
  • Which way does yertle first move in the example above?

Activity 1.7.6.

What type of thing is Turtle in the program above?
  • class
  • Yes, Turtle is a class that defines the data and behaviors for all turtles.
  • object
  • yertle is an object of type Turtle.
  • attribute
  • An attribute is something the object knows about itself.
  • method
  • A method is something an object can do like go forward.

Activity 1.7.7.

What type of thing is turnRight in the program above?
  • object
  • An object has data and behavior.
  • class
  • A class defines the data and behavior for all objects of that type.
  • attribute
  • An attribute is something the object knows about itself.
  • method
  • A behavior or method is something an object can do like turn right.

Activity 1.7.8.

What type of thing is the position of a turtle in a world?
  • object
  • An object has data and behavior.
  • class
  • A class defines the data and behavior for all objects of that type.
  • attribute
  • An attribute is something the object knows about itself like its position.
  • method
  • A method is something an object can do like turn right.

Activity 1.7.9.

What are attributes of a class?
  • Methods that perform tasks
  • Incorrect. Attributes are not methods.
  • Data related to the class stored in variables
  • 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 1.7.10.

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 1.7.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 check the API at https://www2.cs.uic.edu/~i101/doc/SimpleTurtle.html
 7 
https://www2.cs.uic.edu/~i101/doc/SimpleTurtle.html
:
  • yertle.forward();
  • yertle.turnLeft();
  • yertle.turnRight();
  • yertle.backward();
  • yertle.penUp();
  • yertle.penDown();

Project 1.7.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 1.7.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 it) and are defined by methods.
You have attempted of activities on this page.