Skip to content

Course navigation

For teachersTopic 3.1 · Lesson kit

Abstraction and Program Design

  • 2 class periods
  • 45 minutes each
  • Objective 3.1.A
  • 6 downloads with keys

Downloads

  • Slides

    Speaker notes on every slide

  • Lesson plan

    2 periods of 45 minutes

  • Worksheet

    Puzzles, about 30 minutes

  • Worksheet key

    Answer key

  • Exit ticket

    End of the last period

  • Exit ticket key

    Answer key

Download all AP CSA teacher filesEvery topic's slides, lesson plan, worksheet, and exit ticket, with keys (.zip)

At a glance

Pacing
2 class periods of 45 minutes
Learning objectives
3.1.A
Essential knowledge
3.1.A.13.1.A.23.1.A.33.1.A.43.1.A.53.1.A.63.1.A.7
Materials
Projector and the Keep What Matters slide deckWhiteboard and markersMini whiteboards or scrap paperIDE on the projector (student computers optional)Printed Keep What Matters puzzle worksheet (answer key for you) and exit ticket

45 minutes a day

Agenda

Day 145 minutes

  • Warm-up
  • Direct instruction
  • Independent practice
  • Closure
  • Activity
  1. 0:00–0:055 minWarm-upsame phone, two descriptions (slide 2)
  2. 0:05–0:1510 minDirect instructionabstraction and data abstraction (slides 3–6)
  3. 0:15–0:194 minActivityDay 1 pause and try — keep or hide? (slide 7)
  4. 0:19–0:3011 minDirect instructionattributes, class diagrams, instance and class variables (slides 8–11)
  5. 0:30–0:355 minActivityDay 1 pause and try — instance or class? (slide 12)
  6. 0:35–0:427 minIndependent practicepuzzle worksheet questions 1–3
  7. 0:42–0:453 minClosuresame kind of data, same value?

Day 245 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Closure
  • Activity
  1. 0:00–0:055 minWarm-upwhat are lines 7–11 for?
  2. 0:05–0:149 minDirect instructionprocedural abstraction and method decomposition (slides 13–14)
  3. 0:14–0:217 minDirect instructiongeneralizing with parameters (slide 15)
  4. 0:21–0:276 minDirect instructionchanging how a method works (slide 16)
  5. 0:27–0:314 minActivityDay 2 pause and try — tell the callers, or not? (slide 17)
  6. 0:31–0:398 minGuided practicedesign before you code (slides 18–19, 21)
  7. 0:39–0:456 minClosurevocabulary (slide 20), then the exit ticket

Overview

Topic 3.1 opens Unit 3, where students start designing their own classes. It is a planning lesson, not a syntax lesson. Day 1 is about data: abstraction as choosing the details a program needs (3.1.A.1), data abstraction as separating what data means from how it is stored (3.1.A.2), and attributes, sorted into instance variables and class variables (3.1.A.3). Day 2 is about behavior and design: procedural abstraction and method decomposition (3.1.A.4), parameters that turn duplicated code into one general method (3.1.A.5), changing a method’s inside without telling its callers (3.1.A.6), and designing a class in words or a class diagram before coding it (3.1.A.7).

Keep one question running through both days: “What does this program need?” It decides which details to keep, which attributes a class has, whether an attribute is shared, and which data each behavior needs.

Stay inside the boundary. Students do not write attributes or constructors in Java yet (Topics 3.3 and 3.4), and the keyword that makes a variable a class variable waits for Topic 3.7. Class variables appear only in designs, labeled “(class variable)” in a class diagram. The Java on Day 2 uses public static helper methods in one class, as in Topic 1.9; tell students to read past public static, which is explained in later topics.

Teacher note

The slides are the Keep What Matters deck (21 slides); slide numbers below refer to it. Day 1 uses slides 1–12 and Day 2 uses slides 13–21. The Keep What Matters puzzle worksheet (10 questions, about 35 minutes) covers both days: questions 1–3, 7, and 8 need only Day 1 material, and questions 4–6, 9, and 10 need Day 2.

Demonstrations come from the topic’s examples/ folder (.snippet.java files hold only the statements for main). Day 1: TwoTimeRepresentations, OneHourLaterWrong, OneHourLaterFixed. Day 2: PizzaPartyOneBlock, PizzaParty, StarsRepeated, StarsMethod, BonusCopies, BonusMethod, PizzaIfVersion, PizzaFormulaVersion, PizzaNewSignature, PizzaForgotRoundUp. Open them in the IDE before class so each demonstration is one click.

Day 1 · Warm-up: Same phone, two descriptions

Show slide 2. Each student writes three facts about their phone for someone who might buy it, then three for the person repairing it. Give two minutes, then collect a few lists on the board. Buyers’ lists usually have price, storage, and camera; repairers’ lists have the model, the cracked screen, and battery health. The model often makes both lists.

Ask: “Which list is the right description of the phone?” Both are, each for its own reader. Tell students that today has a name for what they just did.

Day 1 · Direct instruction: Abstraction and data abstraction

  1. Goals (slide 3). Read the five goals aloud; the first three are today’s, the last two are Day 2’s.
  2. Abstraction (3.1.A.1, slides 4–5). Abstraction reduces complexity by hiding the details that do not matter for the problem and bringing together the ones that do. Walk through the dog table on slide 5 one row at a time, and ask for each row, “Which program needs this?” Point out that breed is kept by both programs, and the favorite toy by neither. A subway map is a quick everyday example: it leaves out the streets and keeps the stations, because that is what a rider needs.
  3. Data abstraction (3.1.A.2, slide 6). Two programs store the same song length in different ways: one int of seconds (215), or two ints of minutes and seconds (3 and 35). Both print 3:35. Ask: “What is the data?” (a song 3 minutes 35 seconds long) and “How is it stored?” (different in each column). Data abstraction is that separation: you use data by its name and meaning, and the details of its representation stay out of the way. Data can be one variable, a class that brings related data together (a String holds many characters under one name), or a set of data.
  4. Live demo. Run TwoTimeRepresentations: 10:15 stored as 615 minutes since midnight, and as 10 and 15. Then show OneHourLaterWrong and ask what start + 1 does. Students who guess “one hour later” are surprised by Ends at 10:16. Run OneHourLaterFixed to show that the name startMinutes carries the unit, so adding an hour clearly means adding 60.

Misconception: Abstraction means leaving out as much as possible

Students hear “hide details” and conclude that fewer details is always better. A library program that keeps only the title cannot answer “When is it due back?” Abstraction is choosing, not removing. Give students one question to ask of every detail: “Does any question this program answers depend on it?” Keep it if yes, hide it if no.

Misconception: A name that hides what the data means

A variable named start or t does not say what unit it holds, so a teammate guesses, and the guess becomes a logic error that compiles and runs. The OneHourLaterWrong demo is the fix: let students predict before you run it. Then ask for a better name, and point out that the unit is part of what the data means.

Day 1 pause and try: keep or hide?

Show slide 7. Students decide, alone and then with a partner, which facts a trail-picking app should keep. Keep the trail name, length, elevation gain, and today’s weather; hide the year it was built and the ranger who mapped it. If someone argues for the year it was built, ask which choice a hiker makes with it. That is the test from the misconception above.

Day 1 · Direct instruction: Attributes, class diagrams, instance and class variables

  1. Attributes (3.1.A.3, slides 8–9). Every variable students have written so far lives inside a method and is gone when the method ends. An attribute is data defined in a class, outside any method or constructor, so it describes the whole object and every behavior can use it. Say plainly that writing attributes in Java comes in Topic 3.3; today is planning.
  2. Class diagram (slide 9). Read the GameCharacter diagram top to bottom: the class name, then the attributes as type and name (read like a declaration), then the behaviors as method headers (read like Topic 1.9 headers). Ask which data takeDamage and isAlive need (health).
  3. Instance or class variable (slide 10). An instance variable has its own value in each object: Kai’s, Rosa’s, and Lee’s jersey numbers differ, and changing Kai’s changes only Kai. A class variable has one value shared by every object: rename the team once, and every player sees the new name.
  4. The test (slide 11). Show the left box: if health were a class variable, the dragon’s hit on Ada would lower everyone’s health. Write the test on the board and leave it up: “Should every object have the same value?” Not “Does every object have one?”

Misconception: Every object has one, so it should be shared

This is the most common sorting error. Every bank account has a balance and every character has health, so students mark them as class variables. Then one deposit changes every account. Point back to slide 11 and the test on the board: shared means one value for all, not one kind of data for all.

Misconception: A temporary value as an attribute

Students list a loop counter such as int i as an attribute because a behavior uses it. A counter that matters only while one method runs belongs inside that method. Ask of each candidate: “Does every object need to remember this the whole time it exists?”

Day 1 pause and try: instance or class?

Show slide 12. Students mark each HotelRoom attribute on mini whiteboards, then reveal together. roomNumber and occupied are instance variables, because each room has its own value. hotelName and checkoutHour are class variables, because every room in this hotel shares one value. Ask a student who chose differently to apply the test from slide 11 aloud.

Day 1 · Independent practice: Independent practice

Hand out the puzzle worksheet. Students work Part A, questions 1–3: question 1 sorts runner facts into keep and hide for two apps, question 2 traces a distance stored in meters, and question 3 sorts SchoolBus items into instance variable, class variable, behavior, or leave out. Circulate on question 3 and look for int i and dispatchPhone; those two are where the misconceptions show.

Teacher note

Questions 7 (the instance-versus-class face-off) and 8 (two truths and a lie about a LibraryCard diagram) also need only Day 1 material and are homework, along with any of questions 1–3 not finished in class. Questions 4–6, 9, and 10 wait for Day 2.

Day 1 · Closure: Closure

Cold-call two students: “A Student class has name, gradeLevel, and schoolName, and every student in the program goes to the same school. Which one is a class variable, and why?” (schoolName: every student should have the same value.) Then ask what goes wrong if gradeLevel were shared.

Day 2 · Warm-up: What are lines 7–11 for?

Show PizzaPartyOneBlock without running it. Ask students to write, in a few words, what lines 7–11 do. Give two minutes. Most will get there (they round up to whole pizzas), but it takes reading every line. Ask: “How could the code tell you that, so you would not have to work it out?” Collect “give it a name” if someone says it; that is today’s first idea.

Day 2 · Direct instruction: Procedural abstraction and method decomposition

  1. Procedural abstraction (3.1.A.4, slides 13–14). From Topic 1.9: you can use a method by knowing what it does, not how, the way students use Math.sqrt. Procedural abstraction also gives a process a name: the name stands for every step.
  2. Method decomposition (slide 14). Breaking a large behavior into smaller behaviors, one method for each. Read main on slide 14 aloud: it reads like a plan (find the average, find the letter, print the report). Ask a student to trace letter(85.0) to confirm B.
  3. Live demo. Run PizzaParty beside the warm-up program. Both print the same two lines, but main is now four steps, and pizzasNeeded hides the rounding. Ask: “What does a reader of main need to know about pizzasNeeded?” Only that it returns the whole pizzas needed for a number of slices.

Misconception: Naming a method for how it works

Students name a rounding method divideAndMaybeAddOne, which puts the steps back into every call and becomes wrong if the steps change. A procedural abstraction’s name says what the method does: pizzasNeeded. Have students rename two of their own past variables or methods by this rule.

Day 2 · Direct instruction: Generalizing with parameters

  1. Duplicated code (3.1.A.4, 3.1.A.5). Run StarsRepeated: three loops identical except for 5, 3, and 8. Ask what the copies have in common and what differs. The part that differs becomes a parameter.
  2. One general method (slide 15). The two curve copies on the left become curve(int score) on the right. Ask students to predict the three outputs before you reveal them: 95, 100 (capped), and 66. Then run StarsMethod: one printStars(int count), three calls. Name the payoff: code reuse. Write the method once and call it with a range of arguments.
  3. Why copies hurt. Run BonusCopies: the bonus rule changed in one copy only, so two players with the same score get 100 and 90. Nothing crashes. Run BonusMethod: one rule, both totals 100.

Misconception: Fixing one copy of duplicated code but not the other

Duplicated code works until a rule changes, and then a missed copy is a logic error that still compiles. Students do not see the risk while the copies agree. BonusCopies makes it visible; the fix is to keep the rule in one method so a change happens in exactly one place. The worksheet’s Fair Question box after question 5 says the same thing.

Day 2 · Direct instruction: Changing how a method works

  1. The promise (3.1.A.6, slide 16). Callers depend on two things: the method’s signature (name and parameter types) and what it does. Keep both, and the inside can change, to be faster, shorter, or use less storage, with no need to tell the callers.
  2. Safe change (slide 16, left). The new body of curve returns 100 for any score of 95 or more and adds 5 otherwise. Check it with 90 and 98: same results as before, so callers never notice.
  3. Unsafe change (slide 16, right). Adding a second parameter changes the signature, so the old call curve(90) no longer compiles.
  4. Live demo. Run PizzaIfVersion, then PizzaFormulaVersion ((slices + 7) / 8): same outputs 5, 5, 6, so it is safe. Run PizzaNewSignature (compile error, like slide 16). Then run PizzaForgotRoundUp: it compiles, and prints 4, 5, 5. Ask: “Which of these two broken changes is worse?” The second one, because nothing warns anyone.

Misconception: If it still compiles, the change is safe

The signature is only half of the promise. PizzaForgotRoundUp compiles and gives wrong answers. Teach the habit: before changing a method’s inside, test the new version on the same inputs as the old one, including edge cases such as an exact multiple of 8.

Day 2 pause and try: tell the callers, or not?

Show slide 17. Students decide for each change, then vote by holding up fingers for A, B, or C as the safe one. Only C is safe: it returns the same value for every score (a curved of exactly 100 returns 100 either way). A keeps the signature but drops the cap, so curve(98) returns 103 instead of 100: callers must be told. B renames the method, which changes the signature, so every existing call fails to compile.

Day 2 · Guided practice: Design before you code

  1. Why design first (3.1.A.7, slide 18). Attributes are used by every behavior, and other programmers call the methods, so a mistake found halfway through coding can mean a rewrite. A design can be written in natural language or drawn as a class diagram.
  2. Worked design (slide 19). Show only the RentalBike description first. Ask pairs to apply steps 1–3 before you reveal the diagram: facts each bike remembers (ID, rented) are instance variables; the hourly price is the same for every bike, so it is a class variable; the verbs become behaviors, and “for a number of hours” means cost needs a parameter. Then do step 4 together: does each behavior have the data it needs?
  3. Pairs design one (slide 21). Pairs sketch a class diagram for something in the room (a desk, a projector, a water bottle): two or three attributes, two behaviors, and one class variable if any value should be shared. Each pair finishes the sentence starter on slide 21. Call on two pairs and have the class run the step 4 check on their diagram.

Misconception: A behavior with no data to work with

Students write a behavior such as boolean isOverdue() in a LibraryBook design that has no due date. The design looks complete until someone asks how the method could decide. Step 4 on slide 19 is the fix: for each behavior, ask “What data does this need?” and add the missing attribute or parameter. Worksheet question 9 practices exactly this.

Teacher note

Keep the pair designs to plain types from Units 1 and 2 (int, double, boolean, String). If a pair wants an attribute that is a list of things, say that storing sets of data comes in Unit 4 and ask them to choose a different attribute for now.

Day 2 · Independent practice: Independent practice

Puzzle worksheet questions 4–6, 9, and 10 are homework, about 20 minutes, plus questions 7 and 8 if they were not finished after Day 1. Question 4 (code tiles) is procedural abstraction with hidden method bodies, question 5 (snippet pool) is generalizing with parameters, question 6 (safe change?) is changing a method’s inside, question 9 (design detective) finds two problems in a PetFeeder diagram, and question 10 (who am I?) reviews the vocabulary.

Day 2 · Closure: Closure and exit ticket

Spend one minute on slide 20 (vocabulary): read each term and have the class give an example from today. Then give the exit ticket (about five minutes). Score each question separately: question 1 checks designing a class with instance and class variables (3.1.A.1, 3.1.A.3, 3.1.A.5, 3.1.A.7), question 2 changing a method’s inside (3.1.A.4, 3.1.A.6), and question 3 data abstraction (3.1.A.2). Re-teach the weakest one at the start of Topic 3.2.

Exit ticket

Question 1 · Multiple choice

“Each scooter has its own ID and battery percent. Every scooter in the city charges the same price per minute. A scooter can be unlocked and can report the cost of a ride for a given number of minutes.” Which class design fits this description?

  1. Instance variables String scooterId, int batteryPercent, double pricePerMinute; behaviors void unlock(), double rideCost(int minutes)
  2. Instance variables String scooterId, double pricePerMinute; class variable int batteryPercent; behaviors void unlock(), double rideCost(int minutes)
  3. Instance variables String scooterId, int batteryPercent; class variable double pricePerMinute; behaviors void unlock(), double rideCost()
  4. Instance variables String scooterId, int batteryPercent; class variable double pricePerMinute; behaviors void unlock(), double rideCost(int minutes)
Check your reasoning

D. The ID and battery percent are different for each scooter, so each scooter needs its own copy: instance variables. The price per minute is the same for every scooter, so one shared copy is enough: a class variable. The cost depends on how long the ride was, so rideCost takes the minutes as a parameter and returns a double that can hold cents.

  • A makes the price an instance variable, so every scooter keeps its own copy of a value that should be the same for all of them; changing the price would mean changing every scooter.
  • B swaps the two kinds of variable: one shared battery percent would give every scooter the same charge, and each scooter would have its own price.
  • C leaves out the parameter, so rideCost has no way to know how many minutes the ride lasted.

Question 2 · Multiple choice

Many programs call public static int teamsNeeded(int players), which returns how many teams of 5 are needed, rounded up (12 players need 3 teams). Which change can be made without telling the programmers who call it?

  1. Renaming the method to teamCount
  2. Replacing the body with return players / 5;
  3. Replacing the body with return (players + 4) / 5;
  4. Adding a second parameter, int perTeam, to the header
Check your reasoning

C. It keeps the signature and returns the same result for every input: 12 players gives 16 / 5 = 3, and 10 players gives 14 / 5 = 2. Callers depend only on the signature and what the method does, so the inside can change.

  • A changes the name, which is part of the signature, so every existing call fails to compile.
  • B keeps the signature but changes what the method does: 12 players now gives 2 teams, not 3. It compiles, so callers get wrong answers with no warning.
  • D changes the parameter list, so every one-argument call fails to compile.

Question 3 · Multiple choice

A program stores a movie’s length as int t = 135;, counting minutes. A teammate wants the movie to be two hours longer and writes t + 2, which gives 137: two minutes longer. Which change best prevents this kind of mistake?

  1. Rename t to lengthInMinutes, so two hours longer is clearly lengthInMinutes + 120
  2. Rename t to movieLength, so two hours longer is movieLength + 2
  3. Change the declaration to double t = 135.0;
  4. Store the length in hours instead, as int t = 2;
Check your reasoning

A. The mistake happened because the name t does not say what the value means or what unit it uses, so the teammate guessed hours. A name that carries the meaning, including the unit, tells every reader to add 120 minutes (255 in all).

  • B says what the value is but not its unit, so the teammate can still guess hours and write + 2.
  • C changes the type, not the meaning; t + 2 is still two minutes longer, now as 137.0.
  • D loses data: an int cannot hold 2 hours 15 minutes, so the 15 minutes are gone.