CSE1030 Lab 04

Week of Feb 03
Due: Mon Feb 10 before 11:59PM


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.

Question 1: Implement a turtle class

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 Vector2D 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 Vector2D object and aggregation to manage the java.awt.Color object.

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. StdDraw uses an Cartesian coordinate system with the bottom left corner of the window having coordinates (0.0, 0.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:

For the move method, you may find the methods Vector2D.dirVector 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.line(x1, y1, x2, y2);


Submit your work using the following command in the directory where your Turtle.java file is saved.

submit 1030 L4 Turtle.java

or use the web-based submit https://webapp.eecs.yorku.ca/submit/