CS 161 Assignment #3

Due Monday, February 5th by 11:59pm
(Not accepted after 2/7)

Introduction

This week you'll help implement a more complex project than you did on the previous assignment, but you'll extend an existing project rather than starting from scratch. The techniques you'll practice on this assignment should look pretty familiar after the last assignment and the last couple of labs.

The project you'll extend is modeled after the turtle made famous by the Logo programming language in the 1960s. The Logo turtle was displayed on a graphics screen, and could be given basic commands like "turn left 90 degrees" and "go forward 100". Ours will be simpler: We'll have commands to make our turtle face north, south, east, or west, and to move forward by a fixed amount.

The Assignment

For full credit, your class should contain all of the methods described below. They should have exactly the same name as shown and have the correct parameters where appropriate. As part of the grading process I will run a program that creates instances of your class and tests them. If your names or other details differ, my testing code won't compile and I'll get sad and grumpy.
  1. Start by downloading the TurtleProject BlueJ project and extracting it from the .zip archive like you did with the first project in lab. The project contains all of our old friends from the figures class, as well as a new Turtle class. The Turtle class is the only class you'll need to edit. (And, in fact, you should not modify any of the other classes.) Before you worry about writing any Java code, take a moment to play around with the existing program: You should be able to create instances of the Turtle class and call some methods on it, though it only knows how to face north at the moment which is kind of sad.
  2. Now that you've get a sense of how the turtle behaves, take a look inside the Turtle class to see how it's been implemented. Make sure you spend some time looking at the fields and thinking about how a turtle instance remembers which direction it's heading.
  3. Add faceSouth(), faceWest(), and faceEast() methods to the Turtle class. Please add them at the bottom of the class, where the comment instructs you to. (Grading will be easier if everyone puts their new code in the same places.) These will look a lot like faceNorth(). Like faceNorth(), none of them should have parameters — we don't need to supply any extra information when calling one of these methods. Test your new methods and make sure they work before proceeding.
  4. Our turtle is pretty slow — when we ask it to move forward, it takes pretty small "steps". We could just edit the code and change that 20 in the constructor to something larger, but a better solution would be to add a second constructor that lets the user specify the desired movement distance as a Turtle instance is created. That way users can customize their turtles to their needs. Add a second constructor just below the existing constructor. It should take a single parameter — an int that specifies how far the turtle should move on each call to forward(). (It's going to look enough like the existing constructor that it might make sense to copy-and-paste to make the second one, then do a bit of editing.) You should not need to add any new fields to the class to make this work.

    Before proceeding, make sure your second constructor works. When you right-click on the Turtle class, you should see two constructor options — the "default" constructor that doesn't require an input, and the one you added that takes a parameter. Make sure they both still work. Try creating multiple Turtle instances, each with different speeds, to be sure.

  5. Having a second constructor is pretty slick. It lets us customize our turtles so they're as fast or slow as we like, but we're stuck with a given distance setting after creating a turtle. We'd get even more control over our turtles if we added a method that could change their "speeds" any time we want. Add a method called setDistance that takes a single parameter — an int that specifies how far the turtle should move on each future forward call — and changes the distance field to take on the specified value. Please add this method just above the faceNorth method, where the comment suggests. Test your new method before proceeding.
  6. For full credit, your code should contain comments. There should be a Javadoc-style (/** ... */) comment at the top of the class containing your name and a sentence or two explaining what the project is about, and Javadoc-style comments above each of the methods in your class.

Extras

Looking for additional challenges? Try adding additional methods to control the turtle's heading (faceNorthwest, for example). Or add methods to modify other aspects of a turtle object's state (e.g. its color or size). The Logo turtle had an option for drawing a line behind it as it moved. Can you find a way to have our turtle draw lines behind it? (Fair warning: That's pretty hard...)

Grading

This assignment will be graded out of a total of 60 points.

Submitting

Before submitting, make sure your code compiles and is commented properly, and test each of your methods thoroughly. When you're convinced that it's ready to go, submit the assignment using BlueJ's submission tool as described here.


Brad Richards, 2024