280 likes | 359 Views
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.”)
E N D
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.”) • For(i=0;i<nbrs1.size();i++) • System.out.print (nbrs1.get(i) + “ “);
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
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
Notice how changes happen • Look closely at change Direction and change Location methods. (page 39)
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.
myAge • Would have to initialize in the initialize method. • Probably increment in act • Need an accessor method • Modify toString to add the new info.
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?
changeColor method Public void changeColor(Color newColor) { myColor = newColor; }
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))
Possible variation on act • Use a boolean oddOReven fish instance variable If(oddOReven) {changeColor(Color.red); oddOreven = true; } Else {changeColor(Color.yellow); oddOReven = false;}
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.
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
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
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
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
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
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
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.
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.
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?