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)

I realise you say it isn't printing it out correctly but have you worked through your logic manually yet? P.S. What exactly are turtle graphics?

I realise you say it isn't printing it out correctly but have you worked through your logic manually yet? P.S. What exactly are turtle graphics?

Turtle Graphics is a special Java Language. Used to create Graphics.

I dont understand what you mean by worked through my logic manually ?

Oh perhaps I should have googled/binged it. I mean write it down - pen to paper, that's how I was taught. It's like working through a for loop - best way to find errors or something unexpected is to work through it yourself.

P.S. What output are you getting?

Oh perhaps I should have googled/binged it. I mean write it down - pen to paper, that's how I was taught. It's like working through a for loop - best way to find errors or something unexpected is to work through it yourself.

P.S. What output are you getting?

everything builds and compiles successfully but the output only shows


The drawing is: with no image

How are you calling your methods? What order? Most importantly how is executeCommands() getting input?

Yes my logic is fine I just cant seem to get the last part where am supposed to use a display method to show the image once the program runs successfully.

maybe you're not setting the values in floor right.
if you declare it as an empty two-dimensional array, and never changes the values, it's normal that if you print it, you don't see anything.

I got it to print so far thanks for the help. Any Ideas on how to print a rectangle made of asteriks so far this is what it outputs
run:
Enter command (9 to end input): 2
Enter command (9 to end input): 5
Enter forward spaces: Enter command (9 to end input): 12
Enter command (9 to end input): 3
Enter command (9 to end input): 5
Enter forward spaces: Enter command (9 to end input): 12
Enter command (9 to end input): 3
Enter command (9 to end input): 5
Enter forward spaces: Enter command (9 to end input): 12
Enter command (9 to end input): 1
Enter command (9 to end input): 6
Enter command (9 to end input): 9

The drawing is:

--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------

Here is my code

// 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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.