How do I apply the instructions below and get my code to run using Swift code?
I have been posting the same questions but they always come with the same answer that does not use the starting code given below. Please make sure to use the following code as a starting point.
Exercise - Override Methods and Properties
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. 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.
class Spaceship {
var name: String = ""
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 {
var weapon = ""
var remainingFirePower = 5
func fire() {
if remainingFirePower > 0 {
remainingFirePower -= 1
} else {
print("You have no more fire power.")
}
}
}
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.
Step by stepSolved in 2 steps
- Add a toString method to your Account class. For the purposes of this assignment, please use the toString display the following: This account contains $x. You have earned $y in the last month. where x is the account balance (please don't format the decimals) and y is the monthly interest earned, obtained from the getMonthlyInterest method. Compile and test in a driver by creating and printing an Account object. Add an equals method to your Account class. Two Account objects are equal if their balance and annualInterestRates are equal. Compile and test in your driver by creating 2 Account objects to see if they are equal.arrow_forwardFor c++. Need help designing class, no namespsace. FoodWastageRecord NOTE: You need to design this class. It represents each food wastage entry recorded by the user through the form on the webpage (frontend). If you notice the form on the webpage, you’ll see that each FoodWastageRecord will have the following as the data members aka member variables. Date (as string) Meal (as string) Food name (as string) Quantity in ounces (as double) Wastage reason (as string) Disposal mechanism (as string) Cost (as double) Each member variable comes with its accessor/mutator functions. --------------------------------------------------------------------------------- FoodWastageReport NOTE: You need to design this class. It represents the report generated on the basis of the records entered by the user. This class will be constructed with all the records entered by the user as a parameter. It will then apply the logic to go over all the records and compute the following: Names of most commonly…arrow_forwardExercise 5 - Number to Word Make a new class in the Lab2 project called Num2wordthat prompts the user enter a number between 0 and 9 and then displays the corresponding word (i.e. "zero"for 0, "one"for 1, etc.). Use a switchstatement to do this. Include a default case that lets the user know they entered a wrong number. The switch statement has the following syntax: switch (variable) { case value1: // Code that should execute when variable==value1 break; // this prevents it from automatically going to the next case case value2: // Code that should execute when variable==value2 break; // this prevents it from automatically going to the next case default: // Code that should execute when variable has a value that didn't match the above }arrow_forward
- Create a method that randomly select a letter in the alphabetarrow_forward*with psuedo code First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that does the following: This program is similar to the previous assignment - "WeightedAvgDropSmallest" - with a major difference that the inputs for the program will be read from a file and the output for this assignment should be written to a new file. The concept of application is as follows: The program will read numbers from a file - data.txt - that you will create, where the weight is a double (greater than 0.0 and less than or equal to 1.0) and the other numbers are the number, n, of lowest values to drop and then the numbers to be averaged after dropping the lowest n values. Example data.txt file would display: 0.5 3 10 70 90 80 20 The program then calculates the weighted average of all those numbers except the lowest n numbers, where…arrow_forwardPlease help using Javaarrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY