EECS2030 Lab 3

Thu Oct 6, Tue Oct 11, Mon Oct 16

Due date:
ALL SECTIONS: Tue Oct 18 before 23:59

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 dragon curve.

Question 1: Implement a turtle class

This lab has you implement the Turtle class that was used in Lab 2 to draw the Koch snowflake. Your implementation will use the Point2 and Vector2 classes from Lab 2, which we provide for you in the form of a jar file.

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 fields such as colour and radius to control the trail colour and thickness.

The turtle needs fields to represent its position in the plane, the direction that it is facing, and the pen color. The position can be represented using a Point2 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 Point2 object and aggregation to manage the java.awt.Color object.

Implement a Turtle class that supports turtle graphics. Our implementation 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).

Getting started

Here are some resources needed for this lab:

  1. In a Java project, create a package named eecs2030.lab3
  2. Create a new class named Turtle and copy the code shown below into the newly created class.
  3. Add the jar file to the project (under the Project menu, choose Properties; under Java Build Path choose Add External JARs... and select the lab3.jar file)
  4. In the code given to you, there are several examples of methods documented with Javadoc comments. Study the examples to see how to document method parameters, method return values, and thrown exceptions. Also, observe that new paragraphs in Javadoc start with the HTML paragraph tab <p>
  5. To generate the documentation for your class, under the Project menu, choose Generate Javadoc.... In the window that appears, make sure that your Turtle class is selected and set the directory where you want the generated documentation to be saved to:
  6. Press Finish. If there are no errors in your Javadoc, then you should be able to open the file index.html in the directory you specified in the previous step.
  7. Complete the implementation of the Turtle class. We would like you to include Javadoc for all of your constructors (you may include Javadoc for your other methods, but this is not required). You should re-generate the documentation following steps 5 and 6 and check that your documentation matches that provided to you in this lab.

    In the method named move you need to compute the point where the turtle stops after moving the given distance, and then set the turtle's position to this point. You can use methods in Vector2 and Point2 to help you compute the stopping point (Hint: the turtle knows what direction it is facing in and how far it was asked to move; Vector2 contains a method to create a unit vector pointing in a given direction, and another method that can scale the length of vector. Point2 has a method that lets you add a vector to the point causing the point to move).

    Once you have set the turtle's new position, you need to ask StdDraw to draw a line between the turtle's original position and its new position using the turtle's current pen color. The methods StdDraw.setPenColor and StdDraw.line will allow you to draw the correctly colored line.
  8. Test your program using the client program found at the last link shown above (DragonCurve.java).

package eecs2030.lab3;

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

/**
 * A class that supports turtle graphics. A turtle moves between two points in a
 * straight line drawing the line as it moves.
 * 
 * <p>
 * A turtle has-a <code>Point2</code> instance that represents the position of
 * the turtle, and a <code>Color</code> instance that represents the current pen
 * color. The turtle maintains ownership of its position at all times.
 * 
 * 
 * @author EECS2030 Fall 2016-17
 * 
 */
public class Turtle {
    private Point2 position;
    private double angle;
    private Color penColor;

    
    
    /**
     * Sets the pen color.
     * 
     * @param c
     *            the new pen color
     * @throws IllegalArgumentException
     *            if the pen color c is null
     */
    public void setPenColor(Color c) {
        
    }

    /**
     * Compares this turtle with another object for equality. This turtle
     * can be equal to only other turtles.
     * 
     * <p>
     * Two turtles are equal if their positions, directions, and pen colors
     * are all equal; otherwise, the turtles are not equal.
     * 
     * @param obj
     *           An object to compare for equality.
     * @return 
     *           True if the position, direction, and pen color of this turtle
     *           are equal to the position, direction, and pen color of the other
     *           turtle, and false otherwise.
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        Turtle other = (Turtle) obj;
        if (Double.doubleToLongBits(angle) != Double.doubleToLongBits(other.angle)) {
            return false;
        }
        else if (!penColor.equals(other.penColor)) {
            return false;
        } 
        else if (!position.equals(other.position)) {
            return false;
        }
        return true;
    }
    
    /**
     * Returns a hash code for this turtle.
     * 
     * @return a hash code for this turtle 
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(angle);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + ((penColor == null) ? 0 : penColor.hashCode());
        result = prime * result + ((position == null) ? 0 : position.hashCode());
        return result;
    }
    
    /**
     * Returns a string representation of this turtle. The string representation
     * is:
     * 
     * <ol>
     * <li>the position of the turtle (as given by <code>Point2.toString</code>),
     * followed by
     * <li>a comma and a space, followed by
     * <li>the direction, followed by
     * <li>a space, the string "degrees", a space, and a comma, followed by
     * <li>the pen color (as given by <code>Color.toString</code>)
     * </ol>
     * 
     * @return a string representation of this turtle
     */
    @Override
    public String toString() {
        String s = String.format("%s, %f degrees, %s",
                this.getPosition(), this.getAngle(), this.getPenColor());
        return s;
    }

}

Submitting

Submit your program when you are finished:

submit 2030 lab3 Turtle.java

or if you are working in a group:

submit 2030 lab3 Turtle.java group.txt

Instructions for submitting from outside the lab can be found here.