3. Writing Basic Classes 3
You can look up the methods available in the GridWorld classes. There is a directory of all methods. The most useful ones are Actor, Bug, and Flower.
-
CreepingCharlie
: A subclass of flower that has the normal behavior of a flower, except every third time it is asked toact()
, it changes its direction by 45 degrees. (See documentation for Actor.)Flower c = new CreepingCharlie();
-
PayMeBug
: Write a class calledPayMeBug
that is a kind ofBug
. Its constructor should take in a number of steps (N) the bug will go when paid. Write a method namedpay()
to yourPayMeBug
. After it is paid, the next N times its act method is called (maybe by hitting the “Step” button), it moves forward like an ordinary bug. Then it stops and waits to be paid again.Bug s = new NStep(3); Location where = new Location(1,5); world.add(where, s); s.pay(); // now moves
-
IRBug
. Write a classIRBug
for “isosceles right triangle bug”. The constructor should take in the number of steps on one leg of the triangle. When the bug’sact()
method is called, it uses the Bug’smove()
method to move along each side and uses the appropriate number ofturn()
calls to turn at each corner. It can repeat the triangle forever.Bug t = new IRBug(4); Location where = new Location(2,7) world.add(where, t); [...]
It is fine if the
IRBug
crushes flowers and rocks instead of turning.