Using DrJava for the "Marine Biology Simulation" Case Study

( last modified Monday, 03-Aug-2009 16:28:58 UTC )


Introduction

The "Marine Biology Simulation" Case Study

The "Marine Biology Simulation" 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 fish in different environments.

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 "Marine Biology Simulation" Case Study.

Download from the AP Website

You can find all of the "Marine Biology Simulation" 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 J2SE 1.4 or 1.5 (5.0) SDK, download and install one from the Sun Microsystems website. DrJava requires a Java SDK, rather just a Java JRE (virtual machine), because the SDK 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.


Using DrJava for the Case Study

Description of the Different Download Options

For your convenience, we provide two versions of the "Marine Biology Simulation" 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 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. Delete the Code subdirectory - it contains the source code as it was before we split it up for the individual chapters and is not needed anymore.
  4. Download the split up source code from the DrJava website.
  5. Unpack the new source code into the same directory as you unpacked the original materials. After doing so, the directory will contain two jar files (mbsbb.jar and mbsgui.jar), as well as the following five subdirectories:
    • Ch1_SimpleMBSDemo1 - this is the first simple program that Pat looks at in Chapter 1.
    • Ch1_SimpleMBSDemo2 - this is the second program he studies in Chapter 1.
    • Ch1and2_MBSGUI - this is the existing program that the students use in Chapters 1 and 2.
    • Ch3 - this is the program with modifications to the Fish class that allow for breeding and dying. If you have made modifications to their Fish class in the previous chapters and want to continue using it, they should replace the Fish.java file in the Ch3\Source subdirectory with their own version, then follow the FishModsForChap3.txt file in the Ch3\Source directory to incorporate the necessary changes.
    • Ch4and5 - this code is the same as in Ch1and2_MBSGUI, but SlowFish and DarterFish have been added. If students want to use their own Fish class from previous chapters, they can replace the Fish.java file in the Ch4and5\Source subdirectory with their own file. They can also replace it with the Fish.java file from chapter 3 to get the breeding and dying behaviors.
  6. Each of these folders contains a DrJava project file that ends in .pjt. All the source code for a project is contained in a Source subdirectory inside the project directory (e.g. Ch1and2_MBSGUI\Source), and all class files will be placed in a Classes subdirectory inside the project directory.
  7. 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.


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 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. Delete the Code subdirectory - it contains the source code as it was before we created the project file and is not needed anymore.
  4. Download the all-in-one source code from the DrJava website.
  5. Unpack the new source code into the same directory as you unpacked the original materials. After doing so, the directory will contain two jar files (mbsbb.jar and mbsgui.jar), as well as the following tw subdirectories:
    • Code - this folder contains the entire source code of the case study.
    • Code_DynamicPopulation - this folder contains the source code with the changes for breeding and dying already made.
  6. Each of these folders contains a DrJava project file that ends in .pjt. All the source code for a project is contained in a Source subdirectory inside the project directory (e.g. Code\Source), and all class files will be placed in a Classes subdirectory inside the project directory.
  7. 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.


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. Use the Open Folder... command in the File menu and select the Code subdirectory. Make sure that "Open folders recursively" is not checked, then press the Select button.
    5. The source files are now displayed on the left side of the editor.
    6. Use the Project Properties command in the Project menu.
    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. Classes. Select the new directory and press the Select button.
    8. Click on the Add button, find the mbsbb.jar file in the Code subdirectory, and press the Select button to add the mbsbb.jar file to the "Extra Classpath".
    9. Repeat step 4.h above for the mbsgui.jar file.
    10. If you have used DrJava for other projects before, you should also make sure that the two jar files you just added are the only entries in the "Extra Classpath" list. If necessary, select and remove other entries using the Remove button.
    11. Press the Ok to save your changes to the project properties.
    12. Use the Save command in the Project menu.
    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 warnings:

File: ...\Fish.java  [line: ???]
Warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList


File: ...\UnboundedEnv.java  [line: ???]
Warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList

These warnings are generated when the case study source code is compiled with DrJava using the Java 5.0 SDK. The Java 5.0 SDK provides generic versions of the ArrayList class and emits an "unchecked call to X() as a member of the raw type Y()" warning if ArrayList is used as raw type, i.e. without specifying the type of the items it contains. 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.

The current AP® CS curriculum and the case study were written before generics were available in Java. The next version of the curriculum might include coverage of generics and will probably contain an updated version of the case study that will not generate the warnings. In the meantime, 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 Fish.java file, find the emptyNeighbors method.
    2. Change the line
      ArrayList emptyNbrs = new ArrayList();
      to
      ArrayList<Location> emptyNbrs = new ArrayList<Location>();
    3. In the UnboundedEnv.java file, find the definition of the member variable objectList.
    4. Change the line
      private ArrayList objectList; // list of Locatable objects in environment
      to
      private ArrayList<Locatable> objectList; // list of Locatable objects in environment
    5. Find the constructor UnboundedEnv().
    6. Change the line
      objectList = new ArrayList();
      to
      objectList = new ArrayList<Locatable>();

    Note that these changes only represent the minimal amount of work necessary to remove the warnings. The changed program still does not consistently use generics.

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