Exam 11: Simulation:computer Simulation Using Objects

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

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.

Free
(Essay)
4.7/5
(44)
Correct Answer:
Verified

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?

Free
(Multiple Choice)
4.9/5
(33)
Correct Answer:
Verified

A

Provide a high-level overview of the bears and fish simulation by outlining the algorithm of the main function.

Free
(Essay)
4.9/5
(35)
Correct Answer:
Verified

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.

(Essay)
4.8/5
(40)

Match each definition with its term. -The list of things that an object should know.

(Multiple Choice)
4.9/5
(32)

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?

(Multiple Choice)
4.9/5
(42)

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?

(Multiple Choice)
4.8/5
(41)

What built-in function asks if an object is an instance of a particular class?

(Multiple Choice)
4.8/5
(33)

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?

(Multiple Choice)
4.8/5
(45)

When transforming a problem into program code, ____ become objects.

(Multiple Choice)
4.8/5
(48)

Consider the bears and fish simulation presented in your text. Explain how the Bear class is similar to and different from the Fish class.

(Essay)
4.9/5
(36)

When transforming a problem into program code, what should you use to define the instance variables of an object?

(Multiple Choice)
4.7/5
(37)

In the bears and fish simulation presented in your text, if a fish has two or more adjacent fish, it will:

(Multiple Choice)
4.7/5
(46)

Removing a life-form from a simulation world requires only that it be taken off the grid.

(True/False)
4.9/5
(30)

Explain how to design the Python classes that will be used in a program.

(Essay)
4.9/5
(33)

Match each definition with its term. -The list of things than an object should be able to do.

(Multiple Choice)
4.8/5
(29)

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?

(Multiple Choice)
4.9/5
(36)

The mainSimulation function creates all the life-forms that will exist during the simulation.

(True/False)
4.8/5
(33)

In the simulation of bears and fish presented in your text, all life-forms are "alive" at the same time.

(True/False)
4.8/5
(28)

Describe the move method for fish for the simulation presented in your text.

(Essay)
4.8/5
(43)
Showing 1 - 20 of 34
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)