Turtles Assignment 3

This assignment introduces several new and important concepts, and will require you to write some fairly tricky code. After you have completed this assignment, you will have a basic grasp of Java sufficient for more complicated tasks, such as writing a game.

Often in computer programming, you will learn how to do some new thing by a "case study", a study of a sample problem and its solution that introduces the needed capabilities as the solution requires them. In order to do this assignment, you should read the Turtle Plotter Case Study.

1. The Assignment

In this final assignment, your task is to create a new class, called an Alternator, which is a turtle that automatically changes the color it is drawing in every time it draws a line. An Alternator is given two colors when it is constructed, and it switches between these every time a move() is done.

To create a new class in JCreator in the Turtles project, select File:New:Class.... In the dialog that pops up, type the name of your new class (Alternator). You also should tell JCreator that the superclass of Alternator is Turtle (since Alternator is a special type of turtle, inheritting all the capabilities of a Turtle and extending them with new methods of its own). Finally, you need to specify that Alternator is part of the package turtles. Be careful of uppercase / lowercase here.

When you click Finish a new source file will appear. All it has in it is the declaration of the package and class. Notice that Alternator is declared to extend Turtle. You will need to add an import statement under the package declaration so that your class can use color:

import java.awt.Color;

Next, you will want to create two instance variables for your class. These should be declared inside of the class's block, but outside of all methods. Look at the "itsMeDoingThat" variable in Plotter to see how this works.

Next, you will want to create a constructor. Any method named after your class is a constructor; you will want to declare yours as public Alternator(Color c1, Color c2) or something of the sort. Be sure to call super() as the first thing in your constructor, to set yourself up as a Turtle before you start adding the Alternator pieces. Here, I have given the constructor two parameters, so that when I construct the Alternator (in some code elsewhere, like in Controller) I can give it the two colors it will alternate between:

Alternator a = new Alternator(Color.RED, Color.BLUE);

Finally, you will have to override the move(double) method of Turtle, so that it changes the color before moving. Be sure to copy the method declaration exactly, so that you are overriding the old method rather than making a new one with a different name:

public void move(double distance) {
}

Since you will have to use penDown() to change the color, your Alternator won't be able to move without drawing. This was bad design on my part. If you want to fix this, add this method to the Turtle class:

public void setColor(Color c) {
   color = c;
}

With this, you can use the setColor() method of Turtle to change the color without putting the pen down.

Once you have this working, you may choose to do the next part, which requires you to do something slightly different with your Alternator class. Or, you can just go on to Asteroids; you should know most of what you need for it.

2. The Modified Assignment

This part is optional, for only if you want to try it.

In real-world programming, the task you are told to do at first never wuite satisfies your boss. They always want some slight change made. Unfortunately, the change you have to make here is a lot more complex than the code you've written so far.

I want you to change the alternator class so that, instead of switching color every move(), it switches color every time that it has gone forward a distance of 1.

This may mean that a single move is split up into several, with penDown() instructions in between.

It may also mean that you draw several short lines before their lengths add up to make 1.

You can talk to me for hints, but I want you to have a chance to try to work it out on your own.

back to Computer Programming Assignments.