hi! how do you make an object go backwards through an array? my class is doing an ap question called robot clears the hall. everything is working except for the fact that the object doesn't turn around to see if any thing isnt picked up. I need 9 moves and i keep getting 6.
Here is my class defn:


public class Robot
{
private int[] hall;
private int pos; //current position (tile number) of Robot
private boolean facingRight; //true means this Robot is facing right


//Constructor
public Robot (int[] hll, int ps, boolean fcngRgt)
{
hall = hll;
pos = ps;
facingRight = fcngRgt;
}
/**
* @return String containing a row of the tile numbers and
* Underneath a representation of the hallway
* And underneath the hallway, either a < or a > showing the
* Position of the robot
* 0 1 2 3
* _______________
* | 1 | 1 | 2 | 2 |
* ---------------
* >
*/
public String toString()
{
String returnStr = "\n ";
for (int i = 0; i < hall.length; i++)
returnStr += (i + " ");
returnStr += "\n ";
for (int j = 0; j < (4 * hall.length - 1); j++)
returnStr += "_";
returnStr += "\n| ";
for (int k = 0; k < hall.length; k++)
returnStr += (hall[k] + " | ");
returnStr += "\n ";
for (int l = 0; l < (4 * hall.length - 1); l++)
returnStr += "-";
returnStr += "\n ";
for (int m = 0; m < pos; m++)
returnStr += " ";
if (facingRight)
returnStr += ">";
else
returnStr += "<";
returnStr += "\n";
return returnStr;
//thank you Mike Ross!
}
/**
* postcondition: returns true if this Robot has a wall immediately in
* front of it, so that it cannot move forward;
* otherwise, returns false
*/
public boolean forwardMoveBlocked()
{
if ((facingRight && pos ==hall.length-1))
{
return true;
}
else if(!facingRight && pos == hall[0])
{
return true;
}
else
{
return false;
}

}


/**
* if there are any items on the current tile, then one items is removed
* if there are more item on current tile, then robot remains facing same direction
* if no more item exist on current tile
* if robot can move forward, it advances
* otherwise it turns around without changing position
*/
public void move()
{
if (hall[pos] > 0)
hall[pos]--;

if (hall[pos] > 0)
{
facingRight = facingRight;
}


if (hall[pos] == 0)
{
if (!forwardMoveBlocked())
{
pos ++;
}
else
{
facingRight = !facingRight;
}

}

}
/**
* postcondition: returns true if the hallway contains no items;
* otherwise returns false;
*/
public boolean hallIsClear()
{
boolean noItems = true;
for (int i =0; i< hall.length; i ++)
{
if (hall[i] == 0)
{
noItems = true;
}
else
{
noItems = false;
}
}
return noItems;
}
/**
* postcondition: no more items remain in the hallway.
* inside your loop please have the robot print his new situation after each move
*/

public int clearHall()
{
int count = 0;
while(!hallIsClear())
{
move();
count ++;
System.out.println("Yes! I picked up a toy from this hallway!");
}
return count;
}
}

driver:
public class RobotTester
{
public static void main(String[] args)
{
int [] hall = {1, 1, 2, 2};
Robot botOne = new Robot(hall, 0, true);
System.out.println(botOne.clearHall());
Robot bot1E = new Robot(hall, 1, true);
System.out.println("Robot at index 1 of the hallway facing east: Is he blocked? " +
bot1E.forwardMoveBlocked() + "\n" + bot1E);
Robot bot2E = new Robot(hall, 2, true);
System.out.println("Robot at index 2 of the hallway facing east: Is he blocked? " +
bot2E.forwardMoveBlocked() + "\n" + bot2E);
Robot bot3E = new Robot(hall, 3, true);
System.out.println("Robot in at index 3 of the hallway facing east: Is he blocked? " +
bot3E.forwardMoveBlocked() + "\n" + bot3E);
Robot bot0W = new Robot(hall, 0, false);
System.out.println("Robot at the beginning of the hallway facing west: Is he blocked? " +
bot0W.forwardMoveBlocked() + "\n" + bot0W);
Robot bot1W = new Robot(hall, 1, false);
System.out.println("Robot at index 1 of the hallway facing west: Is he blocked? " +
bot1W.forwardMoveBlocked() + "\n" + bot1W);
Robot bot2W = new Robot(hall, 2, false);
System.out.println("Robot at index 2 of the hallway facing west: Is he blocked? " +
bot2W.forwardMoveBlocked() + "\n" + bot2W);
Robot bot3W = new Robot(hall, 3, false);
System.out.println("Robot in at index 3 of the hallway facing west: Is he blocked? " +
bot3W.forwardMoveBlocked() + "\n" + bot3W);

}

}
output:
Yes! I picked up a toy from this hallway!
Yes! I picked up a toy from this hallway!
Yes! I picked up a toy from this hallway!
Yes! I picked up a toy from this hallway!
Yes! I picked up a toy from this hallway!
Yes! I picked up a toy from this hallway!
6
Robot at index 1 of the hallway facing east: Is he blocked? false

0 1 2 3
_______________
| 0 | 0 | 0 | 0 |
---------------
>

Robot at index 2 of the hallway facing east: Is he blocked? false

0 1 2 3
_______________
| 0 | 0 | 0 | 0 |
---------------
>

Robot in at index 3 of the hallway facing east: Is he blocked? true

0 1 2 3
_______________
| 0 | 0 | 0 | 0 |
---------------
>

Robot at the beginning of the hallway facing west: Is he blocked? true

0 1 2 3
_______________
| 0 | 0 | 0 | 0 |
---------------
<

Robot at index 1 of the hallway facing west: Is he blocked? false

0 1 2 3
_______________
| 0 | 0 | 0 | 0 |
---------------
<

Robot at index 2 of the hallway facing west: Is he blocked? false

0 1 2 3
_______________
| 0 | 0 | 0 | 0 |
---------------
<

Robot in at index 3 of the hallway facing west: Is he blocked? false

0 1 2 3
_______________
| 0 | 0 | 0 | 0 |
---------------
<