AP ® Computer Science 2007–2008 Professional Development Workshop Materials Special Focus: GridWorld Case Study The College Board: Connecting Students to College Success The College Board is a not-for-profit membership association whose mission is to connect students to college success and opportunity. Founded in 1900, the association is composed of more than 5,000 schools, colleges, universities, and other educational organizations. Each year, the College Board serves seven million students and their parents, 23,000 high schools, and 3,500 colleges through major programs and services in college admissions, guidance, assessment, financial aid, enrollment, and teaching and learning. Among its best-known programs are the SAT®, the PSAT/ NMSQT®, and the Advanced Placement Program® (AP®).
The College Board is committed to the principles of excellence and equity, and that commitment is embodied in all of its programs, services, activities, and concerns. For further information, visit www. © 2007 The College Board. All rights reserved.
College Board, Advanced Placement Program, AP, AP Central, AP Vertical Teams, Pre-AP, SAT, and the acorn logo are registered trademarks of the College Board. AP Potential and connect to college success are trademarks owned by the College Board. All other products and services may be trademarks of their respective owners. Visit the College Board on the Web: www.
ii Table of Contents AP® Computer Science Special Focus Materials for 2007–08 GridWorld Case Study Table of Contents I. “Content-Enrichment” Articles A. The Design of the GridWorld Case Study. Instructional Units/Lessons A.
Early Exercises with GridWorld. Board Game Projects. Ant Farm Project. Save My Heart.
About the Authors .75 1 Introduction Introduction Debbie Carter, editor Lancaster Country Day School Lancaster, Pennsylvania Starting with the 2007–08 academic year, a new case study based on a grid—called “GridWorld”—will be a required part of the AP® Computer Science A and AB curricula. GridWorld provides a graphical environment in which students can experiment with different types of objects, observing the ways in which programming changes affect the objects’ behavior. Questions related to this case study will first appear on the 2008 AP Computer Science Exams. (AP Computer Science Newsletter, No.
We have commissioned a group of articles and instructional materials to help you prepare to use the GridWorld case study to teach computer science. Those of us who are familiar with the Marine Biology Simulation (MBS), the previous AP Computer Science case study, will find some familiar features in GridWorld. As Cay Horstmann explains in his article, “The Design of the GridWorld Case Study,” GridWorld’s design grew from what we learned from our experiences with the MBS in the classroom. We wanted more flexibility in the types of objects that could populate the world, as well as in the ways that they could be displayed.
An object in the world can • be displayed using an external graphic or via a graphics display class; • examine a list of other objects in the world and respond to one or more of them; • behave independently of other objects (or not at all). In “Integrating GridWorld,” Jill Kaminski gives us both an overview and some detailed practical advice about how we might effectively weave the case study throughout our AP Computer Science courses. Four additional authors, all computer science educators, have contributed hands-on instructional materials, with complete solutions, descriptions of teaching strategies, and suggestions for differentiation for students of varying needs. I would like to thank the contributors for their hard work and cooperation, as well as their continuing commitment to the AP Computer Science community.
We know that you’ll find many treasures in the next several pages, ready to be used to excite your AP Computer Science students! 3 Special Focus: GridWorld Case Study References College Board. AP Computer Science Course Description, May 2007, May 2008. AP Computer Science A and AB Newsletter.com/email/ap_compsci_n8490.html 4 The Design of the GridWorld Case Study The Design of the GridWorld Case Study Cay S. Horstmann San Jose State University San Jose, California Abstract In this article, I describe the rationale behind decisions that were made in the design of the GridWorld case study.
Knowing about these decisions can be useful to address student questions, or simply to satisfy your own curiosity. I also give information about the inner workings of the GUI that is helpful for designing your own worlds. A Brief History of GridWorld In 2004, the AP Computer Science Development Committee solicited proposals for a new case study. The committee received a number of interesting suggestions, but none of them had the flexibility of the Marine Biology Simulation (MBS) case study for producing exam questions.
Instead, we decided to make the MBS code more generic, to easily handle creatures other than fish, and to remove inessential classes. The redesign was governed by four themes: • Continuity. Teachers who are familiar with MBS should feel right at home in GridWorld. Students who see GridWorld for the first time should not be overwhelmed.
The framework should give rise to many kinds of exam questions. Enthusiasts should be able to design grid-based games, mazes, puzzles, simulations, and so on, without GUI programming. The first prototype of GridWorld appeared in March 2005. It consisted of the MBS code, with one important change—the ability to add GIF images instead of having to use Java graphics for drawing occupants.
It turns out that this ability was already present in the MBS GUI code, but it was well hidden. Figure 1 shows the very first GridWorld screen capture; note the cuddly critter and the frame title. The next months were the “Cambrian explosion” of GridWorld, with numerous exotic life forms appearing rapidly: rock hounds that search for rocks by recursively asking their neighbors, robots that drop flowers, potato-shaped cells, flocks of “boids,” and aliens. They became extinct, to be replaced with the bugs and critters that we know today.
5 Special Focus: GridWorld Case Study Figure 1—First GridWorld screen capture. The other major innovation was the direct manipulation interface that allows students to invoke constructors and methods. I had always admired this capability in BlueJ and experimented with controlling grid occupants from the BlueJ workbench. That proved too cumbersome, and I ended up implementing direct manipulation inside GridWorld.
Figure 2 shows another historic first—the first screen capture of the method menu. Note the method names and parameters! Figure 2—First screen capture of the method menu. 6 The Design of the GridWorld Case Study Chris Nevison and Barbara Cloud Wells joined in September 2005 to produce the narrative. They invented the crab and chameleon critters that show off the template method pattern.
Shortly after the narrative was released, we were delighted to receive a beautiful set of icons from Chris Renard, a student at the School for the Talented and Gifted of the Dallas Independent School District. The design of GridWorld has now been finalized, and the case study is ready for use in the 2007–08 school year. In this article, I will discuss some of the design decisions behind GridWorld and give tips for advanced users. Design Decisions In this section, I discuss some of the design decisions that were made during the development of GridWorld.
You may disagree with some decisions; some of your colleagues have done so rather vocally. You may want to discuss the pros and cons of some decisions with your class. Actors Store Their Location The MBS case study used an Environment that holds Locatable objects. This proved to be restrictive when writing exam questions.
We decided that we wanted a grid that can hold objects of any type. That means that the grid cannot ensure that the grid location and the occupant location are synchronized—the grid doesn’t know whether its occupants have locations. We attempted to solve this problem in an ingenious way, by passing the current location to the act method. public void act(Location loc, Grid<E> gr) The actors didn’t have to remember their location; the world reminded them.
Unfortunately, the location and grid ended up being passed into many other helper methods, leading to tedious code. In the end, we settled for actors that store their location, and supplied the putSelfInGrid and removeSelfFromGrid methods for synchronizing the actor and grid locations. As your students will undoubtedly find out, the put and remove methods of the Grid interface do not work for actors. The actors will throw exceptions when they find that their locations are not properly set.
The Out-Of-Subset instanceof Operator The instanceof operator is not in the AP Java subset, but it is used to sense the type of actors that are being processed. For example, a bug decides that it can move to a location by testing (neighbor == null) || (neighbor instanceof Flower) 7 Special Focus: GridWorld Case Study Going beyond the AP Java subset for the case study has precedent. The MBS case study used the protected modifier that is not in the subset. Nevertheless, it is not an ideal situation, and we explored alternatives.
Unfortunately, all of the alternatives required students to write code that was more cumbersome than the instanceof test. There is no point in making students learn cumbersome custom code when they could instead learn a Java feature. Note that we use the instanceof test in its most benign form, without the dreaded cast. That is, we do not have code of the form if (myActor instanceof Critter) { Critter myCritter = (Critter) myActor; //we don’t do in //GridWorld.
} Will other GridWorld users exhibit the same good taste and restraint? Only time will tell. Grid Types are Generic In the AB course, students look at the Grid interface and the AbstractGrid, BoundedGrid, and UnboundedGrid classes. They will see implementations of generic types, even though the implementation of generic types is not currently in the AP Java subset. In the A course, of course, there is no problem.
Students use Grid<E> much like ArrayList<E>. An old-style Grid that stores Object references requires unsightly casts. We felt that this advantage outweighed the slight complexity of type variables in the AB course. No Direction class The MBS case study had a Direction class for compass directions such as Direction.
The class had few interesting methods, and we eliminated it to minimize the number of classes that students see when they encounter GridWorld for the first time. Now the constants are placed in the Location class, which is admittedly not optimal. Eight neighbors A location in the grid has eight neighbors. In contrast, the MBS environment could be configured so that a location had four or eight neighbors, and some people even produced triangular or hexagonal grids.
This led to cumbersome exam questions since we always had to be explicit about the number of neighbors. We decided that hexagonal grids were cute but not useful enough to pay for the added complexity. 8 The Design of the GridWorld Case Study No Random Singleton Naive users often try to generate random numbers like this: int r = new Random().nextInt(n); // DON’T DO THAT! However, constructing many random number generators will lead to poorly distributed random numbers. The MBS case study had its own provider for a singleton random generator.
That is the right design if you are serious about controlling random number generation. In the context of the MBS narrative, it made sense to have repeatable sequences of pseudo-random numbers. However, the subtleties of random number generation are clearly not a central part of the AP Computer Science curriculum. In GridWorld, we simply generate random numbers with the call int r = (int) (n * Math.random()); You Can’t Save the World The MBS case study had a mechanism for saving and reloading simulation data, in files with contents such as bounded 7 5 Fish 3 2 North In GridWorld, that’s a lot harder since grid occupants can be arbitrary objects.
I solved the problem by using the XML file format for a specialized Java feature called “long-term beans persistence.” However, that led to a harder problem.