Question: Exercise - Class Memberwise Initializers and References I need help, please answer using swift programming on playground The exercises below are based on a game
Exercise - Class Memberwise Initializers and References
I need help, please answer using swift programming on playground



The exercises below are based on a game where as right while obstacles "fall" from top to bottom. The base class Spaceship and subclasses Fighter and ShieldedShip have been provided for you below. You wil use these to complete the exercises. paceship avoids obstacles in space. The ship is positioned at the bottom of a coordinate system and can only move left and class Spaceship Class 'Spaceship' has no initializers let name: String var health: Int var position: Int func moveLeft() position 1 func moveRight) position1 func wasHit) health- 5 class Eighter: Spaceshipf O Class 'Fighter' has no initializers let weapon: String var remainingFirePower: Int func fire) f if remaning FirePower > { remainingFirePower1 ) else print("You have no more fire power.") class ShieldedShip: Fighter t Class 'ShieldedShip, has no initializers var shieldStrength: Int override func wasHit () if shieldStrength > 8f shieldStrength 5 else f class ShieldedShip: Fighter f O Class 'ShieldedShip has no initializers var shieldStrength: Int override func wasHit) if shieldStrength > 8i 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
