Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Read the following Swift code and then answer the questions that follow. Write y

ID: 3909432 • Letter: R

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 {

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()

Explanation / Answer

If you have any doubts, please give me comment....

1)

rufus.run() --> it decrements happiness property with 5 value i.e., happiness = 5

rufus.eat() --> it incrementa 3 by three properties i.e.,energy =13, happiness = 8, again increments happiness = 11

rufus.run() --> it decrements happiness property with 5 value i.e., happiness = 6

print(rufus.isContent) --> true

print(rufus.energy) --> 13

output:

true

13

2)

rufus has class of Dog, but Dog class doesn't have playWithOwner method, it will generates error...

rufus.daydream() is private method, it can't access outside of class, it will be access only inherited class and self class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote