Question: In this program, you are to design and implement the turtle graphics application. Turtle graphics was introduced in 1960s as a part of the Logo

In this program, you are to design and implement the turtle graphics application. Turtle graphics was introduced in 1960s as a part of the Logo language. Imagine a mechanical turtle that walks around the room holding a pen. The pen can be in one of two positions: up or down. If a pen is down, the turtle draws a line. If a pen is up, the turtle moves about without drawing anything. The turtle moves according to the commands.

It is mandatory that you create a turtle class in order to solve the problem, with a 2d array of characters representing the turtles world. Your turtle class needs to be compatible with TurtleDriver.java provided in Canvas. The set of turtle commands your application must support is:

Command

Meaning

Turtle.initializeRoom(x, y)

Create the initial room of x by y dimensions

Turtle.draw(outputStream)

Draw the room with all its turtles to the appropriate output stream

Turtle(n)

Create a turtle and assign the drawing character n

up()

Pen up

down()

Pen down

forward(n)

Move forward n spaces; if n spaces takes the turtle through the wall, the turtle stops at the wall

backward(n)

Move backward n spaces; if n spaces takes the turtle through the wall, the turtle stops at the wall

right()

Turn the turtle right 90 degrees

left()

Turn the turtle left 90 degrees

Additional requirements

When creating a turtle, always place it in the middle of the room, with its pen down, and facing to the right.

More than one turtle can occupy the room.

Initialize the room to blanks and keep track of each turtle, its position and direction, as well as the state of its pen at all times.

If more than one turtle happens to walk through the same cells on the grid with its pen down, it is the last turtles tracks that the 2d matrix ought to maintain, e.g. all the marks for the prior turtles walking through the same cells are erased.

Your class is to support proper encapsulation, i.e. client code should not be able to modify any class or object fields directly but only through the provided methods.

Remember that the instructor-provided driver does not cover all possible variations of the proper and improper turtle usage, i.e. test your code by modifying the provided driver and using print statements and the debugger.

Specifications

Name your turtle class file Turtle.java and include your name, course and section numbers, and project number inside your file as a comment. If your code is not fully functional, you need to provide appropriate comments in the TurtleDriver.java file and comment out the commands that do not work correctly.

Use appropriate comments, coding style, and meaningful identifiers throughout this program. Provide pre/postconditions by each method and a comment by each class field describing its purpose.

Your code should be organized in the following manner:

All fields are declared before all constructors.

All constructors are declared before all other methods.

All static fields are declared before all non-static fields.

All static methods are declared before all non-static methods.

Among the same type of entity (constructors, instance methods, static methods, instance fields, static fields), the order for information hiding modifiers should be public, then private.

This is the class Driver with main

public class TurtleDriver {

public static void main(String[] args) throws FileNotFoundException { PrintStream out1 = new PrintStream(new File("turtle.txt")); PrintStream out2 = System.out; Turtle.initializeRoom(40, 30); Turtle myt1 = new Turtle('+'); Turtle myt2 = new Turtle('#'); Turtle myt3 = new Turtle('*'); myt1.up(); myt1.backward(15); myt1.down(); drawSpiral(myt1); myt2.up(); myt2.forward(15); myt2.down(); drawSpiral(myt2); myt3.up(); myt3.right(); myt3.backward(10); myt3.down(); myt3.left(); drawRectangle(myt3, 5, 7); // Turtle.draw(out1); // Turtle.draw(out2); } public static void drawSpiral(Turtle myT) { for(int i = 1; i < 10; i++) { myT.forward(i); myT.right(); } } public static void drawRectangle(Turtle myT, int width, int height) { myT.backward(width); myT.left(); myT.forward(height); myT.right(); myT.forward(width); myT.left(); myT.backward(height); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!