Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im very confused on how to finish this program: The significant methods of the TurtleGraphics class A turtle program is a sequence of commands that

Im very confused on how to finish this program:

The significant methods of the TurtleGraphics class

A turtle program is a sequence of commands that directs the movements and actions of a mechanical turtle as described below:

The enterCommands() method acts as the compiler of a turtle program. Each call to this method enters a new program. The signature and return value of the method is:

public int enterCommands(int[][] cmnds)

When the enterCommands() method is invoked, the method should analyze the the values to see that they are only recognizable and valid commands, and the program is terminated properly. Generate error messages for any problem recognized in the program. The enterCommands() method should transform the program into a more convenient representation, i.e., more convenient than having to re-analyze the commands read from the user, and at the same time store this more convenient representation in an internal structure ready for execution (hint: class TurtleGraphics may define an int[][] to represent the "compiled" commands). If the Turtle Graphics program has been successfully entered, the method will return a true, otherwise a false is returned.

The program that was entered may be executed, by calling the executeCommands() method. The signature and return value of the method should be:

public boolean executeCommands(int[][] cmnds, int count)

This method does not need any parameters, as it will execute the program previously processed and stored in the data field by the enterCommands method. Executing the program actually does the work to move the turtle over the class data field floor and as it moves, if its pen is down, it makes a mark, otherwise it just continues moving and leaves whatever is already on the floor undisturbed and unchanged. The executeCommands() returns a value of false if there is no valid program to execute.

Assume that the turtle drawing is in a box that has a floor 20 units by 20 units in size (as mentioned in the description in the textbook). You yourself may specify what you want the behavior to be when the turtle gets to the edge of the box (since this is not mentioned in the problem descriptionyour choicebut you must write some commentary to say what you have decided the turtle will do when reaching a boundary, so it may be determined whether you have done it correctly).

When a display (6) instruction is found in the program, the current state of the floor is rendered to the standard output. (* is suggested in the textbook as a character to represent a mark on the floor, but two characters such as * works better.)

Format of the Turtle Program

Commas, and end-of-line, blank lines have no significance in a turtle program (just like in a Java program) other than to separate the numbers that make up the commands and distances. A single comma immediately after a number is like a single space. A comma may be replaced by a single space, and the program will be unchanged, and will produce exactly the same drawing. The single comma is allowed in a turtle program just for visual effect, intended to make it easier to read the program when a command has a related move value. It does not have to be there, so need not be checked. All of the five following programs (that draw the box shown in the textbook) are equivalent.

2 5,12 3 5,12 3 5,12 3 5,12 1 6 9

2 5 12 3 5 12 3 5 12 3 5,12 1 6 9

2 5 12 3 5 12 3 5 12 3 5 12 1 6 9

2, 5,12 3, 5,12 3, 5,12 3, 5,12 1, 6, 9,

2, 5 12 3, 5, 12, 3 5, 12, 3 5 12 1 6 9

Hint: how does the class Scanner method useDelimiter() help?

public class TurtleGraphics { private static final int SIZE = 20; // size of the drawing area private static int[][] floor; // array representing the floor private static int xPos; // the x Position of the turtle private static int yPos; // the y Position of the turtle // enters the commands for the turtle graphics public static void main(String[] args) { new TurtleGraphics().run(); // launch the app } public void run(){ final int MAXCOMMANDS = 100; // maximum size of command array int[][] commandArray = new int[MAXCOMMANDS][2]; int count; count = enterCommands(commandArray); if(!executeCommands(commandArray,count)) System.out.println("Invalid command detected"); } //enter the turtle commands public void enterCommands(int[][] cmnds){ int count = 0; // the current number of commands Scanner input = new Scanner(System.in); input.useDelimiter(",| "); floor = new int[SIZE][SIZE]; System.out.print("Enter command (9 to end input): "); int inputCommand = input.nextInt(); while (inputCommand != 9 && count < cmnds.length) { cmnds[count][0] = inputCommand; // prompt for forward spaces if (inputCommand == 5) cmnds[count][1] = input.nextInt(); count++; System.out.print("Enter command (9 to end input): "); inputCommand = input.nextInt(); } } // executes the commands in the command array public boolean executeCommands(int[][] cmnds, int count) { int commandNumber = 0; // the current position in the array int direction = 0; // the direction the turtle is facing int distance = 0; // the distance the turtle will travel int command; // the current command boolean penDown = false; // whether the pen is up or down xPos = 0; yPos = 0; command = cmnds[commandNumber][0]; // continue executing commands until either reach the end // or reach the max commands while (commandNumber < count) { //System.out.println("Executing..."); // determine what command was entered // and perform desired action switch (command) { case 1: // pen down penDown = false; break; case 2: // pen up penDown = true; break; case 3: // turn right direction = turnRight(direction); break; case 4: // turn left direction = turnLeft(direction); break; case 5: // move distance = cmnds[commandNumber][1]; movePen(penDown, floor, direction, distance); break; case 6: // display the drawing System.out.println(" The drawing is: "); printArray(floor); break; default: return false; } command = cmnds[++commandNumber][0]; } return true; } // method to turn turtle to the right public static int turnRight(int d) { // To be completed by student. return Integer.MAX_VALUE; } // method to turn turtle to the left public static int turnLeft(int d) { // To be completed by student. return Integer.MAX_VALUE; } // method to move the pen public static void movePen(boolean down, int a[][], int dir, int dist) { int j; // looping variable // determine which way to move pen switch (dir) { case 0: // move to right for (j = 1; j <= dist && yPos + j < SIZE; ++j) if (down) a[xPos][yPos + j] = 1; yPos += j - 1; break; case 1: // move down // To be completed by student. break; case 2: // move to left // To be completed by student. break; case 3: // move up for (j = 1; j <= dist && xPos - j >= 0; ++j) if (down) a[xPos - j][yPos] = 1; xPos -= j - 1; break; } } // method to print array drawing public static void printArray(int[][] a) { // display array for (int i = 0; i < SIZE; ++i) { // To be completed by student. System.out.println(); } } } // end class TurtleGraphics 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Real Time Analytics In Big Data A Comprehensive Guide For Everyone

Authors: Lennox Mark

1st Edition

B0CPTC9LY9, 979-8869045706

More Books

Students also viewed these Databases questions