Instances of Classes
What are Objects and Classes?: 2.1.1
- Java is object oriented
- Can be used to model objects in the real world
- Class is used to define a new data type or a blueprint for objects
- This is why java files start with
public class
- “blueprint/cookie cutter”
- A type/classification
- has attributes
- properties
- “what the object knows”
- instance variables
- has behaviors
- “what the object does”
- methods
- has attributes
- This is why java files start with
- Object are variables created from a
class
definition- Instances of a class
String
is a class- “Cookies” (cut from a cookie cutter)
- Use a class to create as many objects as you want
Video
- class is a blueprint for an object
- “tells you how to make objects”
- Can make multiple objects with the same class
- Called instances
- Has attributes and methods
- instances can differ
- has different attributes or instance variables
dog1
has blue fur,dog2
has purple fur
- has different attributes or instance variables
Intro to Objects with Turtles: 2.1.2
- Turtle class was written by CS A
- Defines attributes for graphical turtles
- Color
- Position
- Has methods to move
- Defines attributes for graphical turtles
Video
import java.util.*;
import java.awt.*;
public class TurtleTest
{
public static void main(String[] args)
// Main method; runs program
{
World habitat = new World(300,300);
// Declares variable of type world; calls World constructor to make new world
// keeps track of new world created
Turtle yertle = new Turtle(habitat);
// Declares variable of type Turtle; calls Turtle constructor with argument habitat
// Keeps track of a new turtle which was just constructed
yertle.forward();
// tells turtle to move forward
yertle.turnLeft();
// tells turtle to turn left
yertle.forward();
// tells turtle to move forward
habitat.show(true);
// Shows steps taken by a turtle object
}
}
Dot Operator
- dot operator is used to access an object’s attributes and methods
- executes a method
yertle.forward()
- tells the object
yertle
to execute the methodforward()
- Give arguments in parenthesis
- Data used by the method
yertle.forward(50)
- tells the object
- accesses a member variable
- They don’t talk about being able to do this in this lesson, but if I went along with saying that the dot operator only runs methods I would kind of be lying
- depends on variable scope
- this is it’s own can of worms which is why they probably didn’t mention it
- if you don’t understand scope, don’t worry about this tangent; they’ll cover it later :)
- executes a method
Creating Turtle Objects: 2.1.3
- Class instances can be named anything
- Are all unique objects
A class diagram of Turtle
Turtle: SimpleTurtle
Attributes
- name: String
- bodyColor: Color
- width: int = 15
- height: int = 18
- xpos: int = 0
- ypos: int = 0
Methods
- forward()
- backward()
- turnLeft()
- turnRight()
- penUp()
- penDown()
- forward(pixels: int)
- backward(pixels: int)
- turn(degrees: double)
- moveTo(x: int, y: int)
- setColor(theColor: Color)
- setWidth(theWidth: int)
- setHeight(theHeight: int)
Programming Challenge - Turtle Drawing: 2.1.4
Nothing to note; just complete the task
Summary: 2.1.5
- class defines a new data type
- formal implementation or blueprint of the attributes and behaviors of objects of that class
- object is a specific instance of a class
- declared as variables of a class type
- attribute/instance variable
- data the object “knows” about itself
- method is something an object can do
- turtle can
forward()
- turtle can
AP Practice: 2.1.6
- A class is a blueprint for an object
- Useful for modeling real world objects
- Each instance has different attributes
- Belt
- has a length
- has a color
- has a isReversable boolean
Closing Thoughts
My main complaint with this section was the assertion that the dot operator could only be used to access methods. I understand that accessing attributes wasn’t explained to avoid the complexity which comes along with scope, but I feel like the explanation of the dot operator could have been phrased better.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.