Java is an object-oriented programming language. That means that one of the primary ways of designing and organizing a Java program is in terms of objects. Objects combine data and the code that operates on that data into a single unit. To create objects, we first define a class which provides a blueprint for creating the objects. In Java, all programs are built out of classes. This is why, every Java program starts with public class. In this unit, you will learn the vocabulary of object-oriented programming and to create and use objects of a class written for you. In later units, you will learn to write your own classes.
You can think of a class as something like a blueprint of a house that is used to construct houses (objects), or like a cookie cutter that is used to create the cookies (objects). The cookie cutter (class) can be used to create as many cookies (objects) as you want. The cookies (objects) are all the same shape and size, but they can have different colors and decorations.
You can also think of a class as defining a new data type. In this lesson, we will use the class Turtle to make animated turtle objects. Just like you use int to declare variables that hold numbers, you can use Turtle to declare many variables, animated turtle objects, who are instances of the Turtle class.
A class defines the attributes (data) and behaviors (methods) that all objects of that class will have. The objects are the specific instances of the class that have their own values for the attributes. Attributes are the data or properties that an object knows about itself, for example, a turtle objectβs color and size. Behaviors are the things that an object can do. For example, a turtle object can go forward, turn left, or turn right. The attributes and behaviors of a class are defined in the class, but each object has its own values for the attributes.
shows another class called Belt and how it has 3 instance variables to define its attributes. Every belt object can have different values stored in the instance variables.
The following picture has lots of cats (objects of the type cat). They are all different, but they share the same attributes and behaviors that make up a cat. They are all instances of cat with different values for their attributes. Name some of the attributes and behaviors of the cats below. For example, the color ( attribute) of the first cat is black ( attribute value) and it is playing ( behavior).
Discuss with your class: What are some attributes of cats? What are some behaviors of cats? (Note that attributes are often nouns or adjectives describing features of cats, and behaviors are often verbs).
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.
The Turtle class (that weβve written for you and hidden on this page) is a blueprint for turtle objects. It defines attributes for graphical turtles like their color and position and methods to make the turtles move. Try the Java program below that creates a Turtle object called yertle using the Turtle class. If the code below does not work or is too slow in your browser, you can also see the Turtle code in action on JuiceMindβ3β
The dot operator (.) is used to run an objectβs method. You can think of the (.) as asking the object to do something (execute one of its methods). For example, yertle.forward() asks the turtle yertle to go forward. It doesnβt tell yertle how much to go forward, so it goes forward 100 pixels by default. The parentheses () after a method name are there in case you need to give the method arguments (some data) to do its job, for example to go forward 50 pixels instead of 100 in yertle.forward(50); Try changing the code above to go forward 50 pixels instead and then run it again.
A variable like yertle of a reference type like Turtle holds an object reference, which can be thought of as the memory address of that object. Variables that are of a primitive types like int hold just a single number value. But objects are complex and contain a collection of values called attributes, for example the color and the size of the turtle.
When you write a class like the Turtle class, you can create many objects of that class type. In the code below, two turtle objects are created: yertle and myrtle. You can name your turtle and add in a line like the following in the main method to make it move:
// To create or declare a new object, write:
// ClassName variableName = new ClassName(arguments);
Turtle yourTurtleName = new Turtle(habitat);
yourTurtlename.forward();
Activity1.12.8.
Can you add another turtle object to the code below?
Another important concept in object-oriented programming is inheritance. Although the AP CSA exam no longer covers inheritance, you should know what it means. Inheritance is a way to create a new class that is based on an existing class. The new class, called a subclass, inherits the attributes and behaviors of the existing class, called a superclass. In Java, all classes are subclasses of a superclass called Object.
For example, you could have a superclass called Pet with attributes like name and age and behaviors like eat and sleep. You could then have subclasses like Dog and Cat and Turtle that inherit the attributes and behaviors of the Pet class. Each subclass could add attributes and behaviors specific to that type of pet. For example, a Dog subclass could have an attribute called breed and a behavior called bark.
Letβs practice more with the Turtle class and its methods. Here is a class diagram that shows some of the attributes and methods in the class Turtle.
The Turtle world does not use the Cartesian coordinate system with (0,0) in the middle the screen. Instead, (0,0) is at the top left corner of the screen and x increases to the right and y increases towards the bottom of the screen. Most computer graphics systems use this coordinate system which is a carry over from before computers could display graphics and had to approximate the graphics with text print outs.
The following code uses a turtle to draw the digital number 7 (with just straight lines), but the code is mixed up. Drag the code blocks to the right and put them in the correct order to first draw the line going up (towards the top of the page) and then turn and draw a line to the left to make a 7. Remember that the turtle is facing the top of the page when it is first created. Click on the βCheck Meβ button to check your solution.
public class Draw7
{
---
public static void main(String[] args)
{
---
World habitat = new World(300,300);
---
Turtle yertle = new Turtle(habitat);
---
yertle.forward();
---
yertle.turnLeft();
yertle.forward();
---
habitat.show(true);
---
} // end main
---
} // end class
Working in pairs, use the area below to have your turtle draw simple block-style letters for your first or last name initials using just straight lines (no curves or diagonals).
It may help to act out the code pretending you are the turtle. Remember that which way you turn depends on which direction you are facing, and the turtle begins facing north (towards the top of the page).
You may notice that it is challenging to have your turtle draw with these simple methods. In the next lessons, we will use more complex Turtle methods where you can indicate how many steps to take or what angle to turn that will make drawing a lot easier!
Have your turtle create a block drawing of the initials of your name. Use straight lines (no curves or diagonals unless you want to try adding arguments to the turtle methods).
(AP 1.12.A.1) A class defines a new data type (a classification). It is the formal implementation, or blueprint, of the attributes and behaviors of the objects of that class.
(AP 1.12.A.2) A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without replacing these in the code. This creates an inheritance relationship from the subclasses to the superclass. Designing and implementing inheritance relationships are outside the scope of the AP Computer Science A course and exam.