Model of Execution

More Memory Examples

  • Multiple Obejects on the heap
    1
    2
    3
    4
    def winBattle(character: PartyCharacter, xp: Int): Unit = {
    character.battlesWon += 1
    character.experiencePoints += xp
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def main(args: Array[String]): Unit = {
// Add the value mobXP to the stack with a value of 20
val mbXP: Int = 20

// Add the value bossXP to the stack with a value of 100
val bossXP: Int = 100

// A new object of type PartyCharacter is created
// The "hero" value only stores this reference
val hero: PartyCharacter = new PartyCharacter()

// do method "winBattle"
winBattle(hero, mobXP)
val party: Party = new Party(hero, new PartyCharacter())
party.winBattle(bossXP)
winBattle(party.characterOne, mobXP)
winBattle(party.characterTwo, mobXP)
}
1
2
3
4
class PartyCharacter() {
var battlesWon: Int = 0
var experiencePoints: Int = 0
}
1
2
3
4
5
6
7
8
9
10
11
12
class Party(val characterOne: PartyCharacter, val characterTwo: PartyCharacter) {

var battleWon: Int = 0

def winBattle(xp:Int): Unit = {
this.battlesWon += 1
this.CharacterOne.battlesWon += 1
this.CharacterTwo.battlesWon += 1
this.CharacterOne.experiencePoints += xp
this.CharacterTwo.experiencePoints += xp
}
}
  • Multiple frames on the stack
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    def 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
    19
    def 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
    9
    class Bird {
    val timesHelpful: Int = 0
    var timesChecked: Int = 0

    def inDanger(): Boolean = {
    timesChecked += 1
    true
    }
    }
    1
    2
    3
    4
    5
    class 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