1 / 26

Marine Biology Case Study

Marine Biology Case Study. AP Computer Science. Once Upon A Time…. There was an Environment. In this Environment lived…. A Locatable object. (Anything that knows where it is). Some Environments are Bounded Environments. And COLUMNS. 0. 1. 2. 3. 0. With ROWS. 1. 2. 3.

kueng
Download Presentation

Marine Biology Case Study

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Marine BiologyCase Study AP Computer Science

  2. Once Upon A Time… There was an Environment

  3. In this Environment lived… A Locatable object (Anything that knows where it is)

  4. Some Environments areBounded Environments And COLUMNS 0 1 2 3 0 With ROWS 1 2 3

  5. It turns out thatFISHknow where they are. I’m Locatable

  6. So FISH can live inBounded Environments 0 1 2 3 0 1 2 3

  7. FISH know… Direction is a Class.It is similar to Color: Direction.EAST Direction.WEST Direction.NORTH Direction.SOUTH MY VARIABLES privateEnvironmenttheEnv;privateintmyId;privateLocationmyLoc;privateDirectionmyDir;privateColormyColor; My EnvironmentMy ID NumberMy LocationThe Direction I’m FacingMy Color Location is a Class that has already been written.You can make a new Location like so:Location loc = new Location(2, 3);

  8. Bounded Environments Keep track of HOW MANY objects are living in the environment:private intobjectCount; 0 1 2 3 0 1 objectCount 2 0 3 Keep all their Locatable objects in a 2 Dimensional array.

  9. Bounded Environments 0 1 2 3 0 null null null null 1 null null null null objectCount 2 null null null null 0 3 null null null null If NOBODY lives at a certain spot,NULL lives there.

  10. Bounded Environments 0 1 2 3 0 null null null null 1 null null null null objectCount 2 null null null null 0 1 2 3 4 3 null null null null Locatable objects can be ADDED to the Environment.

  11. Playing with an Environment Row, Column Let’s make an Environment: BoundedEnv env = new BoundedEnv(4,5);Location loc1 = new Location(3,4);Location loc2 = new Location(3,3);Location loc3 = new Location(2,4);Direction dir1 = env.getDirection(loc1, loc2);Direction dir2 = env.getDirection(loc3, loc1);Direction dir3 = dir2.reverse(); System.out.println(dir1);System.out.println(dir2);System.out.println(dir3); Parameters = Rows, Columns • Create a new class called EnvironmentTest. (give it a main) Prints:WestSouthNorth

  12. Building A Fish A Fish memorizes its Location and its Environment. Then it HAS to tell its Environment to put it in the right place! All Fish constructors utilize an initialize function to do the real work! private voidinitialize(Environmentenv,Locationloc,Directiondir,Colorcol){theEnv=env;myId=nextAvailableID;nextAvailableID++;myLoc=loc;myDir=dir;myColor=col;theEnv.add(this);}

  13. Building a Fish Let’s Build Some Fish Fishf1=newFish(env,loc2);Fishf2=newFish(env,newLocation(0,4));System.out.println(env.objectAt(loc2));System.out.println(env.isEmpty(f2.location()));System.out.println(env.objectAt(loc1));env.remove(f1);System.out.println(env.objectAt(f1.location())); OUTPUT: 1(3, 3)South false null null

  14. Act Like a Fish This simulation is constantly asking the Fish Objects to act( ) Let’s see how a Fish acts… public voidact(){if(isInEnv())move();} The Fish asks his Environment if he is in there.If so, the Fish moves.

  15. Move Like a Fish • How a Fish Moves: • Decide my next location • Check to see if it is the same as current location. • Change location • Ask my environment what direction I went • Turn to face that direction

  16. Move Like a Fish ?????????? protected voidmove(){ LocationnextLoc=nextLocation();if(!nextLoc.equals(location())){LocationoldLoc=location();changeLocation(nextLoc); DirectionnewDir=environment().getDirection(oldLoc,nextLoc);changeDirection(newDir); } else Debug.println(" Does not move."); }

  17. Where Am I Going? • How a Fish Decides on a Next Location: • Look up, down, left, and right for empty spaces next to me. (These are the potential candidates) • Eliminate the Location behind me as a candidate(It’s hard to swim backwards) • Randomly select one of the remaining candidates • If the list is empty, I stay put.

  18. Where Am I Going? protectedLocationnextLocation(){ArrayListemptyNbrs=emptyNeighbors();DirectionoppositeDir=direction().reverse(); LocationlocationBehind= environment().getNeighbor(location(),oppositeDir); emptyNbrs.remove(locationBehind);if(emptyNbrs.size()==0)returnlocation(); RandomrandNumGen=RandNumGenerator.getInstance();intrandNum=randNumGen.nextInt(emptyNbrs.size());return(Location)emptyNbrs.get(randNum);} Now let’s get back on the move( )…

  19. Move Like a Fish OK ?????????? protected voidmove(){ LocationnextLoc=nextLocation();if(!nextLoc.equals(location())){LocationoldLoc=location();changeLocation(nextLoc); DirectionnewDir=environment().getDirection(oldLoc,nextLoc);changeDirection(newDir); } else Debug.println(" Does not move."); }

  20. Change Location Change my Location variable. Tell my Environment to: Place me at my Location in the grid Set my old Location in the grid to NULL protected voidchangeLocation(Location newLoc){ LocationoldLoc=location();myLoc=newLoc;environment().recordMove(this,oldLoc);}

  21. Act More Like a Fish • Fish do more than just move. • This is how a real Fish acts: • Either Breed(1 in 7 chance) • Birth children into ALL empty neighboring spaces. • OrMove • Business as usual • ThenmaybeDie(1 in 5 chance) • Get out of the environment.

  22. Breeding Fish Assign values in initialize( ) function probOfBreeding = 1.0/7;probOfDying = 1.0/5; • Let’s add a breed( ) method to FISH: • protected: only the Fish should be able to cause breeding to happen. • boolean: returns true if children were created. (so the Fish knows if it should try to move) • FIRST: add two member variables: private doubleprobOfBreeding;private doubleprobOfDying;

  23. Breeding Fish • How Fish Breed: • Decide if it is time to breed (random num) • Get all the empty neighboring Locations • Create new Fish in each of these Locations • Return true or false

  24. Breeding Fish protected booleanbreed(){if(Math.random()<probOfBreeding){ ArrayListemptyNbrs=emptyNeighbors();if(emptyNbrs.size()==0)return false;for(inti=0;i<emptyNbrs.size();i++){ Locationloc=(Location)emptyNbrs.get(i);newFish(environment(),loc, theEnv.randomDirection(), myColor); }return true; }else return false;}

  25. Dying Fish Make a die( ) function protected void die(){ theEnv.remove(this);} • Fish “DIE” when they are no longer in the Environment. • They will still remember what Environmentthey WERE in even though it no longer contains them.

  26. Updating act() Original Act public voidact(){if(isInEnv())move(); } public voidact(){if(!isInEnv())return;if(!breed())move();if(Math.random()<probOfDying)die();}

More Related