var shieldStrength: Int0 override fune 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 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 constant named 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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

How do I solve this classes exercises using Swift code? Classes 4/4

 

class ShieldedShip: Fighter {
var shieldStrength: Int = 0
override func wasHit() {
if shieldStrength > 0 {
shieldStrength
} else {
-= 5
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 constant named 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.
Transcribed Image Text:class ShieldedShip: Fighter { var shieldStrength: Int = 0 override func wasHit() { if shieldStrength > 0 { shieldStrength } else { -= 5 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 constant named 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.
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 = 0
var position: Int = 0
func moveleft() {
position -= 1
}
func moveRight() {
position += 1
}
func wasHit(O {
health -= 5
if health <= 0 {
print ("Sorry, your ship was hit one too many times. Do you want to play again?")
}
}
}
class Fighter: Spaceship {
let weapon: String =
var remainingFirePower: Int = 0
func fire() {
if remainingFirePower > 0 {
remainingFirePower -= 1
} else {
print ("You have no more fire power.")
}
}
class ShieldedShip: Fighter {
var shieldStrength: Int = 0
override func wasHit() {
if shieldStrength > 0 {
shieldStrength -= 5
} else {
super.wasHit()
}
}
Transcribed Image Text: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 = 0 var position: Int = 0 func moveleft() { position -= 1 } func moveRight() { position += 1 } func wasHit(O { health -= 5 if health <= 0 { print ("Sorry, your ship was hit one too many times. Do you want to play again?") } } } class Fighter: Spaceship { let weapon: String = var remainingFirePower: Int = 0 func fire() { if remainingFirePower > 0 { remainingFirePower -= 1 } else { print ("You have no more fire power.") } } class ShieldedShip: Fighter { var shieldStrength: Int = 0 override func wasHit() { if shieldStrength > 0 { shieldStrength -= 5 } else { super.wasHit() } }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY