marine biology simulation part ii: assignment, milestone 1 dung “zung” nguyen mathias ricken...

26
Marine Biology Marine Biology Simulation Simulation Part II: Assignment, Part II: Assignment, Milestone 1 Milestone 1 Dung “Zung” Nguyen Dung “Zung” Nguyen Mathias Ricken Mathias Ricken Stephen Wong Stephen Wong TeachJava 2004! TeachJava 2004! June 25, 2004 June 25, 2004

Upload: christopher-rose

Post on 04-Jan-2016

219 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Marine Biology SimulationMarine Biology SimulationPart II: Assignment, Milestone 1Part II: Assignment, Milestone 1

Dung “Zung” NguyenDung “Zung” NguyenMathias RickenMathias RickenStephen WongStephen Wong

TeachJava 2004!TeachJava 2004!

June 25, 2004June 25, 2004

Page 2: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Rice MBS AssignmentRice MBS Assignment

Target AudienceTarget AudienceStudents at the end of an objects-first CS2Students at the end of an objects-first CS2

Length of AssignmentLength of AssignmentTwo milestonesTwo milestones

Approximately 10 hours of workApproximately 10 hours of work

Probably supported by one or two labsProbably supported by one or two labs

Page 3: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Studying the Case StudyStudying the Case Study

SimpleFishUnboundedEnv Variant Fish Behavior

Variant Environment Behavior

Re-implement as per given specs.

Re-implement as per given specs.

White-box Framework:

Extension by Subclassing

Page 4: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Strategy Pattern

+ void move()

+ void move() + void move()

Static Behavior! Separate the variants from the invariants

Studying the Case StudyStudying the Case Study

Page 5: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Rice MBS AssignmentRice MBS Assignment

Milestone 1Milestone 1Part 1: KnightFishPart 1: KnightFishAdd a new fish species to the simulationAdd a new fish species to the simulationPart 2: WrappingEnvPart 2: WrappingEnvAdd a new kind of environmentAdd a new kind of environment

Milestone 2Milestone 2Part 3: InternalsPart 3: InternalsRe-implement parts of the simulation frameworkRe-implement parts of the simulation frameworkPart 4: Behavior StrategiesPart 4: Behavior StrategiesRefactor framework to allow dynamic behavior changesRefactor framework to allow dynamic behavior changes

Page 6: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

PreparationPreparation

Read assignment documentRead assignment document

Download materials for Milestone 1Download materials for Milestone 1milestone1.zipmilestone1.zipUnzip into empty directoryUnzip into empty directory

Open DrJavaOpen DrJavaCopy your drjava.jar into the project directoryCopy your drjava.jar into the project directory

Page 7: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Configuring DrJavaConfiguring DrJava

Add RiceMBSfw.jar file to “Extra Classpath”Add RiceMBSfw.jar file to “Extra Classpath”Edit, Preferences, Resource LocationsEdit, Preferences, Resource Locations

Click on “Add” buttonClick on “Add” button

Click on RiceMBSfw.jar file and “Select”Click on RiceMBSfw.jar file and “Select”

Click on “OK” at the bottom of the dialogClick on “OK” at the bottom of the dialog

Now the framework has been added and Now the framework has been added and can be used without sourcecan be used without source

Make sure you run in “Full Java”Make sure you run in “Full Java”

Page 8: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Running the SimulationRunning the Simulation

In the “Interactions” pane, typeIn the “Interactions” pane, typejava controller.MBSControllerjava controller.MBSController

Play around with the simulationPlay around with the simulationCreate new environmentCreate new environment

Edit environmentEdit environment

Add fishAdd fish

Run simulationRun simulation

Page 9: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 1: KnightFishPart 1: KnightFish

Randomly pick Randomly pick one of 8 targetsone of 8 targets

Example: 3Example: 3

11 22

33

44

5566

77

88

Page 10: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 1: KnightFishPart 1: KnightFish

Turn into right Turn into right directiondirection

Attempt one Attempt one step forwardstep forward

If blocked, doneIf blocked, done

If open, go thereIf open, go there

33

Page 11: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 1: KnightFishPart 1: KnightFish

Attempt a Attempt a second stepsecond step

If blocked, doneIf blocked, done

If open, go thereIf open, go there

Turn into right Turn into right directiondirection

33

Page 12: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

33

Part 1: KnightFishPart 1: KnightFish

Attempt the third Attempt the third and last stepand last step

If blocked, doneIf blocked, done

If open, go thereIf open, go there

Page 13: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

33

Part 1: KnightFishPart 1: KnightFish

Moves almost Moves almost like a knight, like a knight, except…except…

No jumpingNo jumping

If blocked, might If blocked, might stop on the waystop on the way

Stop here

Stop here

Stop here

Page 14: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 1: KnightFishPart 1: KnightFishComplicated behaviorComplicated behavior

Procedural programming: many nested if-Procedural programming: many nested if-statementsstatements

Delegation methodDelegation methodNested visitorsNested visitorsCannot forget to handle a situation (compiler Cannot forget to handle a situation (compiler error!)error!)

Process flow diagrammingProcess flow diagrammingMakes designing complex algorithm a Makes designing complex algorithm a systematic processsystematic process

Page 15: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 1: KnightFishPart 1: KnightFish

Students do this assignmentStudents do this assignmentMost of the code can be taken from existing Most of the code can be taken from existing examplesexamplesFor lack of time, we will do something simplerFor lack of time, we will do something simpler

Fish attempts to move forwardFish attempts to move forwardIf blocked, turn around 180 degreesIf blocked, turn around 180 degreesIf open, go thereIf open, go there

Open TeachFish.java in model.fishOpen TeachFish.java in model.fish

Page 16: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 1: KnightFishPart 1: KnightFishprotected void move() tryMoveFwd(

,

);}

new ILambda() { public Object apply(Object moveCmd) { ((ILambda)moveCmd).apply(null); return null; }}

Command to execute if blocked

Command to execute if unblocked

new ILambda() { public Object apply(Object notUsed) { turnRight(Math.PI); return null; }}

Ask local environment to move forward

((ILambda)moveCmd).apply(null);

Tell the local environment to

actually move the fish

Page 17: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 1: KnightFishPart 1: KnightFish

Compile the fileCompile the file

Run the simulationRun the simulation

Create a new environment, and edit itCreate a new environment, and edit it

Add a fishAdd a fishSelect “Add…”Select “Add…”

Enter model.fish.TeachFish and hit “Create”Enter model.fish.TeachFish and hit “Create”

Click somewhere on the mapClick somewhere on the map

Run the simulationRun the simulation

New fish class loaded at runtime!

Page 18: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 2: WrappingEnvPart 2: WrappingEnv

Grid-based, Grid-based, bounded, and bounded, and rectangular rectangular environmentenvironment

If the fish leaves If the fish leaves on one side, it on one side, it re-enters from re-enters from the other sidethe other side

Topology: torus Topology: torus (donut)(donut)

Page 19: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 2: WrappingEnvPart 2: WrappingEnv

Very similar to BoundedEnvVery similar to BoundedEnv

In BoundedEnv, the Location class In BoundedEnv, the Location class computes a neighboring location by adding computes a neighboring location by adding a direction vectora direction vector to its own coordinates.to its own coordinates.

The new coordinates might be inside or outside The new coordinates might be inside or outside the environment.the environment.

Can new coordinates ever be outside in Can new coordinates ever be outside in WrappingEnv?WrappingEnv?

Answer is part of class assignment

and has been removed

Page 20: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 2: WrappingEnvPart 2: WrappingEnv

The leftmost x-coordinate that is still inside The leftmost x-coordinate that is still inside the environment is 0. The rightmost one is the environment is 0. The rightmost one is (width-1).(width-1).

In BoundedEnv, the x-coordinate of the In BoundedEnv, the x-coordinate of the neighbor from there to the left is -1.neighbor from there to the left is -1.

What’s the x-coordinate of the left neighbor in What’s the x-coordinate of the left neighbor in WrappingEnv?WrappingEnv?

Answer is part of class assignment and has

been removed

Page 21: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 2: WrappingEnvPart 2: WrappingEnv

The necessary changes are centralized in a The necessary changes are centralized in a single method, single method, WrappingEnv.Location.getNeighbor.WrappingEnv.Location.getNeighbor.

Since BoundedEnv and WrappingEnv are Since BoundedEnv and WrappingEnv are so similar, what OO technique can we apply so similar, what OO technique can we apply to maximize code-reuse?to maximize code-reuse?

Answer is part of class assignment and has

been removed

Page 22: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 2: WrappingEnvPart 2: WrappingEnv

Need to write constructorsNeed to write constructors

The framework uses factory methodsThe framework uses factory methodsAbstract creationAbstract creation

To complete the environment, we have to override To complete the environment, we have to override the factory methodsthe factory methods

WrappingEnv.makeLocationWrappingEnv.makeLocation

WrappingEnv.makeEnvFactory (code provided for WrappingEnv.makeEnvFactory (code provided for makeEnvFactory)makeEnvFactory)

Students’ code is still based on existing code, but Students’ code is still based on existing code, but cannot simply be copied and modified anymorecannot simply be copied and modified anymore

Page 23: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 2: WrappingEnvPart 2: WrappingEnv

Open WrappingEnv.java in sysModel.envOpen WrappingEnv.java in sysModel.envThe project was split into “model” and a The project was split into “model” and a “sysModel” packages to clearly separate “sysModel” packages to clearly separate system code (environments) from user code system code (environments) from user code (fish)(fish)

Fish more likely to get changed than Fish more likely to get changed than environmentsenvironments

Edit the WrappingEnv.Location.getNeighbor Edit the WrappingEnv.Location.getNeighbor methodmethodStudents would have to do a little bit moreStudents would have to do a little bit more

Page 24: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 2: WrappingEnvPart 2: WrappingEnv

Code is part of a class assignmentCode is part of a class assignment

and has been removedand has been removed

Page 25: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

Part 2: WrappingEnvPart 2: WrappingEnv

Compile the fileCompile the file

Run the simulationRun the simulation

Create a new environmentCreate a new environmentSelect “Add…”Select “Add…”

Enter sysModel.env.WrappingEnv and hit Enter sysModel.env.WrappingEnv and hit “Create”“Create”

Pick a size and create the environmentPick a size and create the environment

Add fishAdd fish

Run the simulationRun the simulation

Page 26: Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004

End of Milestone 1End of Milestone 1

Students submit code for Part 1 and Part 2Students submit code for Part 1 and Part 2

Material for Milestone 2 gets released after Material for Milestone 2 gets released after submission deadlinesubmission deadline

Contains solutions for Milestone 1Contains solutions for Milestone 1