1 / 28

MBCS Chapter 2

MBCS Chapter 2. Analysis Questions 7-10. After doing Exercise 3 …. You know the neighbors. Exercise Set 3.. Basic code. Location loc1 = new Location (0,0) ArrayList nbrs1 = env.neighborsOf(loc1) System.out.println(“Location “+loc1 + “ has “ + nbrs1.size() + “ neighbors.”)

ebony
Download Presentation

MBCS Chapter 2

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. MBCS Chapter 2 Analysis Questions 7-10

  2. After doing Exercise 3 … You know the neighbors

  3. Exercise Set 3.. Basic code • Location loc1 = new Location (0,0) • ArrayList nbrs1 = env.neighborsOf(loc1) • System.out.println(“Location “+loc1 + “ has “ + nbrs1.size() + “ neighbors.”) • For(i=0;i<nbrs1.size();i++) • System.out.print (nbrs1.get(i) + “ “);

  4. Exercise 3 Answers…

  5. The code runs and displays the answers • Neighbors: • For (0,0) (0,1) and (1,0) • For 0,1: (0,2) – (1,1) and (0,0) • For 1,1: (0,1) – (1,2) – (2,1) – (1, 0) • Depends on the size of the environment • 4 corners = 2 neighbors • All other edges = 3 neighbors • All inner locations = 4 neighbors

  6. Answers to Analysis Set 7

  7. 1 1 2 3 None 1,1 1,1 2,0 0,0 1,1 2,0 In each diagram 1,0 has 3 neighbors

  8. Notice how changes happen • Look closely at change Direction and change Location methods. (page 39)

  9. Set 8 answers

  10. For the fish at 2,2 • Move will call nextLocation • nextLocation calls empty Neighbors • Empty Neighbors will get a list of the 4 neighbors around 2,2 and return the empty list 1,2 2,1 and 2,3 • Oppositedir will be set behind the fish at 2,1 • Index is randomly chosen and one of the empty locations is selected.

  11. myAge • Would have to initialize in the initialize method. • Probably increment in act • Need an accessor method • Modify toString to add the new info.

  12. Do exercise set 4 pg 40 • Write a color change method • Create a way to change color Explore some color methods myColor.lighter() myColor.darker() What does it do?

  13. Exercise set 4 answers

  14. changeColor method Public void changeColor(Color newColor) { myColor = newColor; }

  15. Using the third Fish constructor Color col = new Color(200,50,50) Fish f1= new Fish(env, loc1, dir1, col); What color is this??? Or When you create fish Fish f1 = new Fish(env, new Location (5,8), new Direction(“west”), new Color(100,150,50))

  16. Possible variation on act • Use a boolean oddOReven fish instance variable If(oddOReven) {changeColor(Color.red); oddOreven = true; } Else {changeColor(Color.yellow); oddOReven = false;}

  17. The test plan (pg 41-43) • One test run can describe many test cases • Use fish.dat as the basis for a test run to cover many cases • The “Fish in every location” describes a single setup with a single run. • “fish with no empty neighbors” could be part of another set, but be careful that the fish around it don’t move before it’s tested.

  18. Set 9 answers

  19. An initial configuration file could contain a fish outside the bounded environment, or multiple fish in a single location. • One test case for each 3 conditions • File with a single fish in an environment > 1X2 = multiple neighbors & empty neighbor • Fish everywhere = multiple neighbors, non-empty • Fish with one empty to front or side

  20. Fish with one empty neighbor behind only • Fish with 2 empty neighbors • Fish with 3 empty neighbors • 1 row, 2 col or 2 rows, 1 col • One full, one empty

  21. Do exercise set 5 (pg 45) • Save your data files • Always start with the same data files so fish start in same configuration. If same seed number for random is used, color and movement should be the same every time. • Text output will tell you ID, location and direction of fish at each step. • Appendix C has a table to fill in

  22. Possible Exercise 5 answers • Invalidenv.dat • Bounded 12 • Emptyenv.dat • Bounded 12 12 • One fish can be found in onefish.dat • Twofishsameloc.dat • Bounded 12 12 • Fish 5 5 North • Fish 3 3 South • Fish 5 5 East • Fullenv.dat is in data file for every loc full

  23. 7. Test cases for move • Any fish with no available location where it can move should remain in its current location • A fish with at least one available location should move there

  24. Test cases for nextLocation • A fish with no empty neighbors , or only an empty neighbor behind should return its current location • A fish with >= 1 valid location should return one of those locations

  25. Test cases for emptyNeighbors • No empty neighbors returns empty ArrayList • A fish with <= 4 adjacent locations full of fish should return an empty ArrayList • A fish with >= 1 empty adjacent location returns a list of empty neighbors (may include behind) • A fish with 4 empty adjacent locations returns all 4 including behind.

  26. Analysis set 10 answers for discussion

  27. These are open ended questions to discuss • The last scenario corresponds to the design chose by the original programmer. • There are many ways to change implementation and parameter lists of various methods if they shift from one class to another.

  28. 3. Justify your reasoning • With the right justification, many designs sound feasible! • Look at the cohesion • Do they make sense? • Do they hold together conceptually? • Are lots of values being passed around? Or is encapsulation good enough that you don’t have to pass lots of data to many different objects?

More Related