CSE1030Z Lab 05

Thu Feb 07 2:30-4:00 PM

Introduction

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.

There is one question in this lab, which should be submitted by the end of the day.

Question 1: Implement a turtle class

This is a modified version of a question from the Week 03 homework questions; it is also slightly different than the Wednesday lab.

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 between -360.0 and 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.

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).

Start with the code shown below. All of the Javadoc, attributes, and method headers have been created for you; you just need to fill in the method bodies.

Test your program using the client program found at the last link shown above. Submit your program when you are finished:

submit 1030Z L5R Turtle2.java
package cse1030;

import princeton.introcs.StdDraw;
import java.awt.Color;

/**
 * A class that supports turtle graphics.
 * 
 * @author CSE1030Z
 * 
 */
public class Turtle2 {
  private Point2D position;
  private double angle;
  private Color penColor;

  /**
   * Create a turtle at location <code>(0.5, 0.5)</code> with an angle of
   * <code>0.0</code> degrees and a pen color of <code>Color.BLACK</code>.
   */
  public Turtle2() {

  }

  /**
   * Create a turtle from another turtle. The created turtle has the same
   * position, angle, and pen color as the other turtle.
   * 
   * @param other
   *          the turtle to copy
   */
  public Turtle2(Turtle2 other) {

  }

  /**
   * Create a turtle at location <code>position</code> with an angle of
   * <code>0.0</code> degrees and a pen color of <code>Color.BLACK</code>. The
   * starting position must be inside the square with corners
   * <code>(0.0, 0.0)</code> and <code>(1.0, 1.0)</code>, otherwise an
   * <code>IllegalArgumentException</code> will be thrown.
   * 
   * @param position
   *          the starting position of the turtle
   * @throws <code>IllegalArgumentException</code> if the starting position is
   *         not in the square with corners <code>(0.0, 0.0)</code> and
   *         <code>(1.0, 1.0)</code>
   */
  public Turtle2(Point2D position) {

  }

  /**
   * Create a turtle at location <code>position</code> with an angle of
   * <code>angle</code> degrees and a pen color of <code>Color.BLACK</code>. The
   * starting position must be inside the square with corners
   * <code>(0.0, 0.0)</code> and <code>(1.0, 1.0)</code>, otherwise an
   * <code>IllegalArgumentException</code> will be thrown.
   * 
   * @param position
   *          the starting position of the turtle
   * @param angle
   *          the angle in degrees from the x axis that the turtle is facing in
   * @throws <code>IllegalArgumentException</code> if the starting position is
   *         not in the square with corners <code>(0.0, 0.0)</code> and
   *         <code>(1.0, 1.0)</code>
   */
  public Turtle2(Point2D position, double angle) {

  }

  /**
   * Create a turtle with the given starting position, angle, and pen color. The
   * starting position must be inside the square with corners
   * <code>(0.0, 0.0)</code> and <code>(1.0, 1.0)</code>, otherwise an
   * <code>IllegalArgumentException</code> will be thrown.
   * 
   * @param position
   *          the starting position of the turtle
   * @param angle
   *          the angle in degrees from the x axis that the turtle is facing in
   * @param c
   *          the pen color
   * @throws <code>IllegalArgumentException</code> if the starting position is
   *         not in the square with corners <code>(0.0, 0.0)</code> and
   *         <code>(1.0, 1.0)</code>
   */
  public Turtle2(Point2D position, double angle, Color c) {

  }

  /**
   * Moves the turtle by a given distance in the direction the turtle is
   * currently facing. A line is drawn as the turtle moves to the new position.
   * The distance can be negative, in which case the turtle moves backwards in
   * the direction opposite to the direction it is currently facing in.
   * 
   * @param distance
   *          the distance to move
   */
  public void move(double distance) {
    // NOTE: first compute the final position of the turtle
    // (there are methods in Vector2D that might help you!);
    // then use StdDraw.setPenColor to set the pen color of the drawing;
    // then use StdDraw.move to draw the line;
    // then set this.position to the final position of the turtle
  }

  /**
   * Turns the turtle to the left, increasing its angle by 90.0 degrees.
   */
  public void turnLeft() {

  }

  /**
   * Turns the turtle to the right, decreasing its angle by 90.0 degrees.
   */
  public void turnRight() {

  }

  /**
   * Turns the turtle by the specified amount in degrees. A positive
   * <code>delta</code> turns the turtle to the left (counterclockwise) and the
   * negative <code>delta</code> turns the turtle to the right (clockwise). The
   * angle of the turtle is always corrected to lie within the range of
   * <code>-360</code> degrees and <code>+360</code> degrees.
   * 
   * @param delta
   *          the amount by which to turn the turtle
   */
  public void turn(double delta) {
    // NOTE: this method needs to set the new value for this.angle;
    // then it needs to make sure that this.angle is in the range -360 to 360;
    // consider using the % operator
    
  }

  /**
   * Sets the pen color.
   * 
   * @param c
   *          the new pen color
   */
  public void setPenColor(Color c) {
    // NOTE: this method sets the pen color stored by the turtle;
    // you do not need to use StdDraw.setPenColor here!
  }

  /**
   * Gets the current pen color.
   * 
   * @return the current pen color
   */
  public Color getPenColor() {

  }

  /**
   * Gets the current position of the turtle. The client cannot change the
   * position of the turtle using the <code>Point2D</code> returned by this
   * method. To move the turtle the client must use <code>move</code>.
   * 
   * @return the current position of the turtle
   */
  public Point2D getPosition() {

  }

  /**
   * Gets the direction that the turtle is facing in as an angle measured from
   * the x axis. The angle of the turtle is always in the range of
   * <code>-360</code> degrees and <code>+360</code> degrees.
   * 
   * @return the angle measured in degrees from the x axis that the turtle is
   *         facing
   */
  public double getAngle() {

  }

}