Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help solving this in Swift Playground ! Exercise- Override Methods and Properties Note The exercises below are based on a right while obstacles

I need help solving this in Swift Playground !

image text in transcribedimage text in transcribed

Exercise- Override Methods and Properties Note The exercises below are based on a right while obstacles "fall" from top to bottom. Throughout the exercises, you'll create classes to represent different types of spaceships that can be used in the game. The base class Spaceship and one subclass Fighter have been provided for you below. 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 class Spaceship f var name: String var health 100 var position 0 func moveLeft) position1 func moveRight)f position += 1 func wasHit health-5 class Fighter: Spaceship t var weapon ". var remainingFirePower 5 func fire) if remainingFirePower> e f remainingFirePower1 else print("You have no more fire power." Define a new class ShieldedShip that inherits from Fighter. Add a variable pr defender. Set name to "Defender" and weapon to "Cannon." Call moveRight() and print position, then call fire) and print remainingFirePower operty shieldStrength that defaults to 25. Create a new instance of ShieldedShip called Define a new class ShieldedShip that inherits from Fighter. Add a variable property shieldStrength that defaults to 25. Create a new instance of ShieldedShip called defender. Set name to "Defender" and weapon to "Cannon." Call moveRight() and print position, then call fire) and print remainingFirePower. Go back to your declaration of ShieldedShip and override wasHit). In the body of the method, check to see if shieldStrength is greater than O. If it is, decrement shieldStrength by 5. Otherwise, decrement health by 5. Call wasHit() on defender and print shieldStrength and health. When shieldStrength is 0, all wasHit() does is decrement health by 5. That's exactly what the implementation of wasHit) on Spaceship does! Instead of rewriting that, you can call through to the superclass implementation of wasHit(). Go back to your implementation of wasHit() on ShieldedShip and remove the code where you decrement health by 5 and replace it with a call to the superclass' implementation of the method. Call wasHit) on defender, then print shieldstrength and health. Exercise- Override Methods and Properties Note The exercises below are based on a right while obstacles "fall" from top to bottom. Throughout the exercises, you'll create classes to represent different types of spaceships that can be used in the game. The base class Spaceship and one subclass Fighter have been provided for you below. 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 class Spaceship f var name: String var health 100 var position 0 func moveLeft) position1 func moveRight)f position += 1 func wasHit health-5 class Fighter: Spaceship t var weapon ". var remainingFirePower 5 func fire) if remainingFirePower> e f remainingFirePower1 else print("You have no more fire power." Define a new class ShieldedShip that inherits from Fighter. Add a variable pr defender. Set name to "Defender" and weapon to "Cannon." Call moveRight() and print position, then call fire) and print remainingFirePower operty shieldStrength that defaults to 25. Create a new instance of ShieldedShip called Define a new class ShieldedShip that inherits from Fighter. Add a variable property shieldStrength that defaults to 25. Create a new instance of ShieldedShip called defender. Set name to "Defender" and weapon to "Cannon." Call moveRight() and print position, then call fire) and print remainingFirePower. Go back to your declaration of ShieldedShip and override wasHit). In the body of the method, check to see if shieldStrength is greater than O. If it is, decrement shieldStrength by 5. Otherwise, decrement health by 5. Call wasHit() on defender and print shieldStrength and health. When shieldStrength is 0, all wasHit() does is decrement health by 5. That's exactly what the implementation of wasHit) on Spaceship does! Instead of rewriting that, you can call through to the superclass implementation of wasHit(). Go back to your implementation of wasHit() on ShieldedShip and remove the code where you decrement health by 5 and replace it with a call to the superclass' implementation of the method. Call wasHit) on defender, then print shieldstrength and health

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

More Books

Students also viewed these Databases questions