CSE116的学习笔记-Lec#7:Model of Execution More Example
Model of Execution
More Memory Examples
- Multiple Obejects on the heap
1
2
3
4def winBattle(character: PartyCharacter, xp: Int): Unit = {
character.battlesWon += 1
character.experiencePoints += xp
}
1 | def main(args: Array[String]): Unit = { |
1 | class PartyCharacter() { |
1 | class Party(val characterOne: PartyCharacter, val characterTwo: PartyCharacter) { |
- Multiple frames on the stack
1
2
3
4
5
6
7
8
9
10
11
12
13
14def computeGeometricSum(n: Int): Int = {
if (n > 0) {
var result: Int = computeGeometricSum(n - 1)
result += n
result
} else {
0
}
}
def main(args: Array[String]): Unit = {
val result: Int = computeGeometricSum(3)
println(result)
}
See the process detail in PDF
Old Example
- Multiple Objects on the heap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19def main (args: Array[String]): Unit = {
val bird: Bird = new Bird()
var action: String = "Nothing"
if(bird.inDanger()){
val action: String = "Panic!"
} else {
val action: String = "Check bird"
}
println(action)
val box: Box = new Box(bird, new Bird())
if(box.inDanger()){
action = "Stay in the boat"
}
println(action)
}1
2
3
4
5
6
7
8
9class Bird {
val timesHelpful: Int = 0
var timesChecked: Int = 0
def inDanger(): Boolean = {
timesChecked += 1
true
}
}1
2
3
4
5class Box (val bird1: Bird, val bird2: Bird) {
def inDanger(): Boolean = {
bird1.inDanger() && bird2.inDanger()
}
}
See the process detail in PDF
Lecture Question
Question: In a package named “execution” create a Scala class named “Team” and a Scala object named “Referee”.
Team will have:
- Two state values of type Int representing the strength of the team’s offense and defense with a constructor to set these values. The parameters for the constructor should be offense first, then defense (Note: These values do not have defined names in this question so you cannot access them in your testing. If I use “teamOffense” and you name it “offense” and access it in your tests, your tests will crash when testing my code since the variable “offense” will not exist)
- A third state variable of type Int that is not in the constructor that represents the score of the team, is declared as a var, and is initialized to 0 (This variable also has not defined name)
Referee will have:
- A method named “playGame” that takes two Team objects as parameters and return type Unit. This method will alter the state of each input Team by setting their scores equal to their offense minus the other Team’s defense. If a Team’s offense is less than the other Team’s defense their score should be 0 (no negative scores)
- A method named “declareWinner” that takes two Teams as parameters and returns the Team with the higher score. If both Teams have the same score, return a new Team object to indicate that neither competing team won (You may choose any values in the constructor call of this new Team)
Testing: In a package named “tests” create a Scala class named “TestTeams” as a test suite that tests the functionality listed above
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment
TwikooValine