Exam 11: Simulation:computer Simulation Using Objects
Suppose you are working with the bear and fish simulation presented in your text, which uses a two-dimensional grid. Describe the process by which the tryToBreed method chooses a random location next to the fish.
According to the rules, this method must first pick a random adjacent location. To do this, we can use an offset list technique. In this case, instead of iterating through all of the offsets, we will simply pick one. In order to choose a random element of the list, we will first choose a random integer in the range of index values using the length of the offsetList as the upper bound. Once we compute this new (x, y) position, we must be sure that it is in the actual range of legal coordinates. If it is not, we must try again.
Case Study 2:
1. def liveALittle(self):
2. self.__breedTick = self.__breedTick + 1
3. ??????
4. self.tryToBreed()
5.
6. self.tryToEat()
7.
8. if self.__starveTick == 10:
9. self.__world.delThing(self)
10. else:
11. ??????
-Refer to the session in the accompanying Case Study 2, which shows the liveALittle method for the Bear class. What belongs on line 3?
A
Provide a high-level overview of the bears and fish simulation by outlining the algorithm of the main function.
1. Set initial constants pertaining to the number of bears and fish, the size of the world, and the length of the simulation.
2. Create an instance of the World.
3. Create the specified number of bears and fish and place them at random locations in the world.
4. Let the world live for the specified number of time units.
Suppose you are working with the bear and fish simulation presented in your text, which uses a two-dimensional grid. Explain how to determine the number of fish that are next to a certain fish.
Match each definition with its term.
-The list of things that an object should know.
Case Study 2:
1. def liveALittle(self):
2. self.__breedTick = self.__breedTick + 1
3. ??????
4. self.tryToBreed()
5.
6. self.tryToEat()
7.
8. if self.__starveTick == 10:
9. self.__world.delThing(self)
10. else:
11. ??????
-Refer to the session in the accompanying Case Study 2, which shows the liveALittle method for the Bear class. What belongs on line 11?
Case Study 1:
1. class Fish:
2. def __init__(self):
3. self.__turtle = turtle.Turtle ()
4. self.__turtle.up()
5. ??????
6. self.__turtle.shape(""Fish. g i f"")
7.
8. self.__xPos = 0
9. self.__yPos = 0
10. self.__world = None
11.
12. self.__breedTick = 0
-Refer to the session in the accompanying Case Study 1. What belongs on line 5?
What built-in function asks if an object is an instance of a particular class?
Case Study 3:
1. def mainSimulation():
2. numberOfBears = 10
3. numberOfFish = 10
4. worldLifeTime = 2500
5. worldWidth = 50
6. worldHeight = 25
7.
8. myWorld = World(worldWidth, worldHeight)
9. myWorld.draw()
10.
11. for i in range(numberOfFish):
12. newFish = Fish()
13. x = random.randrange(myWorld.getMaxX())
14. y = random.randrange(myWorld.getMaxY())
15. while not myworld.emptyLocation(x, y):
16. x = random.randrange(myWorld.getMaxX())
17. y = random.randrange(myWorld.getMaxY())
18. myWorld.addThing(newFish, x, y)
19.
20. for i in range(numberOfBears):
21. newBear = Bear()
22. x = random.randrange(myWorld.getMaxX())
23. y = random.randrange(myWorld.getMaxY())
24. while not myWorld.emptyLocation(x, y):
25. x = random.randrange(myWorld.getMaxX())
26. y = random.randrange(myWorld.getMaxY())
27. myWorld.addThing(newBear, x, y)
28.
29. for i in range(worldLifeTime):
30. myWorld.liveALittle()
31.
32. myWorld.freezeWorld()
-Refer to the session in the accompanying Case Study 3. On what lines does most of the simulation work take place?
When transforming a problem into program code, ____ become objects.
Consider the bears and fish simulation presented in your text. Explain how the Bear class is similar to and different from the Fish class.
When transforming a problem into program code, what should you use to define the instance variables of an object?
In the bears and fish simulation presented in your text, if a fish has two or more adjacent fish, it will:
Removing a life-form from a simulation world requires only that it be taken off the grid.
Explain how to design the Python classes that will be used in a program.
Match each definition with its term.
-The list of things than an object should be able to do.
Case Study 1:
1. class Fish:
2. def __init__(self):
3. self.__turtle = turtle.Turtle ()
4. self.__turtle.up()
5. ??????
6. self.__turtle.shape(""Fish. g i f"")
7.
8. self.__xPos = 0
9. self.__yPos = 0
10. self.__world = None
11.
12. self.__breedTick = 0
-Refer to the session in the accompanying Case Study 1. What does this code represent?
The mainSimulation function creates all the life-forms that will exist during the simulation.
In the simulation of bears and fish presented in your text, all life-forms are "alive" at the same time.
Describe the move method for fish for the simulation presented in your text.
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)