Feb 04 & 05
Due: Tue Feb 10 before 4PM
The purpose of this lab is to implement a class that uses aggregation and composition. This lab also introduces the concept of recursion and uses your implemented class in a recursive method to draw a version of the Koch snowflake.
Turtle graphics is a method for drawing geometric primitives (such as points, lines, and curves) in the Cartesian (xy) plane. Logo was one of the early computer languages designed for educational purposes, and it had turtle graphics as one of its main features.
In turtle graphics, the client has control of a cursor (the turtle) that responds to commands such as "move forward 1.5 units", "turn right", "turn left", and "turn 45 degrees". As the turtle moves, it leaves behind a trail using a pen; the pen can have attributes such as colour and radius to control the trail colour and thickness.
The turtle needs attributes to represent its position in the plane,
the direction that it is facing, and the pen color. The position can be represented
using a Point2D
object, and the direction can be represented as an
angle greater than -360.0 and less than 360.0 degrees (measured counterclockwise from
the positive x axis). The pen color is represented using a
java.awt.Color
object. Your class should use composition
to manage the Point2D
object and aggregation to manage
the java.awt.Color
object.
Furthermore, our turtle will have a class invariant: The position of the
turtle is always inside or on the circle having radius equal to 1
.
If the turtle is asked to move outside of the circle,
then the turtle will move as far as possible in its current direction and
then stop at the perimeter of the circle.
Implement a Turtle
class that supports turtle graphics.
Use the code shown below to start your program. The code below uses
the StdDraw
class from the textbook
Introduction to Programming in Java:
An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
StdDraw
provides the capability to easily create simple
drawings in a Java program. In this lab, StdDraw
uses an Cartesian
coordinate system with the bottom left corner of the window having
coordinates (-1.0, -1.0) and the top right corner of the window having
coordinates (1.0, 1.0).
Below you will find several resources to help you with your implementation:
Turtle
. Make sure that you do not modify
any of the starter code given to you, as this make break the tester. The
starter code includes the following features:
toString
main
method that may help you test your implementationTurtle
, Point2D
, IVector2D
,
TurtleUtil
, and StdDraw
Point2D
, IVector2D
,
TurtleUtil
, and StdDraw
It is recommended that you complete Turtle
in the following
order:
Turtle(Point2D position, double angle, Color c)
Turtle(Turtle other)
getAngle()
getPenColor()
getPosition()
(remember to use composition)
setPenColor(Color c)
(remember to change the turtle's pen color and the pen color of StdDraw
)
turn(double delta)
turnLeft()
turnRight()
maxDistance()
(see note below)
move(double distance)
(see note below)
Note that you have three ways to test your code: the JUnit tester,
the Koch snowflake client program, and the main
method
of Turtle
.
If you examine the API of TurtleUtil
you will find a method
that helps you with Step 2.
For the move
method, you may find the methods
Vector2D.dirAndMag
and Turtle.maxDistance
to be useful in your implementation.
In the move
method, you need to draw a line using the
turtle's pen color. To draw a line from a point (x1, y1)
to a point (x2, y2)
using the turtle's current pen color,
use the following code:
StdDraw.setPenColor(this.penColor); StdDraw.line(x1, y1, x2, y2);
Submit your solution before Tuesday February 10, 4pm. To submit, log into a computer of the Prism lab (clips showing how to log in from home and transfer files can be found here). Transfer your Turtle.java to your EECS account (if it is not already there). Create a file named group.txt in the directory which contains the lab directory which contains the turtle directory which in turn contains Turtle.java. Do not put the group.txt in the same directory as Turtle.java. Each line of the file group.txt contains the EECS login name of a member of the group. Open a terminal and go to the directory that contains the file group.txt. In that directory, run the following command.
submit 1030 lab4 group.txt lab/turtle/Turtle.javaIf something fails, you will be see something like
submitted: group.txt (18 bytes) submitted: Turtle.java (4871 bytes) All files successfully submitted. Your Turtle class compiled successfully. Your Turtle class passed all the tests. Your code contains no style errors. frank in group.txt is not a valid EECS account SUMITTED FILES HAVE BEEN REMOVED. PLEASE SUBMIT AGAIN.Fix your code and submit again. If you successfully submitted your code, you will see something like this
submitted: group.txt (19 bytes) submitted: Turtle.java (4871 bytes) All files successfully submitted. Your Turtle class compiled successfully. Your Turtle class passed all the tests. Your code contains no style errors. Your group.txt file has been successfully validated.