AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

34
AP ® Marine Biology Simulation Case Study Alyce Brady Alyce Brady Kalamazoo College Kalamazoo College

Transcript of AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Page 1: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

AP® Marine Biology SimulationCase Study

Alyce BradyAlyce Brady

Kalamazoo CollegeKalamazoo College

Page 2: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Case Study as Educational Tool

describes a real situationdescribes a real situation provides an interesting example from which provides an interesting example from which

to draw certain lessonsto draw certain lessons provides a context for comparing theoretical provides a context for comparing theoretical

ideas against real-world experienceideas against real-world experience

Page 3: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Case Studies in AP CS

try to give the illusion of being a real-world try to give the illusion of being a real-world situation (MBCS) or real-world tool situation (MBCS) or real-world tool (BigInt)(BigInt)

must be simplified and cleaned-up enough must be simplified and cleaned-up enough to be readable and understandable for HS to be readable and understandable for HS studentsstudents

Page 4: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Benefits of an AP CS case study

example of a largish program example of a largish program opportunity to discuss tradeoffs (design, opportunity to discuss tradeoffs (design,

performance issues, readability, etc)performance issues, readability, etc) example of good coding, design, and example of good coding, design, and

documentation practicedocumentation practice approximation of master/apprentice relationshipapproximation of master/apprentice relationship context for covering design and testingcontext for covering design and testing rich source of assignments rich source of assignments source of non-trivial exam questionssource of non-trivial exam questions

Page 5: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Goals for Java MBS

provide benefits described in prev. slidesprovide benefits described in prev. slides be similar to C++ MBCSbe similar to C++ MBCS

teachers can pick it up fasterteachers can pick it up faster can use it as they learn Javacan use it as they learn Java

be different from C++ MBCSbe different from C++ MBCS highlight differences in languagehighlight differences in language highlight differences in curriculumhighlight differences in curriculum

Page 6: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

The Story A CS student, Pat, gets a summer job working for A CS student, Pat, gets a summer job working for

marine biologists.marine biologists. Hired to enhance an existing program that Hired to enhance an existing program that

simulates fish movement in a bounded simulates fish movement in a bounded environment.environment. Needs to understand existing programNeeds to understand existing program Designs, codes, and tests modificationsDesigns, codes, and tests modifications

Occasionally Pat turns to an experienced Occasionally Pat turns to an experienced programmer, Jamie, for help.programmer, Jamie, for help.

Narrative is Pat’s report of summer job.Narrative is Pat’s report of summer job.

Page 7: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

The Package

Code for the “existing” programCode for the “existing” program Source for core classesSource for core classes Jar files for “black box” & GUI classesJar files for “black box” & GUI classes

Javadoc documentation for most classesJavadoc documentation for most classes Data FilesData Files Instructions for compiling/runningInstructions for compiling/running Narrative (pdf file)Narrative (pdf file)

Page 8: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

The Modules (Chapters)

Experiment with existing program (run it)Experiment with existing program (run it) Guided tour of the code by JamieGuided tour of the code by Jamie Add breeding and dyingAdd breeding and dying Add two new kinds of fish (inheritance)Add two new kinds of fish (inheritance)

Provide alternative representations Provide alternative representations (unbounded environment, others)(unbounded environment, others)

Page 9: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

First Day on the Job

““[The program] was designed to help the [The program] was designed to help the marine biologists study fish movement in a marine biologists study fish movement in a bounded environment, such as a lake or a bounded environment, such as a lake or a bay.”bay.”

Jamie not available until the next day.Jamie not available until the next day. Pat is given instructions for running the Pat is given instructions for running the

program and told where to find data files.program and told where to find data files.

Chapter 1

Page 10: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Exercise 1

Let’s see it run!Let’s see it run!

Complete the table on p. 11.Complete the table on p. 11.

Page 11: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Guided Tour

““The biologists think of the environment as The biologists think of the environment as a rectangular grid, with fish moving from a rectangular grid, with fish moving from cell to cell in the grid. Each cell contains cell to cell in the grid. Each cell contains zero or one fish.”zero or one fish.”

Chapter 2

Page 12: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

What classes are necessary?

To model To model fishfish swimming in a swimming in a bounded bounded environmentenvironment, the program has , the program has FishFish objects objects and an and an EnvironmentEnvironment object. object.

The purpose of the program is to The purpose of the program is to simulatesimulate fish moving in the environment, so the fish moving in the environment, so the program also has a program also has a SimulationSimulation object. object.

There are other useful, but less important There are other useful, but less important "utility classes.""utility classes."

Chapter 2

Page 13: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

One step in the simulation

Fish object id

color

environment

toString

isInEnv

direction

act

location

EnvDisplayobject

showEnvEnvironmentobject

(partial list of methods)

allObjects

objectAt

isEmpty

add

neighborsOf

getNeighbor

getDirection

remove

recordMove

Fish object id

color

environmen

toString

isInEnv

direction

act

locationFish object id

color

environment

toString

isInEnv

direction

act

locationFish object id

color

environment

toString

isInEnv

direction

act

location

Simulationobject step

Chapter 2

Page 14: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

What do core classes look like?

Simulation: Simulation: stepstep method - very simple method - very simple loop through all the fish (see p. 21)loop through all the fish (see p. 21)

Environment: black box; only look at Environment: black box; only look at class class documentationdocumentation (until Chap 5) (until Chap 5)

Fish: Fish: has color, directionhas color, direction movemove method is a little more method is a little more

complicated, has more helper methodscomplicated, has more helper methods

Chapter 2

Page 15: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Constructors

Initialize instance variablesInitialize instance variables Add the fish to the environmentAdd the fish to the environment

a fish knows about its environmenta fish knows about its environment must always be in an environment to be in must always be in an environment to be in

a a consistentconsistent state state See pp. 27-28See pp. 27-28

Chapter 2

Page 16: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

move method

Get next location to move to (call Get next location to move to (call nextLocationnextLocation))

If next location is different from this If next location is different from this location,location, move there (call move there (call changeLocationchangeLocation)) change direction (call change direction (call changeDirectionchangeDirection))

Chapter 2

Page 17: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

nextLocation method

Get list of empty neighboring locations (call Get list of empty neighboring locations (call emptyNeighborsemptyNeighbors))

Remove location behind fish from listRemove location behind fish from list If there are any empty neighbors left, If there are any empty neighbors left,

randomly choose one; otherwise return randomly choose one; otherwise return current locationcurrent location

Chapter 2

Page 18: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Exercise 2

Let’s see some sample exercises.Let’s see some sample exercises. Do Questions 1, 7, 8, 9 on p. 31Do Questions 1, 7, 8, 9 on p. 31 Read over exercises on p. 32Read over exercises on p. 32

What’s the difference between Analysis What’s the difference between Analysis Questions and Exercises?Questions and Exercises?

Page 19: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Breeding and Dying

Problem Specification: A fish should ...Problem Specification: A fish should ... have a 1 in 7 chance of breeding,have a 1 in 7 chance of breeding, breed into all empty neighboring locations,breed into all empty neighboring locations, attempt to move when it does not breed,attempt to move when it does not breed, never move backwards, andnever move backwards, and have a 1 in 5 chance of dying after it has have a 1 in 5 chance of dying after it has

bred or moved.bred or moved.

Chapter 3

Page 20: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Breeding and Dying

Pseudo-code for act methodPseudo-code for act method

if this is the 1 in 7 chance of breedingif this is the 1 in 7 chance of breeding

call the call the breedbreed method method

elseelse

call the move methodcall the move method

if this is the 1 in 5 chance of dyingif this is the 1 in 5 chance of dying

call the call the diedie method method

Chapter 3

Page 21: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Breeding and Dying

Test PlanTest Plan Testing random behavior (Chap 2)Testing random behavior (Chap 2) Black-box testingBlack-box testing Code-based testingCode-based testing

Chapter 3

Page 22: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Breeding and Dying

Sample Exercise:Sample Exercise:Introduce a new instance variable in Fish keeping Introduce a new instance variable in Fish keeping track of how many times a fish bred. Initialize it track of how many times a fish bred. Initialize it in the constructor and increment it in the breed in the constructor and increment it in the breed method. Modify the debugging statement in the method. Modify the debugging statement in the die method to print the number of times the fish die method to print the number of times the fish bred. Run your simulation for 20 timesteps. What bred. Run your simulation for 20 timesteps. What is the maximum number of times a fish bred in is the maximum number of times a fish bred in your test run? What is the minimum number? Are your test run? What is the minimum number? Are these values you would have expected given the these values you would have expected given the probability of breeding in each timestep?probability of breeding in each timestep?

Chapter 3

Page 23: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Exercise 3

Let’s see it run!Let’s see it run!

Page 24: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Specialized Fish

Different patterns of movementDifferent patterns of movement Darters (Darters (DarterFishDarterFish)) Slow fish (Slow fish (SlowFishSlowFish))

InheritanceInheritance Dynamic BindingDynamic Binding

Chapter 4

Page 25: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Specialized Fish

DarterFishDarterFish darts two cells forward if darts two cells forward if

possiblepossible or darts one cell forwardor darts one cell forward or reverses direction or reverses direction

(without moving)(without moving)

Chapter 4

DarterFish object id

color

environment

toString

randomColor

isInEnv

direction

act

location

breed

generateChild

move

nextLocation

emptyNeighbors

changeLocation

changeDirection

die

move

nextLocation

generateChild

Page 26: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Specialized Fish

SlowFishSlowFish has only a 1 in 5 chance has only a 1 in 5 chance

of moving out of current of moving out of current cellcell

otherwise movement is otherwise movement is the same as the same as FishFish

Chapter 4

SlowFish object id

color

environment

toString

randomColor

isInEnv

direction

act

location

breed

generateChild

move

nextLocation

emptyNeighbors

changeLocation

changeDirection

die

nextLocation

generateChild

Page 27: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Exercise 4

Let’s see it run!Let’s see it run!

Let’s see some sample exercises.Let’s see some sample exercises. Questions 1 and 2 on p. 75Questions 1 and 2 on p. 75 Read over exercises on p. 76Read over exercises on p. 76

Page 28: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

“A” Exam Summary

Class ImplementationsClass Implementations Simulation Simulation (Chap 2)(Chap 2) FishFish (Chaps 2 & 3) (Chaps 2 & 3) DarterFish DarterFish (Chap 4)(Chap 4) SlowFish SlowFish (Chap 4)(Chap 4)

Class DocumentationClass Documentation A number of utility classesA number of utility classes

Page 29: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Environment Implementations

Multiple environment implementationsMultiple environment implementations Environment InterfaceEnvironment Interface Bounded: 2D array (matrix) -- existingBounded: 2D array (matrix) -- existing Unbounded: ArrayList of Fish -- Pat Unbounded: ArrayList of Fish -- Pat

develops this implementationdevelops this implementation

Chapter 5

Page 30: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Environment Implementations

ExercisesExercises Very large bounded environment (list or Very large bounded environment (list or

sparse matrix)sparse matrix) Sorted list for unbounded environment Sorted list for unbounded environment

with binary searchwith binary search BST for unbounded environmentBST for unbounded environment Hash map for unbounded environmentHash map for unbounded environment

Chapter 5

Page 31: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

“AB” Exam Summary

Classes and documentation from “A” ExamClasses and documentation from “A” Exam Additional Class Interfaces/ImplementationsAdditional Class Interfaces/Implementations

EnvironmentEnvironment BoundedEnvBoundedEnv UnboundedEnvUnboundedEnv

Class DocumentationClass Documentation One new utility classOne new utility class

Page 32: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Exercise 5

Let’s see it run!Let’s see it run!

Let’s see some sample exercises.Let’s see some sample exercises. Questions 1 and 2 on p. 80Questions 1 and 2 on p. 80 Read over exercises on p. 104Read over exercises on p. 104

Page 33: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Key Features

Introduces “large” program to intro studentsIntroduces “large” program to intro students Covers key topics; Covers key topics; highlights new oneshighlights new ones

Object interactionObject interaction Inheritance, dynamic bindingInheritance, dynamic binding InterfacesInterfaces Design issuesDesign issues TestingTesting Data RepresentationData Representation

Page 34: AP ® Marine Biology Simulation Case Study Alyce Brady Kalamazoo College.

Want to use it?

www.collegeboard.com/ap/students/www.collegeboard.com/ap/students/

compsci/download.htmlcompsci/download.html