We’ve now had a chance to explore the Turtle class and its basic capabilities. However, what happens if we aren’t satisfied with its current capabilities and want to add new ones to a class? In this reading we’ll look at how to add new methods to a class, focusing for now on modifying the Turtle class. We’ll also get an early glimpse at our next topic, pictures, by learning how to create and display pictures.
Sections 3.5-3.7 (pp. 50-74) from the course textbook.
Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.
void
.drawSquare
) to the Turtle
class using DrJava.drawSquare
).Picture
object based on a picture to be selected with the file chooser.The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.
These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.
I wrote a method named drawRectangle
that makes a turtle draw a rectangle of a given width and height. The header for that method is given below.
public void drawRectangle(int width, int height)
If I have a turtle variable named satTheTurtle
, write a Java expression that makes this turtle draw a 30x50 rectangle. Note that you don’t have to write the code inside of the drawRectangle
method, you should simply call that method with the correct parameters (like you would call e.g. the forward
method).
In the body of my drawRectangle
method, would I need to create a new turtle object? Explain why or why not.
For each of the following, write a Java statement that performs that action.
myPic
that refers to a new picture whose contents we will decide using the file chooser.copyOfMyPic
and make it refer to the same picture object as before.