Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise - Class Memberwise Initializers and References note. The exercises below are based on a game where a spaceship avoids obstacles in space. The ship

Exercise - Class Memberwise Initializers and References

note.

The exercises below are based on a game where a spaceship avoids obstacles in space. The ship is positioned at the bottom of a coordinate system and can only move left and right while obstacles "fall" from top to bottom. The base class Spaceship and subclasses Fighter and ShieldedShip have been provided for you below. You will use these to complete the exercises.

class Spaceship {

let name: String

var health: Int

var position: Int

func moveLeft() {

position -= 1

}

func moveRight() {

position += 1

}

func wasHit() {

health -= 5

}

}

class Fighter: Spaceship {

let weapon: String

var remainingFirePower: Int

func fire() {

if remainingFirePower > 0 {

remainingFirePower -= 1

} else {

print("You have no more fire power.")

}

}

}

class ShieldedShip: Fighter {

var shieldStrength: Int

override func wasHit() {

if shieldStrength > 0 {

shieldStrength -= 5

} else {

super.wasHit()

}

}

}

Note that each class above has an error by the class declaration that says "Class has no initializers." Unlike structs, classes do not come with memberwise initializers because the standard memberwise initializers don't always play nicely with inheritance. You can get rid of the error by providing default values for everything, but it is common and better practice to simply write your own initializer. Go to the declaration of Spaceship and add an initializer that takes in an argument for each property on Spaceship and sets the properties accordingly.

Then create an instance of Spaceship below called falcon. Use the memberwise initializer you just created. The ship's name should be "Falcon."

Writing initializers for subclasses can get tricky. Your initializer needs to not only set the properties declared on the subclass, but also set all of the uninitialized properties on classes that it inherits from. Go to the declaration of Fighter and write an initializer that takes an argument for each property on Fighter and for each property on Spaceship. Set the properties accordingly. (Hint: you can call through to a superclass' initializer with super.init after you initialize all of the properties on the subclass).

Then create an instance of Fighter below called destroyer. Use the memberwise initializer you just created. The ship's name should be "Destroyer."

Now go add an initializer to ShieldedShip that takes an argument for each property on ShieldedShip, Fighter, and Spaceship, and sets the properties accordingly. Remember that you can call through to the initializer on Fighter using super.init.

Then create an instance of ShieldedShip below called defender. Use the memberwise initializer you just created. The ship's name should be "Defender."

Create a new instance of Spaceship called sameShip and set it equal to falcon. Print out the position of sameShip and falcon, then call moveLeft() on sameShip and print out the position of sameShip and falcon again. Did both positions change? Why? If both were structs instead of classes, would it be the same? Why or why not? Provide your answer in a comment or print statement below.

Note: Prefered laugage"SWIFT"

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

Question Can I collect benefits if I become disabled?

Answered: 1 week ago

Question

Question May I set up a Keogh plan in addition to an IRA?

Answered: 1 week ago