java - "GridWorld" for ThinkJava Exercise 5.1 -


newbie completeing thinkjava book , trying figure out 1 of answers exercises. calls me download "gridworld" files , complete following steps:

  1. write method named movebug takes bug parameter , invokes move. test method calling main.
  2. modify movebug invokes canmove , moves bug if can.
  3. modify movebug takes integer, n, parameter, , moves bug n times (if can).
  4. modify movebug if bug can’t move, invokes turn instead.

i stuck on number 3, can not figure out how pass n "move() method"

-please newbie

my code:

import info.gridworld.actor.actorworld; import info.gridworld.actor.bug; import info.gridworld.actor.rock;  public class bugrunner {     public static void main(string[] args)     {         actorworld world = new actorworld();         bug redbug = new bug();         world.add(redbug);         world.add(new rock());         world.show();         movebug(redbug,5);         system.out.println(redbug.getlocation());     }      public static void movebug(bug abug, int n){         if(abug.canmove() == true){         abug.move();         } else {         abug.turn();         }     }   } 

you mean you're stuck on number 3:

  1. modify movebug takes integer, n, parameter, , moves bug n times (if can).

this means write loop, looping n times, calling move() once per iteration, if canmove() returns true.

btw: if (canmove() == true) {...} long way if (canmove()) {...}.

and fix indentation of if statement.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -