Deck 11: Simulation:computer Simulation Using Objects
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/34
Play
Full screen (f)
Deck 11: Simulation:computer Simulation Using Objects
1
Computer simulations use ____ as a way of introducing some realistic variability into the underlying model.
A) relationships
B) random numbers
C) algorithms
D) classes
A) relationships
B) random numbers
C) algorithms
D) classes
B
2
In the bears and fish simulation presented in your text, if a fish has two or more adjacent fish, it will:
A) die.
B) breed.
C) turn into a bear.
D) eat.
A) die.
B) breed.
C) turn into a bear.
D) eat.
A
3
When transforming a problem into program code, ____ become objects.
A) verbs
B) classes
C) adjectives
D) nouns
A) verbs
B) classes
C) adjectives
D) nouns
D
4
When transforming a problem into program code, what should you use to define the instance variables of an object?
A) The things the object should know
B) The things the object should do
C) The things an object is related to
D) The name of the object
A) The things the object should know
B) The things the object should do
C) The things an object is related to
D) The name of the object
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
5
Assume a list g exists. When using row major order representing a grid, how do you access an item at location (x, y) in g?
A) g[y][x]
B) g[x][y]
C) g[x+y]
D) g[x*y]
A) g[y][x]
B) g[x][y]
C) g[x+y]
D) g[x*y]
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
6
To create a grid using row major order, first create a row by repeatedly appending ____ to an empty list.
A) []
B) None
C) row
D) 0
A) []
B) None
C) row
D) 0
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
7
The freezeWorld method of a simulation calls the ____ method of wscreen to allow the graphics window to remain drawn after the simulation has ended.
A) quit
B) stop
C) exitonclick
D) exit
A) quit
B) stop
C) exitonclick
D) exit
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
8
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?
A) The constructor for a simulation
B) The constructor for World
C) The constructor for Fish
D) The definition for Fish
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?
A) The constructor for a simulation
B) The constructor for World
C) The constructor for Fish
D) The definition for Fish
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
9
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?
A) self.turtle.pos = (0,0)
B) hideturtle ()
C) turtle.hide ()
D) self.turtle.hideturtle ()
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?
A) self.turtle.pos = (0,0)
B) hideturtle ()
C) turtle.hide ()
D) self.turtle.hideturtle ()
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
10
Consider the bears and fish simulation presented in your text. Imagine that a fish is at location (x, y) and squares are drawn all around it in two dimensions. What are the coordinates for the location that is immediately above the fish in the previous row?
A) (x - 1, y + 1)
B) (x + 0, y + 1)
C) (x - 1, y + 0)
D) (x + 1, y + 0)
A) (x - 1, y + 1)
B) (x + 0, y + 1)
C) (x - 1, y + 0)
D) (x + 1, y + 0)
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
11
What built-in function asks if an object is an instance of a particular class?
A) is
B) instanceof
C) isinstance
D) getclass
A) is
B) instanceof
C) isinstance
D) getclass
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
12
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) if self.__breedTick >= 8:
B) if self.__breedTick &It; 8:
C) if self == "Bear":
D) if self.__currentPosition > 1:
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) if self.__breedTick >= 8:
B) if self.__breedTick &It; 8:
C) if self == "Bear":
D) if self.__currentPosition > 1:
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
13
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?
A) world.delete()
B) fish.tryToEscape()
C) self.tryToMove()
D) world.tryToMove()
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?
A) world.delete()
B) fish.tryToEscape()
C) self.tryToMove()
D) world.tryToMove()
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
14
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?
A) 2-9
B) 11-18
C) 20-27
D) 29-30
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?
A) 2-9
B) 11-18
C) 20-27
D) 29-30
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
15
As you consider the implementation for the Fish, Bear, and Plant classes, you have probably noticed that there are a number of duplications. The technique of ____ would help to solve this issue.
A) information hiding
B) simulation
C) generation
D) inheritance
A) information hiding
B) simulation
C) generation
D) inheritance
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
16
The complexity of a computer simulation is dependent on the complexity of the underlying reality as well as the specific characteristics we are interested in representing.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
17
In the simulation of bears and fish presented in your text, all life-forms are "alive" at the same time.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
18
When creating objects, the list of things that an object can do will become methods.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
19
Removing a life-form from a simulation world requires only that it be taken off the grid.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
20
The mainSimulation function creates all the life-forms that will exist during the simulation.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
21
Match each definition with its term.
-A programmatic model of some specific aspects of a real situation or system.
A) Computer simulation
B) Instance variables
C) Class
D) Methods
-A programmatic model of some specific aspects of a real situation or system.
A) Computer simulation
B) Instance variables
C) Class
D) Methods
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
22
Match each definition with its term.
-The list of things that an object should know.
A) Computer simulation
B) Instance variables
C) Class
D) Methods
-The list of things that an object should know.
A) Computer simulation
B) Instance variables
C) Class
D) Methods
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
23
Match each definition with its term.
-Identified by nouns and used to create objects that correspond to parts of a problem.
A) Computer simulation
B) Instance variables
C) Class
D) Methods
-Identified by nouns and used to create objects that correspond to parts of a problem.
A) Computer simulation
B) Instance variables
C) Class
D) Methods
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
24
Match each definition with its term.
-The list of things than an object should be able to do.
A) Computer simulation
B) Instance variables
C) Class
D) Methods
-The list of things than an object should be able to do.
A) Computer simulation
B) Instance variables
C) Class
D) Methods
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
25
What is a computer simulation?
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
26
Explain how to design the Python classes that will be used in a program.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
27
Explain how to identify the instance variables and methods that should belong to an object.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
28
The solution for the bears and fish simulation in your text creates a two-dimensional grid. Explain how to implement this type of grid in Python.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
29
Describe the move method for fish for the simulation presented in your text.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
30
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
31
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
32
Consider the bears and fish simulation presented in your text. Explain how the Bear class is similar to and different from the Fish class.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
33
Provide a high-level overview of the bears and fish simulation by outlining the algorithm of the main function.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
34
Explain how you might add a Plant class to the bears and fish simulation presented in your text.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck