Hi
Am currently taking a Java class and we are supposed to use turtle graphics to draw the following shape rectange made of Asterix
*************
* *
* *
* *
* *
* *
* *
* *
*************
My program builds and compiles successfully but I cant get it to display the array.
// Exercise 7.21: TurtleGraphics.java
// Drawing turtle graphics based on turtle commands.
import java.util.Scanner;
public class TurtleGraphics
{
private static final int MAXCOMMANDS = 100; // maximum size of command array
private static final int SIZE = 20; // size of the drawing area
private int[][] floor; // array representing the floor
private int[][] commandArray; // list of commands
private int count; // the current number of commands
private int xPos; // the x Position of the turtle
private int yPos; // the y Position of the turtle
// enters the commands for the turtle graphics
public void enterCommands()
{
Scanner input = new Scanner( System.in );
count = 0;
commandArray = new int[ MAXCOMMANDS ][ 2 ];
floor = new int[ SIZE ][ SIZE ];
System.out.print( "Enter command (9 to end input): " );
int inputCommand = input.nextInt();
while ( inputCommand != 9 && count < MAXCOMMANDS )
{
commandArray[ count ][ 0 ] = inputCommand;
// prompt for forward spaces
if ( inputCommand == 5 )
{
System.out.print( "Enter forward spaces: " );
// To do - get input and save it to
//commandArray[ count ][ 1 ]
} // end if
count++;
System.out.print( "Enter command (9 to end input): " );
inputCommand = input.nextInt();
} // end while
} // end method enterCommands
// executes the commands in the command array
public void executeCommands()
{
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 = commandArray[ 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
// To do -
break;
case 3: // turn right
direction = turnRight( direction );
break;
case 4: // turn left
// To do -
break;
case 5: // move
distance = commandArray[ commandNumber ][ 1 ];
movePen( penDown, floor, direction, distance );
break;
case 6: // display the drawing
System.out.println( "\nThe drawing is:\n" );
printArray( floor );
break;
} // end switch
command = commandArray[ ++commandNumber ][ 0 ];
} // end while
} // end method executeCommands
// method to turn turtle to the right
public int turnRight( int d )
{
return ++d > 3 ? 0 : d;
} // end method turnRight
// method to turn turtle to the left
public int turnLeft( int d )
{
return --d < 0 ? 3: d;
// To do
} // end method turnLeft
// method to move the pen
public 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 do
break;
case 2: // move to left
// To do
break;
case 3: // move up
// To do
break;
} // end switch
} // end method movePen
// method to print array drawing
public void printArray( int[][] a )
{
// display array
for ( int i = 0; i < SIZE; ++i )
{
for ( int j = 0; j < SIZE; ++j )
System.out.print( ( a[ i ][ j ] == 1 ? "*" : " " ) );
System.out.println();
} // end for
} // end method printArray
} // end class TurtleGraphics
Command Meaning
1 Pen up
2 Pen down
3 Turn right
4 Turn left
5,10 Move forward 10 spaces (replace 10 for a different number of
spaces)
6 Display the 20-by-20 array
9 End of data (sentinel)