1 / 36

Shared Data

Shared Data. Gail Carmichael gail_c@scs.carleton.ca. Please Download:. www.scs.carleton.ca /~gail_c/comp1005/Robots1.zip www.scs.carleton.ca/~gail_c/comp1005/Birds1.zip. About Me. Sharing Resources. http://www.flickr.com/photos/marcosbessa/6260425373/. familyCar = new Car(“black”);

claus
Télécharger la présentation

Shared Data

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. Shared Data Gail Carmichael gail_c@scs.carleton.ca

  2. Please Download: www.scs.carleton.ca/~gail_c/comp1005/Robots1.zip www.scs.carleton.ca/~gail_c/comp1005/Birds1.zip

  3. About Me

  4. Sharing Resources http://www.flickr.com/photos/marcosbessa/6260425373/

  5. familyCar = new Car(“black”); mom.car = familyCar; dad.car = familyCar; teen.car = familyCar;

  6. teen.car.crash(); // Now the family car is not drivable! if (!mom.car.drivable()) mom.isStranded(); if (!dad.car.drivable()) dad.isStranded();

  7. Robots Example

  8. Examine code in Robots1.pde www.scs.carleton.ca/~gail_c/comp1005/Robots1.zip

  9. Beacon Beacon Beacon Robot Robot Robot

  10. Pros and cons?

  11. Let’s add movement (demo first)

  12. Step 1: Trigonometry to move forward in current direction x = x + speed * cos(direction) y = y + speed * sin(direction)

  13. Step 2: Change direction to head for beacon

  14. crossProduct = (r'x- rx)(gy- ry) - (r'y- ry)(gx- rx)

  15. Step 3: Add random error for realism amountToTurn = θ + random(θ/4) - (θ/4)/2

  16. Try implementing the move() method… x = x + speed * cos(direction) y = y + speed * sin(direction) crossProduct = (r'x- rx)(gy- ry) - (r'y- ry)(gx- rx) positive → left hand turn negative → right hand turn amountToTurn= θ + random(θ/4) - (θ/4)/2

  17. Let’s add drag-and-drop for the beacons (demo first) www.scs.carleton.ca/~gail_c/comp1005/Robots2.zip

  18. What if we want multiple robots per beacon? www.scs.carleton.ca/~gail_c/comp1005/Robots3.zip

  19. Beacon Beacon Robot Robot Robot

  20. Implement multiple robots per beacon.

  21. Bird Animation Example

  22. Please Download: www.scs.carleton.ca/~gail_c/comp1005/Birds1.zip Study the code, run the program.

  23. How is the data in the program set up? Pros and cons?

  24. Where can data be shared?

  25. Implement sharing so each bird points to the same set of animation frames.

  26. Separating Shared Data

  27. Person molly = new Person(“Molly”); Person henry = new Person(“Henry”); Person[] personList = new Person[2]; personList[0] = molly; personList[1] = henry;

  28. Person[] personListCopy = new Person[2]; for (inti=0; i < personList.length; i++) { personListCopy[i] = personList[i]; }

  29. personListCopy[0].name = “Mallory”; // Prints Mallory System.out.println(personList[0].name);

  30. Person[] personListCopy = new Person[2]; for (inti=0; i < personList.length; i++) { personListCopy[i] = new Person(personList[i].name); }

  31. personListCopy[1].name = “Harry”; // Still prints Henry since it’s unchanged System.out.println(personList[1].name);

  32. Shallow Copies vs Deep Copies

  33. Thanks! Gail Carmichael gail_c@scs.carleton.ca

More Related