Wyo Java Ch. "Marine Bio" Lecture Notes
Objective #1: Explain the rationale for the Case Study.
- College Board includes questions on the AP Computer Science exam from a case study. Currently, the case study is known as the Marine Biology Simulation Case Study (MBS). In order to do well on the AP exam, you must be familiar with the content of this Case Study.
- On part 1 of the exam, students will be asked questions that have to do with analysis of the Case Study source code, explanation of design decisions, understanding the results of the execution of code, & developing test cases. There will be at least 5 multiple choice questions out of 40 that have to do with the Case Study.
- On part 2 of the exam, students will have to modify the
source code or add additional code to existing methods. One of the four
free-response questions will have to do with the Case Study. This question
will probably ask you to derive another class from Fish and override some
of its methods or add new methods. When answering this free-response question,
try to find a similar code segment in the Case Study source code and adapt
that code rather than writing something from scratch. Be sure to use equals
rather than == when necessary. Be sure to correctly use ArrayLists vs arrays
when necessary. Don't forget to cast when using the ArrayList get method.
Don't call methods from objects of the wrong type (e.g. don't write env.act();
). Don't use private properties when you're writing code in a different
class.
- The case study requires you to work with a real-world program that is significantly longer than those typically assigned by a programming teacher.
- It also makes you think about issues of good programming design.
- It is also beneficial to study code written by expert programmers.
- A number of principles of object-oriented programming (OOP) are intentionally worked into the Case Study.
Objective #2: Know where to obtain the case study source code & narrative materials.
- The College Board home page for the Case Study is found at www.collegeboard.com/student/testing/ap/compsci_a/case.html
- The narrative & source code can be found at www.collegeboard.com/prod_downloads/student/testing/ap/compsci_a/2002_mbcs.pdf. Only AB students are responsible for the material in Ch. 5 of this packet.
- The source code for the "visible" classes that you must know for the exam are found in Appendix B.
- The source code for the "black box" classes that you must not understand are found in Appendix C.
- The source code for the BoundedEnv & UnboundedEnv classes is found in Appendix D. Only AB students are responsible for this code.
- IMPORTANT: Appendices B, C, D, E, & F will be provided to you during the AP exam. (Appendices D & F are only relevant for the AB exam.) This includes the source code for the Simulation, Fish, DarterFish, and SlowFish classes and the BoundedEnv and UnboundedEnv classes for AB students. ALL STUDENTS SHOULD BE FAMILIAR WITH FINDING CODE AND DOCUMENTATION WITHIN THESE APPENDICES.
Objective #3: Be able to set up & execute the Case Study in JCreator.
Objective #4: Be familiar with the core classes & utility classes of the Case Study.
- Core classes
& interfaces
- The core classes of the Case Study are Simulation, Fish, DarterFish, & SlowFish. The Environment interface is also important. AB students should also consider the BoundedEnv and UnboundedEnv to be core classes.
- Simulation - You must be familiar with the actual implementation of the Simulation class. The simulation acts in timesteps. During each timestep, each fish gets a chance to move. It is very important to understand and be able to trace the step method.
- Fish - You must be familiar with the actual implementation of the Fish, SlowFish, and DarterFish classes. There are two versions of the Fish class. The simple version of the Fish class only allows fish to move. The more complex Fish class includes breeding and dying. Chapters 1 & 2 of the College Board packet only refer to "simple" fish while Chapter 3 deals with "dynamic" fish. The dynamic Fish class is the one that will be tested on the AP exam.
- Environment - You only need to be familiar with
the documentation of the Environment interface. The environment is
modelled on a two-dimensional grid. The environment is configured
to contain any type of object that implements the Locatable interface.
The Environment interface can therefore be used with objects as diverse
as ArmyTank and Millipede objects as long as those classes realize
the Locatable interface. A exam students will not be asked to modify
any
aspect
of the environment. AB exam students may be asked to modify the environment
and they must know that it is a two-dimensional array named theGrid[][].
At most, one fish can occupy each cell of the grid. AB students must
understand how the abstract SquareEnvironment is used. There is an
includeDiagonalNeighbors parameter in the SquareEnvironment constructor
that allows cells to have 8 neighbors (including the diagonals) rather
than 4.
- BoundedEnv - AB students must be familiar with the actual implementation of the BoundedEnv class and be able to modify this class. The BoundedEnv class has a two-dimensional array of Locatable's named theGrid as a private field.
- UnboundedEnv - AB students must be familiar with
the actual implementation of the UnboundedEnv class and be able to
modify this class. An unbounded environment models a rectangular
grid. It's different in that locations such as (-4, -8) are valid.
The default implementation of UnboundedEnv class has an ArrayList
named objectList as a private property. However, an exercise on the
AP exam might require an AB student to design or even implement another
implementation of UnboundedEnv. For example, a student may have to
use a TreeMap or a HashMap to keep track of fish in an unbounded environment.
Since the Location class has a hashCode method and a compareTo method,
Location objects will need to be stored in a TreeSet or a HashSet
and used as keys for a TreeMap or HashMap.
- Utility classes
& interfaces
- The utility classes are Debug, Direction, Location, & RandNumGenerator. They are black box classes since students do not have to know how the methods in those classes work (i.e. how they are implemented).
- Direction - You only need to be familiar with the documentation of the Direction class. Students must be familiar with the 8 class constants: NORTH, SOUTH, EAST, WEST, NORTHEAST, NORTHWEST, SOUTHEAST, & SOUTHWEST. However, you don't need to understand the FULL_CIRCLE constant or the hashCode or roundedDir methods. Direction objects actually measure their magnitude as an integer number of degrees between 0 and 359.
- EnvDisplay - You only need to be familiar with the documentation of the EnvDisplay class.
- Locatable - You only need to be familiar with the documentation of the Locatable interface.
- Location - You only need to be familiar with the documentation of the Location class. A Location object represents a row and column position (think RC Cola) within the environment. When two locations are compared with the compareTo method, they are first compared by row number. Whichever Location object has the smaller row number is considered to be less than the other. If the row numbers are equal, then the column numbers are compared.
- RandNumGenerator - You only need to be familiar with the documentation of the RandNumGenerator class. It is necessary because the Java Random class does not provide varied random numbers without it. Students should know that the computer can only produce pseudo-random values since it is impossible for the computer to create truly random values. Students should understand the role of a seed value and how it can be used to produce consistent sets of pseuodo-random values for testing purposes. The RandNumGenerator class will probably not be tested on the exam. However, students must be familiar with the java.util.Random class and its nextInt and nextDouble methods.
- Color - You only need to know how to use the constructor and constants of the Color class. Some constants in the Color class include black, blue, cyan, gray, green, magenta, orange, pink, red, white, & yellow. There is a useful constructor that accepts integer parameters between 0 and 255 to define the amount of red, green, & blue in custom-made colors.
- Debug - The Debug class will probably not be tested on the AP exam. Students should appreciate though how easy it is to use in order to test various aspects of the Case Study.
- You must know the following vocabulary terms and how they relate to the Case Study:
- boundary tests
- client code
- dynamic binding
- instance variable (aka field)
- override
- pseudo-code
- redefine
- timestep
- black box
- Prof. Owen Astrachan's Marine Biology API is very useful for reviewing the various classes and methods.
- Students do not have to be familiar with the client program MBSGUI that is provided by College Board. Students should be familiar with how data files are used to initially place fish in the environment though this probably won't be tested on the exam.
Objective #5: Understand the implementation of the Fish class and various behaviors of Fish objects.
- Fish move in "row-major" order. That is, all of the fish in the first (i.e. top) row #0 get a chance to move from left to right and then the fish in row #1 from left to right.
- Fish can only move north, south, east, or west. It moves in a random direction. A fish cannot move diagonally and it cannot move backwards.
- If all of the adjacent cells are occupied, a fish does not move.
- When a fish moves by turning right or left, it's direction changes to the direction in which it moved. If a fish moves straight, it keeps the same direction.
- An act method was provided to the Fish class as a "controller" method that can be used to either call the move method or another method. It would not have been good design to directly call the move method in the Simulation step method.
- A fish first attempts to breed. If a random value less than 1/7 is generated, the fish successfully breeds and places baby fish in all empty, adjacent cells. If the fish does not breed, it attempts to move. Then, whether the fish bred and/or moved, it dies if a random value less than 1/5 is generated.
- The default probability of breeding (probOfBreeding) is 1.0/7.0 while the default probability of dying (probOfDying) is 1.0/5.0.
- Each Fish object is aware of its environment due to the fact that it has an Environment reference as a private property.
- The initialize method is private and therefore can only be called within the Fish class. DarterFish or SlowFish cannot even directly call the initialize method. The constructors of the Fish class call the initialize method.
- There are several protected methods of the Fish class. Since they are protected methods, they may be called by DarterFish and SlowFish.
- When a fish breeds, the new fish have the same color as their "parent" but a random direction.
- Each fish has a field named myId. This unique integer value is obtained from the Fish class static variable nextAvailableID that is incremented each time a Fish is constructed.
Objective #6: Understand the SlowFish and DarterFish classes and how they override methods from their parent, the Fish class.
- SlowFish and DarterFish are child classes of the Fish class. Therefore, DarterFish "is-a" Fish and SlowFish "is-a" Fish.
- The default color for DarterFish is yellow and the default color for slow fish is red.
- Polymorphism occurs since the Fish reference nemo can take on different types in the following example:
Fish nemo;
nemo = new Fish(env, loc);
or
Fish nemo;
nemo = new DarterFish(env, loc);
- Inheritance is used since SlowFish and DarterFish inherit public and protected methods from Fish class. This allows for code resuse.
- Dynamic binding (aka late binding) occurs when the Java Virtual Machine decides at run-time which version of the move method to execute. There are times when DarterFish may be passed to a method that accepts a Fish parameter and the move method is executed whereas there are times when a SlowFish is passed to the same method and a different version of the move method is executed.
- SlowFish overrides (aka redefines) the nextLocation & generateChild methods while DarterFish overrides the move, nextLocation & generateChild methods.
- Darter fish can only move forward. If there are two empty spaces in front of it, a darter fish "darts" two spaces forward. If there is only one empty space in front of it, it only moves one space. If it can't move at all because the cell in front of it is occupied or if it is located on the boundary of the environment, it does not move but changes direction by 180 degrees.
- Slow fish move like normal fish but a slow fish only has a 1/5 probability of moving in each timestep. A SlowFish has an additional private field probOfMoving that is set to 1.0/5.0.
Objective #7: (AB Students only). Understand the implementations of BoundedEnv and UnboundedEnv classes.
- The UnboundedEnv class is discussed in Ch. 5 of the College Board Marine Biology packet.
- BoundedEnv
- The BoundedEnv class has a two-dimensional array of Locatable's as its main private property.
- UnboundedEnv
- The UnboundedEnv class has an ArrayList as its main private property. This allows it to contain as many Fish as it wants to.
Objective #8: Be familiar with online resources that can help you study more about the Case Study.