Turtles Assignment 2

Table of Contents



Now that you have completed the first assignment, you have a good idea of the basic building blocks of a Java program. You know how to create ("declare") a variable, how to create ("instantiate") a new object, how to "assign" a variable to refer to an object, and how to call a method of an object, possibly giving parameters.

Now, you need to take your first big step into how programming works. Modern programming is said to be "Object-Oriented" because it is structured around "objects" with particular programmed behavior that work together to get a task done.

It is time for you to create your first Object class.

The things that a Turtle knows how to do are fairly basic. If we want to draw a square, we have to give it eight instructions: four "rt()" instructions and four "move()" instructions. I think it wouldn't be unreasonable to expect that there would be some kind of Turtle out there that can just be told to "square()" and it would do it.

1. Implementing basic methods

You are going to create a new class of Turtle called an Artist. An Artist should be able to do everything that a Turtle can do, but it should extend these capabilities by adding a few new methods.

In programming terms, we would say that the Artist class "extends" the Turtle class. Or we might say that Artist is a "subclass" of Turtle, and Turtle is the "superclass" of Artist.

Open up the file "Artist.java". At the top of the file, after the standard preliminar stuff, it says that the file is defining a class calles Artist that extends Turtle. The rest of the file contains a definition of what methods the Artist class adds.

Take a look at the sample method, rightTriangle(). You will notice a few things:

The "declaration" of the method calls it a "public void". The "public" means that this is a method that can be used by other objects. A "private" method would be only usable within the object, as a helper method for other public methods. The "void" says that it doesn't "return" any information to the object that called it; we'll learn more about return values in the next assignment.

Within a class definition, you can call other methods of this object as simply [method](), not [variable].[method](). (Actually, if you wanted to use the [variable].[method]() syntax, there is a special variable "this" that refers to the curent object. So, I could say "this.move()" and mean the same as "move()."

Write the first three methods of Artist. For each one, you should put the pen down, draw the shape, and raise the pen again, and you should end up in the same place, facing the same way, as you were before the method was called.
square()triangle()cross()

For the cross() method, be sure that you draw it starting at the point where the turtle is shown in the picture above. When writing that method, also notice that there is a simple pattern of moves that you repeat 4 times. It would be smart to create another method that does that set of moves; you could declare that method to be private, since it is only there to help other methods in the class. Then, you could call that method four times, instead of having four copies of the same instructions in cross().

When you are done writing these methods, compile Artist. Then, in Controller.java, delete whatever is in the run() method at the moment and write in this:

public void run() {
Assignment2.part1();
}

This will run the test for assignment 2, part 1. The test (which you can look at in Assignment2.java) just creates a new Artist and has it draw one each of the three shapes, about one step apart. You need to compile Controller.java after making this change.

Run the applet. If you did everything right, you should see all three shapes appear, as seen above, with no lines connecting them. When you've worked out all your bugs, go on to the next part.

2. Writing Methods with Parameters

Now you've seen the other side of methods. You knew how to call them, now you know how to define what happens when a method is called. The last thing you need to know how to do is to deal with method parameters.

Parameters to a method act just like variables. You list off in the parentheses in the method declaration what the parameters are, just as you would declare a varaible. Then, you can use that parameter name as a variable within that method.

You should know a bit about some common types of parameters. The most common two are "int" and "double." A double represents a number with a decimal value, such as 2.25; an int has to be a integer value. Both are allowed to be either positive or negative.

Notice also that there are now two methods called "square" in your artist class. When square() is called, Java decides which to use by matching up the parameters. We sometimes refer to the new one as square(double) to distinguish it from the other. (This is called the "signature" of the method. (So, the signature of the new triangle() method is "triangle(double, Color)")

Write the methods square(double) and triangle(double, Color), following the guidance of the comments above each. Recompile Artist.java (recompile means "compile again"). Then, change the Controller to call the method part2() of Assignment2, and recompile Controller.java. Then run the applet. You should see the turtle draw some sort of biohazard-like symbol. When you've worked out any bugs, go on to the last part.

3. Using Loops

Open up the file "Assignment2.java", and take a look at that last test that you ran. I wanted to repeat a certain process three times: draw a large triangle, then a tiny square, then a small triangle, then another tiny square. Instead of writing the code and then copying and pasting it three times, I made use of a Java instruction that will repeat some code for me.

A "while" loop looks like this:

while(condition) {
   Some statements...
}

There is a block of code, enclosed in curly brackets, that will continue to be executed over and over again as long as the condition is true. This condition is some statement that the computer can determine to be either true or false just before it starts each repetition of the loop. So, for example, a boxer might want to execute the following code:

while(opponent.isStillStanding()) {
   punchInTheFace(opponent);
}

In the loop in Assignment2.test2(), we create a variable called i, originally equal to 1. At the start of each repetition of the while loop, the computer checks whether i is still less than or equal to three. If so, it executes the block of code in the loop, which ends by increasing i by 1. Once i gets to be 4, the loop will stop running, and the program will continue to the next statement after the loop (t.die(), in this case).
polygon(6)polygons(6)

Using this same type of loop, using a variable to count how many times you have done something, you can repeat any given task a set number of times. Use this to write the method Artist.polygon(int), which draws a polygon with the given number of sides. (You will have to move, then turn, sides times, and the size of each turn will be determined by sides).

Now, write the method polygons(int), which draws a sequence of polygons, all starting from the same place, with the smallest being a triangle and the largest having the given number of sides.

Your final task for this assignment is to write a new version of cross(). This time, you pass it an integer parameter saying how many "steps" to draw in between the each of the four outside sides. Some examples are shown below:
cross(0)cross(1)cross(2)cross(3)cross(14)

Recompile Artist.java. Change Controller.run() to call Assignment2.part3(), and recompile Controller.java. When you run the applet now, you should see the sketch shown below:

Fix any mistakes you might notice. Then, be sure to include your name and the date in the header at the top of Artist.java, and print it out and hand it in.

4. Extras

4.1. Some Timesavers

Increment and decrement operators: You will often find yourself using some sort of loop that increases or decreases a variable by one each time by doing something along the lines of "i = 1 + i;" at the end of the loop. This is called "incrementing" the variable (the opposite is called "decrementing"), and there is a simpler way of doing it:

i++; // Add 1 to i.
i--; // Subtract 1 from i.

The interesting thing about the "++" and "--" operators is that they can appear next to that variable any place that it appears. So, I might want to save myself a line of code in polygons() by having the inside of the loop just say "polygon(i++);"

Of course, when you do this, you should wonder whether the polygon drawn will be one with i sides, or one with i + 1 sides. It turns out that it will have i sides; i isn't incremented until after its value has been used in the statement. If you want the increment or decrement to happen before the value is used, you would put the operator before the variable name: "++i" or "--i".

Code reuse: This assignment asked you to write several different versions of square(), triangle(), and cross() to deal with the possibility of different parameters. Thus, you have code in two different places that does basically the same thing. To save yourself time, and to make it easier to find and fix bugs, you would usually write the square(double) method first, and then simply have square() call square(1.). Similarly, cross() could just call cross(1). For that matter, you could write a cross() that takes both a side length and a number of steps, and then all the other cross() and square() methods could be written just calling it.

As you were doing this consolidation of method code, you would realize that triangle() can't be written in terms of triangle(double, Color), becase triangle(double, Color) has to change the pen color and triangle() is not allowed to. It is not always the most complicated method of a type that you should write first; here, you would want to write triangle(double side), and then base the other two off that, one adding something to it, one calling a special case of it.

back to Computer Programming Assignments.