From Snap! sprites to Java objects.
You already know more about programming than you may think. Today you will use physical LEGO bricks to discover the ideas behind classes, objects, attributes, methods, abstraction, and algorithms, before writing your first Java program.
Start with what you already know.
In Snap!, a sprite has information that belongs to it and blocks that tell it what to do. Java uses different vocabulary, but the big idea is familiar.
Sprite
Information: x position, y position, direction, costume, variables
Actions: move, turn, say, custom blocks
Object
Attributes: information that describes the object's state
Methods: actions the object can perform
You will eventually write these ideas with Java text instead of dragging blocks.
Build it. Write it. Run it. Debug it.
Before we talk about Java objects, you are going to experience what it feels like to write an algorithm for a very literal computer. Your partner will be the computer.
Build: “Write the program”
Partner A quietly builds a small, unique LEGO structure using pieces from the bag.
Partner B may not look.
Write: Create the algorithm
Partner A writes a detailed, step-by-step set of directions that explains exactly how to recreate the structure.
No drawings, photos, or pointing. Use words only.
Run: Execute the instructions
Partner B starts with the matching unused LEGO set and follows the written instructions exactly as written.
Partner A may not explain, clarify, or correct the instructions while they are being executed.
Debug: Compare the results
Lift the barrier and compare the original structure with the replica.
Find the first place the results differed. What instruction caused the problem?
Logic error
The instruction was legal and could be followed, but it produced the wrong result.
Example: “Turn the red brick sideways” was too vague, so the partner chose the wrong direction.
Compilation-style error
The instruction cannot be carried out as written.
Example: “Use the green 2×6 brick” when no green 2×6 brick exists in the matching set.
Class or object?
Pick up one LEGO brick. Ask yourself whether you are holding the idea of a brick or one actual brick.
Class = blueprint
A class describes a type of thing. Think of the mold, blueprint, or design LEGO could use to produce many similar bricks.
A class describes what objects of that type know and what they can do.
Object = one instance
The physical piece in your hand is one specific object. Another brick made from the same design is a different object.
One class can be used to create many objects.
Describe one brick object.
With a partner, choose one brick and separate its information into attributes and possible methods.
Attributes: what it knows
colorlengthwidthstudCountisAttachedAttributes describe the state of an object. They are similar to information stored in sprite variables.
Methods: what it does
snapTo()disconnect()rotate()printDetails()Methods describe behaviors. They are similar to commands or custom blocks in Snap!.
Build a Vehicle class.
Work in a group of 2 to 3. You are designing one blueprint that could describe many different LEGO vehicles.
Design
Agree on 3 attributes every vehicle object should have.
Behaviors
Agree on 2 methods every vehicle object should be able to perform.
Build
Construct two different vehicle objects that both fit your class.
VehicleSame class. Different objects.
Compare your two builds. They should share the same kind of information and behavior, while storing different values.
Shared blueprint
Both objects belong to the Vehicle class.
Both might have attributes such as color, wheelCount, and speed.
Both might support methods such as driveForward() and turn().
Different state
One object might be a red racecar with four wheels.
Another might be a blue truck with six wheels.
They are different objects because their attribute values can be different.
Algorithms live inside methods.
Earlier, your LEGO directions were an algorithm: a precise sequence of steps that another “computer” could execute. Java methods work the same way.
Method name = useful abstraction
driveForward() gives a behavior a short, meaningful name.
The programmer using the method does not need to think about every low-level step each time.
Method body = algorithm
Inside the method are the precise instructions that make the behavior happen.
The computer executes those instructions literally, just like your partner executed the LEGO directions.
Partner discussion
Choose one method from your Vehicle class. What lower-level steps would need to happen inside that method?
Abstraction hides unnecessary detail.
Hold up one finished vehicle. You probably call it a car or truck, not “a collection of plastic pieces arranged in a particular geometric pattern.”
High-level idea
Vehicle
You focus on the important concept without thinking about every stud, edge, and connection.
Programming connection
A class is an abstraction. It lets us work with a meaningful idea such as Vehicle while hiding many lower-level implementation details.
Abstraction helps programmers manage complexity.
What will this look like in Java?
You are not expected to understand every symbol yet. The goal is to recognize the ideas you already modeled with bricks.
class Vehicle
The blueprint.
color and wheelCount
Attributes that store state.
driveForward()
A method whose behavior is created by an algorithm.
Exit reflection.
1. Snap! to Java
How is a Snap! sprite similar to a Java object?
2. Class vs. object
Explain the difference using today's LEGO activity.
3. Attributes and methods
Give one attribute and one method from your Vehicle class.
4. Algorithm and abstraction
How did the LEGO activity show both ideas?
Download your work.
Your name, email, and responses stay in this browser. When you download your Word document, the file is created on this device and includes your student information and every response from this activity.
LEGO Object-Oriented Blueprint Responses
Review your responses above before downloading. You can download a new copy at any time.