Chapter 5 GridWorld: Part 15.1 Getting startedNow 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: http://www.collegeboard.com/student/testing/ap/compsci_a/case.html. When you unpack this code, you should have a folder named GridWorldCode that contains projects/firstProject, which contains BugRunner.java. Make a copy of BugRunner.java in another folder and then import it into your development environment. There are instructions here that might help: http://www.collegeboard.com/prod_downloads/student/testing/ap/compsci_a/ap07_gridworld_installation_guide.pdf. Once you run BugRunner.java, download the GridWorld Student Manual from http://www.collegeboard.com/prod_downloads/student/testing/ap/compsci_a/ap07_gridworld_studmanual_appends_v3.pdf. The Student Manual uses vocabulary I have not presented yet, so to get you started, here is a quick preview:
Now you should be able to read Part 1 of the Student Manual and do the exercises. 5.2 BugRunnerBugRunner.java contains this 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();
world.add(new Bug());
world.add(new Rock());
world.show();
}
} The first three lines are import statements; they list the classes from GridWorld used in this program. You can find the documentation for these classes at http://www.greenteapress.com/thinkapjava/javadoc/gridworld/. Like the other programs we have seen, BugRunner defines a class that provides a main method. The first line of main creates an ActorWorld object. new is a Java keyword that creates new objects. The next two lines create a Bug and a Rock, and add them to world. The last line shows the world on the screen. Open BugRunner.java for editing and replace this line: world.add(new Bug()); with these lines: Bug redBug = new Bug();
world.add(redBug); The first line assigns the Bug to a variable named redBug; we can use redBug to invoke the Bug’s methods. Try this: System.out.println(redBug.getLocation()); Note: If you run this before adding the Bug to the world, the result is null, 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 canMove, move and turn and be sure you understand what they do. 5.3 ExercisesExercise 1
Exercise 2
Exercise 3 GridWorld uses Color objects, which are defined in a Java library. You can read the documentation at http://download.oracle.com/javase/6/docs/api/java/awt/Color.html. To create Bugs with different colors, we have to import Color: import java.awt.Color; Then you can access the predefined colors, like Color.blue, or create a new color like this: Color purple = new Color(148, 0, 211); Make a few bugs with different colors. Then write a method named colorBug that takes a Bug as a parameter, reads its location, and sets the color. The Location object you get from getLocation has methods named getRow and getCol that return integers. So you can get the x-coordinate of a Bug like this: int x = bug.getLocation().getCol(); Write a method named makeBugs that takes an ActorWorld and an integer n and creates n bugs colored according to their location. Use the row number to control the red level and the column to control the blue. |
Newer edition: Think Java, 2nd edition is free to read online. If you find this book useful, please consider supporting free, open educational materials from Green Tea Press. Modeling and Simulation in Python Book links to Amazon and Bookshop.org are affiliate links: I earn a small commission on purchases. |