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.
Subsection3.2.1The 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.
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.
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.
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.)
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.
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.
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.
public class TurtleTest {
---
public static void main(String[] args) {
---
World habitat = new World(300,300);
Turtle yertle = new Turtle(habitat);
---
yertle.turnRight();
---
yertle.right(); #paired
---
yertle.forward();
---
yertle.forward() #paired
---
yertle.turnLeft();
---
yertle.forward(50);
---
habitat.show(true);
---
} // end main
} // end class
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.
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.
(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) 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.