Results 1 to 12 of 12
  1. #1
    jmc724 is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default Turtle assignment

    I have no idea of what is required in doing a turtle assignment. But here is the code so far:

    // TurtleGraphics.java

    import java.awt.Color;
    import java.*;
    import javax.swing.JPanel;
    import java.util.Scanner;


    public class TurtleGraphics
    {


    int xpos=0, ypos=0; //turtle always starts at 0,0 position
    final static int penUp = 1;
    final static int penDown = 2;
    final static int turnRight = 3;
    final static int turnLeft = 4;
    final static int moveFwd = 5;
    //final static int moveFwd2 =12;
    final static int print = 6;
    final static int end = 9;


    int commandArray [] =

    {
    penDown,
    moveFwd,
    12,
    turnRight,
    moveFwd,
    12,
    turnRight,
    moveFwd,
    12,
    turnRight,
    moveFwd,
    12,
    penUp,
    turnRight,
    moveFwd,
    6,
    penDown,
    turnRight,
    moveFwd,
    11,
    print,
    end
    };

    boolean penPosition;
    boolean pen_down = true;
    boolean pen_up = true;



    // enters the commands for the turtle graphics
    public void enterCommands()
    {
    Scanner input = new Scanner( System.in );

    int MAXCOMMANDS [];
    int count[];
    int commandNumber;
    int command[];

    int inputCommand []= new int [6]; //array of inputCommand
    //int inputCommand[] = {2,5,12,3,5,12,3,5,12,3,5,12,1,6,9};

    //Enter command (9 ) to end input;

    System.out.printf(" 1. Pen Up\n"); //prints out header of the exercise
    System.out.printf(" 2. Pen down\n"); //prints out header of the exercise
    System.out.printf(" 3. Turn right\n"); //prints out header of the exercise
    System.out.printf(" 4. Turn left\n"); //prints out header of the exercise
    System.out.printf(" 5. Move forward\n"); //prints out header of the exercise
    System.out.printf(" 6. Print\n"); //prints out header of the exercise
    System.out.printf(" 9. End\n"); //prints out header of the exercise
    // System.out.print("Please enter a command: "); //prompt to enter first number

    while ( inputCommand != 9 && count < MAXCOMMANDS )
    {

    commandArray[ count ][ 0 ] = inputCommand;

    // prompt for forward spaces

    count++;

    System.out.print( "Enter command (9 to end input): " );
    inputCommand = input.nextInt();
    } // end while

    executeCommands();
    } // end method enterCommands


    // executes the commands in the command array
    public void executeCommands()
    {

    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
    boolean penUp;
    boolean penDown;
    boolean turnRight;
    boolean turnLeft;
    boolean moveFwd;
    //boolean moveFwd2;
    boolean print;
    boolean end;

    switch ( command )
    {

    case 1: // pen up
    penUp = false;
    break;

    case 2: // pen down
    penDown = false;
    break;

    case 3: // turn right
    turnRight = false;
    break;

    case 4: // turn left
    turnLeft = false;
    break;

    case 5: // move forward 12 spaces
    moveFwd = false;
    //moveFwd2 = false;
    break;

    case 6: // display 20x20 array
    print = false;
    break;

    case 7: // end
    end = false;
    break;


    } // end switch

    command = commandArray[ ++commandNumber ][ 0 ];
    } // end while
    } // end method executeCommands

    // method to turn turtle to the right

    // public void getturnRight() // need to know how to move 45 degrees.
    // {
    // return turnRight;
    // }



    // method to turn turtle to the left

    // public void getturnLeft() // need to know how to move 45 degrees.
    // {
    // return turnLeft;
    // }


    // method to move the pen
    public boolean getPenPosition()//check to see if pen is up
    {
    return penPosition;
    }


    public boolean pickPenUp()
    {
    penPosition = pen_up;
    }

    //public boolean getPenPosition() //check to see if pen is down
    // {
    // return penPosition;
    // }


    public boolean putPenDown()
    {
    penPosition = pen_down;
    }

    // method to print array drawing
    } // end class TurtleGraphics

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What you want to do and where you stuck with? It's better to explain your question well. I don't think anyone wants to run your code and find errors for you.

  3. #3
    jmc724 is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    The turtle must be able to carry out all the commands, the turtle must be able to move freely about without drawing anything.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What programming problem(s) are you having?
    Do you get compile errors?
    Do you get runtime errors?
    If either, copy the FULL text of the error message and post it here.

    Does your program NOT do what you want? Tell us what it does do and how you want to change it.

    For all the possible commands you need to design what each means and how to code it.
    For example what does penDown mean?
    YOu need to define the contents of the commandArray, field by field.
    How is a command selected?

    Can you ask your instructor for more info on how to do the assignment?
    Last edited by Norm; 07-15-2008 at 11:26 PM.

  5. #5
    jmc724 is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    7.21 (Turtle Graphics) The Logo language, which is popular among young computer users, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a Java program. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem you will simulate the operation of the turtle and create a computerized sketchpad as well.

    Use a 20-by-20 array floor which is initialized to zeros. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position 0,0 of the floor with its pen up. The set of turtle commands your program must process are as follows:

    Command Meaning
    1 Pen up
    2 Pen down
    3 Turn right
    4 Turn left
    5,10 Move forward 10 spaces (or a number other than 10)
    6 Print the 20-by-20 array
    9 End of data (sentinel)




    Suppose that the turtle is somewhere near the center of the floor. The following "program" would draw and print a 12-by-12 square leaving the pen in the up position:

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

    As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the 6 command (print) is given, wherever there is a 1 in the array, display an asterisk or some other character you choose. Wherever there is a zero, display a blank. Write a program to implement the turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle graphics language.

  6. #6
    jmc724 is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    Error messages:

    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:81: incomparable types: int[] and int
    while ( inputCommand != 9 && count < MAXCOMMANDS )
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:81: operator < cannot be applied to int[],int[]
    while ( inputCommand != 9 && count < MAXCOMMANDS )
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:84: incompatible types
    found : int[]
    required: int
    commandArray[ count ][ 0 ] = inputCommand;
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:84: array required, but int found
    commandArray[ count ][ 0 ] = inputCommand;
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:88: operator ++ cannot be applied to int[]
    count++;
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:91: incompatible types
    found : int
    required: int[]
    inputCommand = input.nextInt();
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:102: cannot find symbol
    symbol : variable command
    location: class TurtleGraphics
    command = commandArray[ commandNumber ][ 0 ];
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:102: cannot find symbol
    symbol : variable commandNumber
    location: class TurtleGraphics
    command = commandArray[ commandNumber ][ 0 ];
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:102: array required, but int found
    command = commandArray[ commandNumber ][ 0 ];
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:107: cannot find symbol
    symbol : variable commandNumber
    location: class TurtleGraphics
    while ( commandNumber < count )
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:107: cannot find symbol
    symbol : variable count
    location: class TurtleGraphics
    while ( commandNumber < count )
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:121: cannot find symbol
    symbol : variable command
    location: class TurtleGraphics
    switch ( command )
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:156: cannot find symbol
    symbol : variable command
    location: class TurtleGraphics
    command = commandArray[ ++commandNumber ][ 0 ];
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:156: cannot find symbol
    symbol : variable commandNumber
    location: class TurtleGraphics
    command = commandArray[ ++commandNumber ][ 0 ];
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:156: array required, but int found
    command = commandArray[ ++commandNumber ][ 0 ];
    ^
    15 errors

    Tool completed with exit code 1

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:81: incomparable types: int[] and int
    while ( inputCommand != 9 && count < MAXCOMMANDS )
    ^
    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:81: operator < cannot be applied to int[],int[]
    while ( inputCommand != 9 && count < MAXCOMMANDS )
    ^
    To compare the contents of two variables using == or != they must be the same type.
    What are the types of the variables in the above statement?

    C:\Users\Documents\IWU Courses\BIS346\WS5\TurtleGraphics.java:107: cannot find symbol
    symbol : variable commandNumber
    The compiler can't find commandNumber. Where is it defined?

    array required, but int found
    command = commandArray[ commandNumber ][ 0 ];
    You are using array syntax: [ ] after a variable that is not an array.

  8. #8
    jmc724 is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    To compare the contents of two variables using == or != they must be the same type.
    What are the types of the variables in the above statement?

    int inputCommand []= new int [6]; //array of inputCommand

    The compiler can't find commandNumber. Where is it defined?

    int commandNumber;

    You are using array syntax: [ ] after a variable that is not an array.

    commandArray[ count ][ 0 ] = inputCommand;

    Array of inputCommand

    int commandArray [] =

    {
    penDown,
    moveFwd,
    12,
    turnRight,
    moveFwd,
    12,
    turnRight,
    moveFwd,
    12,
    turnRight,
    moveFwd,
    12,
    penUp,
    turnRight,
    moveFwd,
    6,
    penDown,
    turnRight,
    moveFwd,
    11,
    print,
    end
    };

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Does it compile now?
    Don't understand your last post.

  10. #10
    jmc724 is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    It doesnt compile, its is passing the values of one array into another and the checking to see if the value is correct before execution. The errors occurs from one array passing to another array.

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post references in Java

    Norm, I found this a moment ago. This is how we do fancy addressing in Java: Object references (I) | Java Tips Blog

    Poster, your coding challeng actually leads to some very interesting work.
    Java Code:
    // POJO
    Object object = new Object();
    // Simple numeric value.:
    int someInt = 0;//
    // An array of ints:
    int[] intArray = new int[size];// where size is a primitive numeric.
    Your last post has strings and what is supposed to be numerics in the same array. Better design is to use Java's widely touted OO approach. Place all that is necessary in for your work, in an array of turtles - eg class Turtle extends Object - then get like a pond of turtles like this:
    Java Code:
    Turtle[] turtles = new Turtle[size];
    
    for each turtle in Turtle[] you have to put a new turtle before you do anything.
    
       turtles[ index ] = new Turtle();
    Norm, see: Mark Watson's Open Content Free Web Books and skip down to
    Practical Artificial Intelligence Programming in Java
    New version 2007: work in progress.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  12. #12
    jmc724 is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    Nicholas,

    Thank you. I will work on re-code and see if I am progressing. I thought it the assignment was a little too advance for a java beginner but will see after the tips you posted comes into play, thanks guys.

Similar Threads

  1. Java assignment
    By xtianah77 in forum New To Java
    Replies: 1
    Last Post: 02-18-2008, 12:54 AM
  2. Please help... assignment for school
    By confused2000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2007, 09:12 AM
  3. for Assignment plz help
    By assamhammad in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 09:35 PM
  4. java assignment, need help bad.
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 05:53 PM
  5. Help with my assignment java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •