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 0. If it is, decrement shieldStrength by 5. Otherwise, decrement health by 5. Call wasHit() on defender and print shieldStrength and health.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

How do I solve the following practise exercise using Swift code? (Overriding Methods and Properties)

Note: The exercise below is 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. 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 in the picture.

INSTRUCTIONS:

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 0. 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.

class Spaceship {
var name: String
II I
%3D
var health = 100
var position = 0
func moveLeft() {
position -= 1
}
func moveRight() {
position += 1
}
func wasHit() {
health -= 5
if health <= 0 {
print ("Sorry, your ship was hit one too many times. Do you want to play again?")
}
}
}
class Fighter: Spaceship {
II II
var weapon =
var remainingFirePower = 5
func fire() {
if remainingFirePower > 0 {
remainingFirePower
} else {
print("You have no more fire power.")
-= 1
}
}
}
Transcribed Image Text:class Spaceship { var name: String II I %3D var health = 100 var position = 0 func moveLeft() { position -= 1 } func moveRight() { position += 1 } func wasHit() { health -= 5 if health <= 0 { print ("Sorry, your ship was hit one too many times. Do you want to play again?") } } } class Fighter: Spaceship { II II var weapon = var remainingFirePower = 5 func fire() { if remainingFirePower > 0 { remainingFirePower } else { print("You have no more fire power.") -= 1 } } }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 5 images

Blurred answer
Knowledge Booster
Math class and its different methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education