CS 161 Lab B - Turtle Graphics
Due Fri Sep 13 at 9:00am
Overview
In the late 60s, Seymour Papert at MIT developed a robot "turtle" to help teach young children to program. This robot has a small pen attached to it, which would draw a trail (a line) on a piece of paper as the robot moved and turned. Children could then program the robot's movement in order to have it draw a particular picture. This method of producing drawings is known as turtle graphics, versions of which can be found in pretty much every programming language.
In this lab, you will program a Java version of the robot to draw some simple images. This will give you a chance to continue practice writing Java code and calling methods on Java objects.
Remember that this lab should be completed in pairs. You will need to find a different partner than you had for the last lab. Be sure to review the pair programming guidelines before you begin; remember to switch off who is driving and who is navigating!
Objectives
- To practice writing Java code.
- To practice calling methods on objects, and passing parameters to those methods.
- To practice working with numbers and variables.
Necessary Files
You will need to download and extract the Turtles BlueJ project from the
LabB.zip file.
This project includes Turtle
class that has already been written for you, as well as a Pyramid
class that you will need to fill in.
The Turtle
-
The turtle class is relatively simple to use in a program. You will first need to instantiate a
Turtle
object using the following code:Turtle turtle = new Turtle();
If you do this (or instantiate the object by right-clicking in BlueJ), you should be able to see the Turtle on a canvas: -
You can tell the turtle to move forward with the
forward()
method, and you can tell it to turn with theright()
andleft()
methods. If you don't want the turtle to draw a line when it moves, use thepenUp()
method to raise the pen (and remember to usepenDown()
to put it down again!) -
In code, you can call methods on the turtle by using dot notation---the name of the object, followed by a period (a dot), followed by the name of the method.
-
Remember that methods may (or may not!) have parameters: values that go inside the parentheses. Remember that parameters just include the value, not the data-type. So if you want to tell the turtle to move forward 20, you say
turtle.forward(20);
, not turtle.forward(int20); If you're not sure whether to include parameters or what to include, check the documentation below or the method comment that appears when you select a method in BlueJ: (1) tells you that the parameter is going to be a double (a number that can have a decimal point), (2) tells us what that number means (in this case, that it is the distance in pixels), and (3) is where we enter the parameter.
-
Remember that methods may (or may not!) have parameters: values that go inside the parentheses. Remember that parameters just include the value, not the data-type. So if you want to tell the turtle to move forward 20, you say
-
A note about color:
-
Color is actually a complex topic in computer science; colors can be represented in lots of different ways. In the previous assignment, we used Strings (like "red") to talk about colors. In Java, you use a previously-written class called
Color
. So the color red is represented by a particularColor
object---in Java almost everything is an object! -
In order to use this class, you need to tell Java that you want it (otherwise Java is lazy and won't include it). To do this, include the line:
import java.awt.Color;
This tells Java to include the class, which has the full name ofjava.awt.Color
. We put this line at the very top of the file for the class where we want to use Color objects. -
Java has already created a lot of
Color
objects for you to use. These objects are referenced with the formColor.RED
orColor.BLUE
---note that the name of the color is in all capital letters (these are called constants; we will talk about them later). All of the colors we have used so far are available in this way---a full list can be found here. -
Finally, if you want to specify a color using the BlueJ interface (such as when you right-click on the method to call it), you'll need to use the full name of the Color object. For example:
java.awt.Color.RED
(note that this is one of the few instances we will use full names for classes). - Or you can just use the
setPenColorByName()
method, which lets you pass in the color name as a String.
-
Color is actually a complex topic in computer science; colors can be represented in lots of different ways. In the previous assignment, we used Strings (like "red") to talk about colors. In Java, you use a previously-written class called
-
You can experiment with the turtle by right-clicking on the Turtle class and creating a new instance in BlueJ, or by using the instantiation code in either a class or in the codepad.
- Helpful hint! After you create the turtle, make sure to click on the BlueJ window to give it focus again; this will make it easier to select methods from the object bench!
- Note that you can create multiple turtles and they will all show up on the same window; remember to give them different names!
Below is a list of some of the methods that the turtle supports:
-
forward(double distance)
//move the turtle forward the specified distance (in pixels) -
left(double angle)
//rotates the turtle to the left the specified number of degrees -
right(double angle)
//rotates the turtle to the right the specified number of degrees -
penUp()
//raises the pen, so that the turtle won't draw when it moves -
penDown()
//lowers the pen, so that the turtle will draw when it moves. Note that the pen is down by default. -
printText(String message)
//prints the given message on the screen. Note that this will not move the turtle. -
setPenColor(Color color)
//sets the color of the turtle's pen. Note that if called through BlueJ, you will need to specify the full path to the color (i.e.,java.awt.Color.RED
orjava.awt.Color.BLUE
). -
setSpeed(double speed)
//sets the turtle's speed. 0.5 is default, 1 is fastest, 0 is slowest. I find that 0.75 is good for testing.
Assignment Details
For this lab, you will need to write programs that instruct a turtle on how to produce two different drawings. You can produce these programs in either order---but make sure you do them both!
1. The House
- You will write a program that instructs the turtle to draw a simple picture of a house. Your house should have a roof, a door, and at least two windows. Otherwise this can be as simple or as complex as you like (though don't get bogged down in this!). Be sure and use multiple colors!
- You should write this program in a new class to represent your house drawing. Click on the New Class button and name your class
HouseDrawing
. - Delete the sample code BlueJ provides, and instead add in a main method:
-
Inside the body of the main method, you'll need to instantiate a new
Turtle
object; see the above section for details. When you then run the main method (by right-clicking on the class).- Also remember to import the
java.awt.Color
class!
- Also remember to import the
-
Once you have instantiated the Turtle, you can type in commands to begin drawing the house! Compile your class and re-run the main method to see the Turtle follow your instructions.
- Helpful hint! Try writing only a few method calls at a time, and then compile and test your method. Get used to this "write a little; test it; write a little more" workflow---it will make your life much easier in the future!
2. The Pyramid
-
You will also write a program that instructs the turtle to draw a simple
step pyramid,
such as the one shown below:
Your pyramid should have 4 layers, which get smaller as they are closer to the top. The challenge is: your pyramid should be variable sized---that is, I should be able to specify how big the bottom layer is, and the upper layers should be made smaller automatically! This kind of processing is what computers were meant for. Your pyramid should have the following dimensions:
- The height of each level is 1/4 the width of the bottom level
- Each level is 1/5 the width of the bottom level smaller than the one below. So if the bottom level is 200 pixels, then each level will be 200*1/5 = 40 pixels smaller than the previous.
- There are four layers, each centered on the other.
-
I have provided you with the
Pyramid
class to get you started. This class defines an object that represents a turtle drawing of a Pyramid. Inside the class you will find the following empty method: This method will cause BlueJ to prompt you for the width of the bottom brick when you instantiate the Pyramid class.- Your code for instantiating and calling methods on your turtle should go inside this method.
-
Because your pyramid has a variable size, you're going to need to use variables to determine how far to move the turtle. So if you want the turtle to move the height of the pyramid, you might make a variable called
height
and then tell the turtle to move that far:turtle.forward(height)
Basically, you should write the code to draw a single level, then write the code to change any variables, and then copy that code four times to draw the four levels.- Think about this before you start writing--it's the same kind of thinking you used to program Karol! Can you come up with a generalized algorithm for drawing a level with a given width, and then just change the current "width" that you're drawing?
-
Note that if you try to multiply an
int
(a whole number) by adouble
(a decimal) it will work fine, but you'll get adouble
as your result. You can't squeeze adouble
value into anint
variable, so Java will give you an error if you do somehing likeint x = y*0.5;
. Instead, you'll need to put that value into adouble
variable!
- Be sure and test your code with lots of different pyramid sizes---big, small, and inbetween! Your pyramids should all look the same, just bigger or smaller.
Submission
Make sure both your names are on both the classes you've modified (HouseDrawing
and Pyramid
), and upload the entire project directory to the LabB submission folder on hedwig (see the previous lab if you need help). Only one partner needs to upload the code. Make sure you upload your work to the correct folder! The lab is due at the start of class the morning after lab.
After you have submitted your solution, log onto Moodle and submit the Lab B Partner Evaluation. Both partners need to submit evaluations.
Grading
This assignment will be graded on approximately the following criteria:
- Submitted a
HouseDrawing
class that draws a house [40%] - Submitted a modified
Pyramid
class that draws a pyramid at any specified size [50%] - Code is formatted correctly (good indentation, names on the files, reasonable comments, etc). Select "Edit > Autolayout" from the BlueJ menu to automatically fix the indentation! [5%]
- Submitted a lab partner evaluation [5%]
Remember that it is very easy to get partial credit, so turn in whatever you have before the lab's due date!