Gridworld Case Study Part 1

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.

Using DrJava for the "GridWorld" Case Study

( last modified Monday, 29-Nov-2010 00:54:33 UTC )


Introduction

The "GridWorld" Case Study

The "GridWorld" Case Study is part of the Advanced Placement (AP®) Computer Science curriculum. It is required in both the A and AB course syllabi. The case study is an interactive, graphical program that simulates the behavior of different objects.

DrJava is a light-weight development environment that is well suited for student projects involving the case study. This page provides some instructions on how to use DrJava for the "GridWorld" Case Study.

Download from the AP Website

You can find all of the "GridWorld" Case Study materials at the AP® Computer Science website.

The source code for the case study is available in a ZIP file on that website. You may need a program to extract the ZIP file, e.g. WinZip.

Downloading and Installing DrJava

DrJava is a pure Java application that is distributed in three different forms: as a Java jar file, as a Windows .exe file, and as a Mac OS X application. The jar distribution will run on all platforms that support Java. On Windows, the Windows .exe file is usually more convenient because the it wraps the jar file inside a a script that finds the Java compiler (which can be in a variety of locations on Windows) before it starts DrJava, freeing the user from manually entering its location the first time DrJava is executed. On Mac OS X, the DrJava Mac application is a better choice than the jar file because it wraps the jar file in a "Mac app" that conforms to the user-interface conventions of the Mac platform. To install DrJava:

  1. If you have not already installed a Java SE Development Kit (JDK), version 5 or 6, download and install one from the Sun Microsystems website. DrJava requires a Java Development Kit (JDK), rather just a Java JRE (virtual machine), because the JDK includes the javac compiler.
  2. Download DrJava from our website. No "install" step is necessary; to run DrJava simply click on the icon for the file that you downloaded. If clicking on the jar file icon fails for some reason, see our instructions on how to run the DrJava jar file from the command line.

If you need help with any of these steps, please watch our video tutorials or contact us.


Using DrJava for the Case Study

Description of the Different Download Options

For your convenience, we provide two versions of the "GridWorld" source code for use with DrJava. The first version contains individual projects corresponding to the separate chapters of the case study. We recommend using this option, since students only have to work with the files necessary for the respective chapters. The projects are completely configured and ready to use with DrJava.

The second version contains the entire source code for all chapters configured as a single DrJava project.

Another option is to download the source code from the AP® website and create the DrJava project yourself. We provide descriptions on how to do that.


Download Option 1: Source Split Into Individual Chapters

For your convenience, we have created several different project files that correspond to the programs studied in the individual chapters of the case study. To use the project files, please follow these instructions:

  1. Download the case study materials with DrJava separate project files from our website. You do not need material from the the AP® Computer Science website.
  2. Unpack the archive into a directory of your choice. You may have to use a separate program like WinZip to extract the ZIP file.
  3. The directory where you unpacked the archive now contains four DrJava project files:
    • is the project for part 1.
    • is the project for part 2.
    • is the project for part 4.
    • is the project for parts 3 and 5.
  4. To open a project file,
    1. Run DrJava.
    2. Use the Open command in the Project menu (or press Ctrl+I).
    3. Find the project file you want to open and press the Open button.
    You are now ready to edit, run, and compile the project with DrJava. For more information about how to do that, please take a look at our Documentation and QuickStart Guide.

Note: Parts 3 and 5 do not specify which classes should be run; these parts ask you to study the source code. If you press the Run button in DrJava, you will get an error
To run programs, please open the project files for parts 1, 2 or 4.

Download Option 2: Source as a Single Project

If you prefer the source code not to be split up by chapters, here are instructions on how to use our pre-made project file that contains all the source code of the case study:

  1. Download the case study materials with a single DrJava project file from our website. You do not need material from the the AP® Computer Science website.
  2. Unpack them into a directory of your choice. You may have to use a separate program like WinZip to extract the ZIP file.
  3. The and framework directories have been merged into a single directory.
  4. There is a single DrJava project file called . It contains the source code for all five parts of the case study.
  5. To open a project file,
    1. Run DrJava.
    2. Use the Open command in the Project menu (or press Ctrl+I).
    3. Find the project file you want to open and press the Open button.
    You are now ready to edit, run, and compile the project with DrJava. For more information about how to do that, please take a look at our Documentation and QuickStart Guide.
Note: The project file is set up to run the class for part 1. To run the class for part 2 or the class for part 4, please change the Main Class in the Project Properties (see documentation), or type one of the following in the Interactions Pane:
or

Download Option 3: Do-It-Yourself

If you like, you can also create the project for DrJava yourself as follows:

  1. Download the case study materials from the AP® Computer Science website.
  2. Unpack them into a directory of your choice. You may have to use a separate program like WinZip to extract the ZIP file.
  3. To create a project file,
    1. Run DrJava.
    2. Use the New command in the Project menu.
    3. Pick a name for the new project file, select a place to save it, and press the Save button. We suggest you save it in the case study directory.
    4. In the Project Properties dialog, edit the project settings as described below.
    5. Set the Project Root to the directory that corresponds to the default package:
      • Part 1:
      • Part 2:
      • Part 4:
      • Parts 3 and 5:
    6. Click on the Add button in the Extra Classpath section, find the file in the case study directory, and press the Select button to add the file to the "Extra Classpath".
    7. Optional: If you want the class files to be saved in a directory separate from the Java source files, press the top ... button in the "Build Directory" row and navigate to the case study directory. Now press the Make New Folder button and enter a name for the directory, e.g. . Select the new directory and press the Select button.
    8. Optional: If you want to use the "Run Project" button of DrJava to run the project, enter the main class name in the Main Class field. Depending on the case study part, enter:
      • Part 1:
      • Part 2:
      • Part 4: oror
      • Parts 3 and 5 cannot be run.
    9. Press the OK button to close the Project Properties dialog.
    10. Use the Open Folder... command in the File menu and select the directory you selected as project root in step 3.e., then press the Select button.
    11. The source files are now displayed on the left side of the editor.
    You are now ready to edit, run, and compile the project with DrJava. For more information about how to do that, please take a look at our Documentation and QuickStart Guide.

Notes

DrJava Compiler Warnings when Compiling Case Study

When you are using DrJava to compile the case study source code, you may see one or both of the following compiler warning:

File: /Users/mgricken/Desktop/GridWorldCode_DrJava_AllTogether/source/info/gridworld/grid/BoundedGrid.java [line: 90] Warning: [unchecked] unchecked cast found : java.lang.Object required: E

This warning is generated because the case study contains a generic class that uses a plain array to store elements of type . The warning is unavoidable in generic Java. Other development environments often ignore these warnings by default; however, we chose to display these warnings to enable students to write code that is as timely and correct as possible.

You may either disable the warnings in DrJava or change the source code, as described below:

  • Disabling the Warnings in DrJava
    1. Use the Preferences... command in the Edit menu.
    2. In the Preferences dialog, switch to the Compiler Options category.
    3. Disable the Show Unchecked Warnings option.
    4. Press the OK button.
  • Changing the Source Code
    1. In the file, find the get method.
    2. Change the line

      to


Old "Marine Biology Simulation" Case Study

We also have instructions available for the previous case study, the "Marine Biology Simulation".

DrJava has been partially funded by the National Science Foundation, the Texas Advanced Technology Program, and Sun Microsystems, Inc.

One thought on “Gridworld Case Study Part 1

Leave a Reply

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