Question

Thank you for taking the time to help me learn what's wrong with this code and troubleshoot it. I'm not looking for someone to do this for me, I just wish to understand what is wrong and if you have any suggestions for my code please feel free to make them but ONLY if you explain it to me. I can't submit code I don't understand.

I am having issues with an out of bound exception. Below is a brief synopsis of the assignment. I will then post the driver, class (my code) and the output file example (we are required to use the driver without changes to the initial code, as per the assignment).

Also I will mute a majority of the code once this is solved to prevent others from using it directly (copy / paste) in future assignments if the this instructor or another decides to do this exact project

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 be placed in the same room at the same time. 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.

The class (my code)

class Turtle {

    boolean penStatus;      //pen status true = down, false = up
    int xPos, yPos;         //x position and y position of turtle (x = height,         y = width)
    static String Room[][];     //sets room size via 2d array (static string array)
    String Turtle;          //determines turtle output marker
    int Direction;          //0 = north, 1= east, 2= south, 3= west
    static int height;
    static int width;

    /*
     Precondition:  accepts int height and int width
     Postcondition: returns void

     Constructor for The array"Room" when initializeRoom is called, sets all fields to blank
     */
    public static void initializeRoom(int h, int w){
        String emptyStr = "";
        height = h;
        width = w;
        Room = new String[height][width];
        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++){
                Room[i][j] = emptyStr;
            }
        }
    }
//
//all code between here is redacted to prevent use on similar assignment
//  
    public static void draw(PrintStream input){
        if(input.equals(System.out)){
            for(int i = 0; i < height; i++){
                for(int j = 0; j < width; j++){
                    System.out.println(Room[i][j]);
                }
            }
        }
    }

}

The Driver here to demonstrate input and all movements.

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, 20);
      Turtle myt1 = new Turtle('+');
      Turtle myt2 = new Turtle('#');
      Turtle myt3 = new Turtle('*');

      //
      //driver information redacted to prevent use on this assignment in the future
      //

And the compile errors listed below. Essentially I see that it is going out of bounds. but I'm having a hard time editing this code to figure out how it's moving, mostly because I can't get any output because it fails. So some help would be appreciated.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at Turtle.forward(Turtle.java:131) at TurtleDriver.drawSpiral(TurtleDriver.java:41) at TurtleDriver.main(TurtleDriver.java:20)

Size is 40x20 as per the assignment. The height/width and x/y are not as expected due to matching the array and not traditional graphs. as per the instructors requirement, though I Could flip them if needed. output should look like this in console and in output file. outfile not implemented yet.

     ******         
     *    *         
     *    *         
     *    *         
     *    *         
     *    *         
     *    *         
     ******         





 +++++++++ ######## 
 +         #        
 + ++++++  # ###### 
 + +    +  # #    # 
 + + ++ +  # # ## # 
 + +  + +  # #  # # 
 + ++++ +  # #### # 
 +      +  #      # 
 ++++++++  ######## 

Below is at line 20
drawSpiral(myt1);

Below is the method called at line 20

   public static void drawSpiral(Turtle myT) {
      for(int i = 1; i < 10; i++) {
         myT.forward(i);
         myT.right();
      }
   }  

This code is what is marked as causing the out of bounds once running the spiral method. spiral method is ran from the driver and the code below is line 131 from the Turtle class.

if(Direction == 1){         
        yPos = yPos + number;
        if(penStatus == true){
            for(int i = number; 0 < number; i--){
                Room[xPos][(yPos-i)] = Turtle;
            }
        }           

update as of 10:14pm April 19th, 2014

I overlooked that the for loops were not checking if "i" was greater than zero but if number was. So the code was running until out of bounds, like an infinite loop. I am getting it to print some stuff to console with direct prints in the class code. below is a snippet of what I updated.

if(penStatus == true){
                for(int i = number; 0 < i; i--){

updated as of 12:30am April 23, 2014

I have removed much of the code to prevent use of this code on the same or similar project by this or other instructors at my university. If you care to see this code unrelated to your school project or you wish help on this assignment then please send me a message on this page or to my account directly. I am more than willing to help and store all of my files.

Était-ce utile?

La solution

In your forward function, you do not check whether Room[xPos][yPos] is still within Room[height][width]. You need to check if xPos is less than height, yPos is less than width, and both are greater than or equal to zero.

I suggest changing it into this:

if(Direction == 1) {
    int i = 0;
    if (yPos + number >= width) {
        yPos = width-1;
        i = width-1 - yPos;
    } else {
        yPos = yPos + number;
        i = number;
    }
    if(penStatus == true){
        for(; i > 0; i--){
            Room[xPos][(yPos-i)] = Turtle;
        }
    }
}

You also need to add a similar condition for each of the other directions.

I also suggest renaming the String Turtle into something different from the class Turtle. It's confusing and may lead to errors.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top