First day of AP Computer Science A

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.

01

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.

Snap!

Sprite

Information: x position, y position, direction, costume, variables

Actions: move, turn, say, custom blocks

move 10 stepsturn 15°
→
Java

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.

02

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.

Setup: Work with one partner. Partner A and Partner B each need an identical bag of 10–15 LEGO bricks. Put a folder, binder, or other barrier between you so neither partner can see the other workspace.
1

Build: “Write the program”

Partner A quietly builds a small, unique LEGO structure using pieces from the bag.

Partner B may not look.

2

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.

3

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.

4

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?

Partner A's algorithm

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.

The computer lesson: A computer does what you tell it to do, not what you wanted it to do. Precise instructions matter.
Where did your replica first differ?
What instruction would you rewrite?
03

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.

04

Describe one brick object.

With a partner, choose one brick and separate its information into attributes and possible methods.

Attributes: what it knows

colorlengthwidthstudCountisAttached

Attributes 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!.

Important: there is not always one perfect answer. A programmer decides which attributes and methods matter for the problem being solved.
05

Build a Vehicle class.

Work in a group of 2 to 3. You are designing one blueprint that could describe many different LEGO vehicles.

1

Design

Agree on 3 attributes every vehicle object should have.

2

Behaviors

Agree on 2 methods every vehicle object should be able to perform.

3

Build

Construct two different vehicle objects that both fit your class.

Class name
Vehicle
3 attributes
2 methods
Object #1
Object #2
Your responses save automatically in this browser.
06

Same 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.

07

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?

08

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.

09

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.

public class Vehicle { private String color; private int wheelCount; public void driveForward() { // an algorithm will go here } }

class Vehicle

The blueprint.

color and wheelCount

Attributes that store state.

driveForward()

A method whose behavior is created by an algorithm.

10

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?

11

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.

Your completed activity

LEGO Object-Oriented Blueprint Responses

Review your responses above before downloading. You can download a new copy at any time.

Privacy: This page stores the student profile and activity responses only in this browser's local storage. The Word file is generated locally. The site does not upload the profile or responses to a server.