Grid World Case Study Student Manual

Chapter 5  GridWorld: Part 1

5.1  Getting started

Now is a good time to start working with the AP Computer Science Case Study, which is a program called GridWorld. To get started, install GridWorld, which you can download from the College Board: .

When you unpack this code, you should have a folder named that contains , which contains .

Make a copy of in another folder and then import it into your development environment. There are instructions here that might help: .

Once you run , download the GridWorld Student Manual from .

The Student Manual uses vocabulary I have not presented yet, so to get you started, here is a quick preview:

  • The components of GridWorld, including Bugs, Rocks and the Grid itself, are objects.
  • A constructor is a special method that creates new objects.
  • A class is a set of objects; every object belongs to a class.
  • An object is also called an instance because it is a member, or instance, of a class.
  • An attribute is a piece of information about an object, like its color or location.
  • An accessor method is a method that returns an attribute of an object.
  • A modifier method changes an attribute of an object.

Now you should be able to read Part 1 of the Student Manual and do the exercises.

5.2  

contains this code:

info.gridworld.actor.ActorWorld; info.gridworld.actor.Bug; info.gridworld.actor.Rock; BugRunner { main(String[] args) { ActorWorld world = ActorWorld(); world.add( Bug()); world.add( Rock()); world.show(); } }

The first three lines are statements; they list the classes from GridWorld used in this program. You can find the documentation for these classes at .

Like the other programs we have seen, BugRunner defines a class that provides a method. The first line of creates an object. is a Java keyword that creates new objects.

The next two lines create a Bug and a Rock, and add them to . The last line shows the world on the screen.

Open for editing and replace this line:

world.add( Bug());

with these lines:

Bug redBug = Bug(); world.add(redBug);

The first line assigns the Bug to a variable named ; we can use to invoke the Bug’s methods. Try this:

System.out.println(redBug.getLocation());

Note: If you run this before adding the Bug to the , the result is , which means that the Bug doesn’t have a location yet.

Invoke the other accessor methods and print the bug’s attributes. Invoke the methods , and and be sure you understand what they do.

5.3  Exercises

Exercise 1  
  1. Write a method named that takes a bug as a parameter and invokes . Test your method by calling it from .
  2. Modify so that it invokes and moves the bug only if it can.
  3. Modify so that it takes an integer, , as a parameter, and moves the bug times (if it can).
  4. Modify so that if the bug can’t move, it invokes instead.
Exercise 2  
  1. The class provides a method named that returns a double between 0.0 and 1.0 (not including 1.0).
  2. Write a method named that takes a Bug as a parameter and sets the Bug’s direction to one of 0, 90, 180 or 270 with equal probability, and then moves the bug if it can.
  3. Modify to take an integer and repeat times.

    The result is a random walk, which you can read about at .

  4. To see a longer random walk, you can give ActorWorld a bigger stage. At the top of , add this statement:

    info.gridworld.grid.UnboundedGrid;

    Now replace the line that creates the ActorWorld with this:

    ActorWorld world = ActorWorld( UnboundedGrid());

    You should be able to run your random walk for a few thousand steps (you might have to use the scrollbars to find the Bug).

Exercise 3  

GridWorld uses Color objects, which are defined in a Java library. You can read the documentation at .

To create Bugs with different colors, we have to import :

java.awt.Color;

Then you can access the predefined colors, like , or create a new color like this:

Color purple = Color(148, 0, 211);

Make a few bugs with different colors. Then write a method named that takes a Bug as a parameter, reads its location, and sets the color.

The Location object you get from has methods named and that return integers. So you can get the x-coordinate of a Bug like this:

x = bug.getLocation().getCol();

Write a method named that takes an ActorWorld and an integer and creates bugs colored according to their location. Use the row number to control the red level and the column to control the blue.

Быть может, смерть Танкадо в публичном месте была необходимостью, однако публика возникла чересчур. Халохот был вынужден скрыться, не успев обыскать убитого, найти ключ.

А когда пыль осела, тело Танкадо попало в руки местной полиции. Стратмор был взбешен. Халохот впервые сорвал задание, выбрав неблагоприятные время и место.

0 thoughts on “Grid World Case Study Student Manual

Leave a Reply

Your email address will not be published. Required fields are marked *