For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

Unit 4: Methods and Classes to Abstract Drawings

In this unit students write their own methods and classes. A drawTree() method hides the details of drawing a tree. A Tree class goes further: each tree becomes an object with its own instance variables and methods, just like the Easy Draw shapes students have been using all year.

Teacher notes

  • Time: 9 class periods: 4 lessons (1 class each) and 5 classes for the unit project

  • AP Computer Science A: Writing Classes (instance variables, constructors, methods, accessors and mutators, this, static, encapsulation, and scope).

  • Before this unit: students have finished Unit 3: Iteration and Drawings.

  • Files: projects now have three files in one folder: EasyDraw.java, Main.java, and the student's class, like Tree.java. Compile with javac *.java.

Learning Objectives

Students will be able to:

  1. Write static methods with parameters to draw reusable pictures.

  2. Write methods that return a value.

  3. Write a class with private instance variables, a constructor, accessor methods, and mutator methods.

  4. Use this to refer to the current object's instance variables.

  5. Create many objects from their own class and store them in an array.

  6. Explain how classes abstract (hide) details so a program is easier to read and change.

Lesson 4.1: Static Methods That Draw

Vocabulary: method header, parameters, arguments, method call.

Practice:

  1. Use a loop in main to call drawTree() 6 times across the canvas.

  2. Add a String color parameter so each tree's leaves can be a different color.

Lesson 4.2: Methods That Return Values

Practice: Move circle b to (400, 300). Predict the color, then run it.

Lesson 4.3: Writing a Class

drawTree() draws a tree, but afterward we cannot move it or change it. A Tree class keeps references to its shapes in instance variables, so every Tree object can be changed later.

Tree.java:

Main.java:

Walk through the parts of the class:

  • Instance variables (x, y, size, trunk, leaves) - every Tree object has its own copy. They are private, so only Tree's own methods can change them. This is encapsulation.

  • Constructor - runs when we write new Tree(...). this.x = x; copies the parameter x into the instance variable x.

  • Accessor methods (getX(), getY()) return information.

  • Mutator methods (move(), setLeafColor()) change the object.

Discussion: Main does not know a tree is made of a rectangle and a circle. If we changed the leaves to a triangle, would Main need to change? This is abstraction.

Practice:

  1. Add a grow(double amount) method that makes the tree bigger. (Hint: change size, then update both shapes with their setters.)

  2. Add a toString() method that returns something like "Tree at (150.0, 400.0)".

Lesson 4.4: Objects That Respond to Events

A class can set up its own event handlers in its constructor. Each object then responds on its own.

Balloon.java:

Main.java:

Discussion: popped is static, but body is not. What is the difference? (There is one popped shared by every balloon, but each balloon has its own body.)

Unit Project: My Own Class

Students design a class for something that can be drawn and changed, then write a program that uses many objects of that class. Ideas: Car, Fish, House, Robot, Monster, Flower, Spaceship.

Requirements:

  • A class in its own file with at least 3 private instance variables, including at least 2 Easy Draw shapes.

  • A constructor with parameters that uses this.

  • At least 2 accessor methods and at least 2 mutator methods (one should move the whole drawing).

  • A toString() method.

  • At least one static variable or method in the class.

  • Main creates at least 5 objects, stored in an array, and uses a loop to call a method on each one.

  • At least one event (click or key) changes the objects.

  • A short written explanation of how the class abstracts the drawing.

Category
4 - Exceeds
3 - Meets
2 - Approaching
1 - Beginning

Class design

Well-encapsulated class with meaningful methods

All required variables, constructor, and methods

Missing some required parts

Class does not compile

Objects

Many objects interact in interesting ways

5+ objects in an array, changed with a loop

Objects not stored in an array

One object

Events

Objects respond to several kinds of events

At least one event changes the objects

Event attempted but does not work

No events

Code quality

Clear names, Javadoc comments on every method

Comments explain each method

Few comments

Hard to read

Explanation

Clearly explains abstraction and encapsulation

Explains how the class hides details

Explanation is unclear

Missing

Last updated

Was this helpful?