Exercise - Define a Base Class 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. Throughout the exercises, you'll create classes to represent different types of spaceships that can be used in the game. Create a Spaceship class with three variable properties: name, health, and position. The default value of name should be an empty string and health should be 0. position will be represented by an Int where negative numbers place the ship further to the left and positive numbers place the ship further to the right. The default value of position should be 0. Create a let constant called falcon and assign it to an instance of Spaceship. After initialization, set name to "Falcon". Go back and add a method called moveLeft() to the definition of Spaceship. This method should adjust the position of the spaceship to the left by one. Add a similar method called moveRight() that moves the spaceship to the right. Once these methods exist, use them to move falcon to the left twice and i the right once. Print the new position of falcon after each change in position. The last thing Spaceship needs for this example is a method to handle what happens if the ship gets hit. Go back and add a method wasHit() to Spaceship that will decrement the ship's health by 5, then if health is less than or equal to 0 will print "Sorry, your ship was hit one too many times. Do you want to play again?" Once this method exists, call it on falcon and print out the value of health.

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 exercise step by step using Swift code? (Defining a Base  Class)

 

Exercise -
- Define a Base Class
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. Throughout the exercises, you'll create classes to represent different types of spaceships that can be used in the game.
Create a Spaceship class with three variable properties: name, health, and position. The default value of name should be an empty string and health should be 0.
position will be represented by an Int where negative numbers place the ship further to the left and positive numbers place the ship further to the right. The default value of
position should be 0.
8
9.
Create a let constant called falcon and assign it to an instance of Spaceship. After initialization, set name to "Falcon".
11
12
Go back and add a method called moveLeft() to the definition of Spaceship. This method should adjust the position of the spaceship to the left by one. Add a similar method
called moveRight() that moves the spaceship to the right. Once these methods exist, use them to move falcon to the left twice and to the right once. Print the new position of
falcon after each change in position.
14
15
The last thing Spaceship needs for this example is a method to handle what happens if the ship gets hit. Go back and add a method wasHit() to Spaceship that will
decrement the ship's health by 5, then if health is less than or equal to 0 will print "Sorry, your ship was hit one too many times. Do you want to play again?" Once this method
exists, call it on falcon and print out the value of health.
Transcribed Image Text:Exercise - - Define a Base Class 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. Throughout the exercises, you'll create classes to represent different types of spaceships that can be used in the game. Create a Spaceship class with three variable properties: name, health, and position. The default value of name should be an empty string and health should be 0. position will be represented by an Int where negative numbers place the ship further to the left and positive numbers place the ship further to the right. The default value of position should be 0. 8 9. Create a let constant called falcon and assign it to an instance of Spaceship. After initialization, set name to "Falcon". 11 12 Go back and add a method called moveLeft() to the definition of Spaceship. This method should adjust the position of the spaceship to the left by one. Add a similar method called moveRight() that moves the spaceship to the right. Once these methods exist, use them to move falcon to the left twice and to the right once. Print the new position of falcon after each change in position. 14 15 The last thing Spaceship needs for this example is a method to handle what happens if the ship gets hit. Go back and add a method wasHit() to Spaceship that will decrement the ship's health by 5, then if health is less than or equal to 0 will print "Sorry, your ship was hit one too many times. Do you want to play again?" Once this method exists, call it on falcon and print out the value of health.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
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