Try to answer these questions on your own. Use pp. 16-19 and Appendix B page B-1.
Now to see how you did, write a class to find out how you did. For example, here is a tiny program I wrote, copying and pasting from the Manual, then adding my own println statements to reveal the results of certain method calls. If you are lazy (but clever) you can use the Debugger to inspect the results instead of println statements. Go ahead and copy and paste the following.
import info.gridworld.actor.ActorWorld; import info.gridworld.grid.Location; import java.awt.Color; /** * This class helps test out answers to Set 3. * */ public class Part3Set3 { public static void main(String[] args) { Location loc1 = new Location(4,3); Location loc2 = new Location(3,4); //Question 1 System.out.println("1. The row value of loc1 is "+loc1.getRow() ); //Question 2 Boolean b= loc1.equals(loc2); System.out.println("2. The value of b is "+ b ); //Question 3 Location loc3 = loc2.getAdjacentLocation(Location.SOUTH); System.out.println("3. The value of loc3 is "+ loc3 ); //Question 4 int dir = loc1.getDirectionToward(new Location(6,5)); System.out.println("4. The direction of 6,5 from loc1 is "+ dir ); //Question 5 Location ne=loc1.getAdjacentLocation( Location.NORTHEAST ); System.out.println("5. The northeast location from loc1 is location of a is "+ ne ); } }
import info.gridworld.grid.*; /** * This class runs a world that helps test out answers to Set 4. * */ public class GridTest { public static void main(String[] args) { BoundedGrid gr=new BoundedGrid(10,10); Location loc1 = new Location(4,3); Location loc2 = new Location(11,4); System.out.println("Is 4,3 in gr? " + gr.isValid(loc1) ); System.out.println("Is 11,4 in gr? " + gr.isValid(loc2) ); } }Go ahead and change the GridTest code to see if you can get it to determine if 0,0 or 10,10 are valid locations.
Now lets turn to the Do You Know? Set 4. To see getOccupiedLocations() at work, we will populate a Grid and then call it. Since it returns an ArraryList, we can use the size() method to see how many locations there are!
import info.gridworld.grid.*; import java.util.ArrayList; /** * This class runs a world that helps test out answers to Set 4. * */ public class Part3Set4 { public static void main(String[] args) { BoundedGrid gr=new BoundedGrid(10,10); Location loc1 = new Location(4,3); Location loc2 = new Location(1,1); gr.put(loc1, "keys"); gr.put(loc2, "coins"); gr.put(new Location(2,1), 7); ArrayList stuff=gr.getOccupiedLocations(); System.out.println(stuff); System.out.println(stuff.size()); System.out.println( (String)gr.get(loc1) ); System.out.println( gr.get(new Location (2,1) ) ); System.out.println("There are "+gr.getOccupiedLocations().size()+" things in the grid"); } }Can you see there are several other methods in the Grid Interface? Go to Appendix B page B-2 and see if you can build upon Part3Set4 to get results printed from calls to :
import info.gridworld.actor.*; import info.gridworld.grid.*; public class ActorRunner { public static void main(String[] args) { ActorWorld world = new ActorWorld(); Actor a = new Actor(); world.add(a); Grid g = a.getGrid(); //must remember the grid for placement back in //the grid world.add(new Rock()); world.show(); } }
import info.gridworld.actor.*; import java.awt.Color; public class Part3Set5Number4a { public static void main(String[] args) { ActorWorld world = new ActorWorld(); Bug b = new Bug(); world.add(b); b.putSelfInGrid(b.getGrid(),b.getLocation()); world.add(new Rock()); world.show(); } }
import info.gridworld.actor.*; import java.awt.Color; public class Part3Set5Number4b { public static void main(String[] args) { ActorWorld world = new ActorWorld(); Bug b = new Bug(); world.add(b); world.add(new Rock()); world.show(); b.removeSelfFromGrid(); b.removeSelfFromGrid(); } }
import info.gridworld.actor.*; import info.gridworld.grid.*; public class Part3Set5Number4c { public static void main(String[] args) { ActorWorld world = new ActorWorld(); Actor a = new Actor(); world.add(a); Grid g = a.getGrid(); //must remember the grid for placement back in //the grid world.add(new Rock()); world.show(); a.removeSelfFromGrid(); a.putSelfInGrid(g,new Location(5,5)); //must specify a location here } }
import info.gridworld.actor.*; import info.gridworld.grid.*; public class Part3Set6Number9 { public static void main(String[] args) { ActorWorld world = new ActorWorld(); //ActorWorld is not Testable.. see page 27 Bug b = new Bug(); world.add(new Location(9,9), b); world.show(); b.removeSelfFromGrid(); } }
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.awt.Color;
/**
* A Jumper
is an actor that will jump over Rocks and Flowers
* and turn.
*/
public class Jumper extends Actor
{
/**
* Constructs a pink Jumper.
*/
public Jumper()
{
}
/**
* Constructs a Jumper of a given color.
* @param JumperColor the color for this Jumper
*/
public Jumper(Color JumperColor)
{
}
public void act()
{
}
/**
* Turns the Jumper 45 degrees to the right without changing its
* location.
*/
public void turn()
{
}
/**
* Moves the Jumper forward two locations.
* The location two in front must be valid or the Jumper will remove
* itself from the grid.
*/
public void jump()
{
}
/**
* Tests whether this Jumper can move forward into a location two in
* front that is empty or contains a flower.
* The location one in front must be empty or contain a Rock or a
* Flower.
*
* Hint: You can tell what kind of Actor something is by using the
* instanceof command. For example, if Flower f=new Flower(), then
* f instanceof Flower returns true, and f instanceof Rock returns false.
*
* @return true if this Jumper can move.
*/
public boolean canJump()
{
}
}
import info.gridworld.actor.*; /** * This class runs a world that contains a jumper, a bug, a flower, and a * rock added at random locations. */ public class JumperRunner { public static void main(String[] args) { ActorWorld world = new ActorWorld(); world.add(new Jumper()); world.add(new Rock()); world.add(new Bug()); world.add(new Flower()); world.show(); } }