Question: Read the following Swift code and then answer the questions that follow. Write your answers in blank space anywhere near the corresponding question. class Dog

Read the following Swift code and then answer the questions that follow. Write your answers in blank space anywhere near the corresponding question.

class Dog {

let name: String

var energy: Int

var happiness: Int

var isContent: Bool { return energy > 5 && happiness > 5 }

init(name: String) {

self.name = name

energy = 10

happiness = 10

}

func eat() {

energy += 3

happiness += 3

daydream()

}

func run() { energy -= 5 }

private func daydream() { happiness += 3 }

}

class GoldenRetriever: Dog {

var loyalty = 10

override var isContent: Bool { return super.isContent && loyalty > 5 }

override func eat() {

super.eat()

loyalty += 1

}

func playWithOwner() { loyalty += 5 }

}

let rufus = Dog(name: "Rufus")

let duke = GoldenRetriever(name: "Duke")

A. What is printed to the console after the following code executes?

rufus.run()

rufus.eat()

rufus.run()

print(rufus.isContent)

print(rufus.energy)

B. Why will each of these two lines produce an error?

rufus.playWithOwner()

rufus.daydream()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!