explain the relationship between a class and an object
create classes with instance variables
Java is an object-oriented programming language that can be used to model objects in the real world.
We have seen that our Java programs start with public class Main.
A class is used to define a new data type or a blueprint for objects.
We have already created Scanner objects using the new keyword!
Scanner input = new Scanner(System.in);
For example, we know we have the data types for integers, decimal numbers, booleans, etc. but what if you wanted to create a new data type for personal information?
You could create a class (public class Person) that will allow you to create new Person objects!
Person p1 = new Person("Dave", "Coombs", "1 Eagles Way", "Milford", "OH", 45150);
Objects are the variables created from a class definition and they are instances of a class.
In this unit we will focus on using classes that have already been created; in Unit 5 we will learn to write our own custom classes.
Analogy: you can think of a class like a blueprint or a cookie cutter.
The cookie cutter (class) is used to create cookies (objects) and can be used to make as many cookies as you want!
You can also think of a class as the type or classification.
Each type can have attributes (the object's properties or what it knows about itself) and behaviors (what an object does).
In a class file, these attributes are written as instance variables and the behaviors are written as methods.
The following picture has lots of cats (objects of type cat):
They are all different, but they share the same attributes and behaviors that make up a cat.
They are all instances of a cat with different values for their attributes.
Cat cat1 = new Cat("Shadow", "Black", "playing");
Cat cat2 = new Cat("Dash", "Orange", "running");
Cat cat3 = new Cat("Charlie", "Beige", "standing");
Cat cat4 = new Cat("Ellie", "Gray", "eating");
Cat cat5 = new Cat("Floof", "Tortoise Shell", "sleeping");
Notice that the cats have different attributes (names and colors) and behaviors which would be methods.
INTRO TO OBJECTS WITH TURTLES
The Turtle class is a blueprint for turtle objects.
The class defines attributes for graphical turtles like their color and position and methods to make the turtles move!
Lets run the following program and see what it does!
IF YOU ARE INTERESTED in using an actual IDE (Eclipse, intellij, Dr. Java, etc.) to play around with the turtles, here is a link that will allow you to download the .java files. If you use an actual laptop and get these working, executing your code will be MUCH faster!
You have to download the .zip file and extract the different class files. All of the files need added to the program in your IDE for this to work...
Notice that the dot operator (.) is used to run an object's method.
You can think of the dot operator as asking the object to do something (execute one of its methods).
For example, t.forward(); asks the turtle t to go forward.
It doesn't tell t 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, t.forward(50); would ask the turtle to go forward 50 pixels instead of 100.
CREATING TURTLE OBJECTS
Since the Turtle class has been created for use, we can create many objects of that class type!
To create or declare a new object using the new keyword the format will be:
ClassName variableName = new ClassName(arguments);
For example:
Scanner scan = new Scanner(System.in);
Turtle t2 = new Turtle(world);
Now that we have another turtle, we can tell it to move using the methods!
If you click through the different files in the Java Swing Turtle program above you will notice that there are a lot of attributes and methods associated with the class Turtle (found in the SimpleTurtle.java file).
Here are some of the important ones:
SUMMARY
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.
An object is a specific instance of a class with defined attributes. Objects are declared as variables of a class type.
An attribute or instance variable is the data the object knows about itself. For example a turtle object knows the direction it is facing or its color.
A behavior or method is something that an object can do. For example a turtle object can go forward 100 pixels.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.