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.
Learning Objectives
Students will be able to:
Write
staticmethods with parameters to draw reusable pictures.Write methods that return a value.
Write a class with
privateinstance variables, a constructor, accessor methods, and mutator methods.Use
thisto refer to the current object's instance variables.Create many objects from their own class and store them in an array.
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:
Use a loop in
mainto calldrawTree()6 times across the canvas.Add a
String colorparameter 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) - everyTreeobject has its own copy. They areprivate, so onlyTree's own methods can change them. This is encapsulation.Constructor - runs when we write
new Tree(...).this.x = x;copies the parameterxinto the instance variablex.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:
Add a
grow(double amount)method that makes the tree bigger. (Hint: changesize, then update both shapes with their setters.)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
privateinstance 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
staticvariable or method in the class.Maincreates 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.
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?