Question: Using Swift 4.2 Please provide your code typed class Spaceship { let name: String var health: Int var position: Int func display(){ name health position

Using Swift 4.2 Please provide your code typed

class Spaceship {

let name: String

var health: Int

var position: Int

func display(){

name

health

position

}

func moveLeft() {

position -= 1

}

func moveRight() {

position += 1

}

func wasHit() {

health -= 5

}

init(name: String , health: Int ,position: Int ) {

self.name = name

self.health = health

self.position = health

}

}

class Fighter: Spaceship {

var weapon: String = ""

var remainingFirePower: Int = 0

func fire() {

if remainingFirePower > 0 {

remainingFirePower -= 1

} else {

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

}

}

init(weapon: String, remainingFirePower: Int, name: String, health: Int, position: Int) {

self.weapon = weapon

self.remainingFirePower = remainingFirePower

super.init(name: name, health: health, position: position)

}

}

class ShieldedShip: Fighter {

var shieldStrength: Int = 0

override func wasHit() {

if shieldStrength > 0 {

shieldStrength -= 5

} else {

super.wasHit()

}

}

init(shieldStrength: Int, weapon: String, remainingFirePower: Int, name: String, health: Int, position: Int)

{

self.shieldStrength = shieldStrength

super.init(weapon: weapon, remainingFirePower: remainingFirePower, name: name, health: health, position: position)

}

}

Above, add a display() method to SpaceShip that displays the name, health, and position. Add an override function display() to both Fighter and ShieldedShip that displays both their specific information as well as the information of the super class method.

Below write a class named Transporter that inherits from Spaceship. A Transporter can hold up to 2 Fighter type ships inside it. Add 2 Fighter type properties called bay1 and bay2 in Transporter without default values. Add a memberwise initializer as in the previous exercises.

Add an override wasHit() method. When a Transporter wasHit(), then not only does it take damage but all of its contained Fighter Types take damage. In other words, in the Transporter's wasHit() method, call wasHit() on all of contained fighters in addition to the Transporter itself (Remember: you can call through to the superclass implementation of wasHit() to use).

Add an override display method that displays the Transporter attributes, as well as the attributes of each contained Fighter.

Create an 1 instance of Fighter type named falcon and 1 instance of ShieldedShip type named defender using the memberwise initializers. Create a new instance of Transporter called carrier. Set the name to "Carrier" and its attributes to whatever you want, but pass falcon into bay1 and defender into bay2.

Then call display on carrier

Notice the fact that the Transporter class definition only ever refers to Fighter types within its definition but we were able to pass defender (a ShieldedShip) into a Fighter parameter as an acceptable substitute. What do you think will happen when the wasHit() method is invoked on carrier? Will the defender use the Fighter implementation or the ShieldedShip implementation? Include a comment of what you think will happen below (don't worry about whether you are right or wrong.) Then Call wasHit() on carrier and display the results to confirm or refute your ideas. Were you right?

print("********************AFTER THE CARRIER IS HIT****************** ")

Lastly, go back into the SpaceShip class one more time. Add a static property numShips, with a default value of 0. Inside the SpaceShip initializer add the line " numShips += 1". Below these instructions, use this property to retrieve and print the number of space ships that currently exist.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!