java - Mechanical Turtle "graphics", out of bounds exception -


thank taking time me learn what's wrong code , troubleshoot it. i'm not looking me, wish understand wrong , if have suggestions code please feel free make them if explain me. can't submit code don't understand.

i having issues out of bound exception. below brief synopsis of assignment. post driver, class (my code) , output file example (we required use driver without changes initial code, per assignment).

also mute majority of code once solved prevent others using directly (copy / paste) in future assignments if instructor or decides exact project

when creating turtle, place in middle of room, pen down, , facing right. more 1 turtle can placed in same room @ same time. initialize room blanks , keep track of each turtle, position , direction, state of pen @ times.

the class (my code)

class turtle {      boolean penstatus;      //pen status true = down, false =     int xpos, ypos;         //x position , 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 , int width      postcondition: returns void       constructor array"room" when initializeroom called, sets fields blank      */     public static void initializeroom(int h, int w){         string emptystr = "";         height = h;         width = w;         room = new string[height][width];         for(int = 0; < height; i++){             for(int j = 0; j < width; j++){                 room[i][j] = emptystr;             }         }     } // //all code between here redacted prevent use on similar assignment //       public static void draw(printstream input){         if(input.equals(system.out)){             for(int = 0; < height; i++){                 for(int j = 0; j < width; j++){                     system.out.println(room[i][j]);                 }             }         }     }  } 

the driver here demonstrate input , 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 prevent use on assignment in future       // 

and compile errors listed below. see going out of bounds. i'm having hard time editing code figure out how it's moving, because can't output because fails. appreciated.

exception in thread "main" java.lang.arrayindexoutofboundsexception: 20 @ turtle.forward(turtle.java:131) @ turtledriver.drawspiral(turtledriver.java:41) @ turtledriver.main(turtledriver.java:20)

size 40x20 per assignment. height/width , x/y not expected due matching array , not traditional graphs. per instructors requirement, though flip them if needed. output should in console , in output file. outfile not implemented yet.

     ******               *    *               *    *               *    *               *    *               *    *               *    *               ******                +++++++++ ########   +         #          + ++++++  # ######   + +    +  # #    #   + + ++ +  # # ## #   + +  + +  # #  # #   + ++++ +  # #### #   +      +  #      #   ++++++++  ########  

below @ line 20
drawspiral(myt1);

below method called @ line 20

   public static void drawspiral(turtle myt) {       for(int = 1; < 10; i++) {          myt.forward(i);          myt.right();       }    }   

this code marked causing out of bounds once running spiral method. spiral method ran driver , code below line 131 turtle class.

if(direction == 1){                  ypos = ypos + number;         if(penstatus == true){             for(int = number; 0 < number; i--){                 room[xpos][(ypos-i)] = turtle;             }         }            

update of 10:14pm april 19th, 2014

i overlooked loops not checking if "i" greater 0 if number was. code running until out of bounds, infinite loop. getting print stuff console direct prints in class code. below snippet of updated.

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

updated of 12:30am april 23, 2014

i have removed of code prevent use of code on same or similar project or other instructors @ university. if care see code unrelated school project or wish on assignment please send me message on page or account directly. more willing , store of files.

in forward function, not check whether room[xpos][ypos] still within room[height][width]. need check if xpos less height, ypos less width, , both greater or equal zero.

i suggest changing this:

if(direction == 1) {     int = 0;     if (ypos + number >= width) {         ypos = width-1;         = width-1 - ypos;     } else {         ypos = ypos + number;         = number;     }     if(penstatus == true){         for(; > 0; i--){             room[xpos][(ypos-i)] = turtle;         }     } } 

you need add similar condition each of other directions.

i suggest renaming string turtle different class turtle. it's confusing , may lead errors.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -