hey guys im doing the gridworld case study and am at the part where i program the Zbug class

I have to try and write the code in a select case statement.

can you guys fix my code?

for more info:
page 14 on

http://www.collegeboard.com/prod_downloads/student/testing/ap/compsci_a/ap07_gridworld_studmanual_appends_v3.pdf

i really need it fixed thanks a lot guys

import info.gridworld.actor.Bug;
import info.gridworld.grid.Location;
public class ZBug extends Bug
{
 private int sideLength;
 private int steps;
 private int segment;
 public ZBug(int length)
 {
  steps = 0;
  segment = 1;
  sideLength = length;
 }

 public void act()
 {
   switch (steps)
   {
     case 0:
     case 2:
     case 4:
     if (steps < sideLength && canMove())
        {
           move(); 
           steps++;
        }
     else if (segment == sideLength)
        break;
     case 1:
     setDirection(Location.SOUTHWEST);
     steps = 0;
     segment++;
 
     case 3:
     setDirection(Location.EAST);
     steps = 0;
     segment++;
     case 5:
      if (canMove() == false)
      break;
   }
 }
}

When you create a ZBug object, you set steps = 0. Then when you call the method act(), you switch steps, which waterfalls down to case 4 and invokes move(). This is probably where your logic is broken. Without knowing what the code for Bug , move(), or setDirection(), it can only be assumed your logic is broken. The rest appears to be valid Java...except for case 3, you'll probably want to add a break in there.

Please either revisit the logic needed to solve this problem or provide code for the classes/methods not shown.

This article has been dead for over six months. Start a new discussion instead.