Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with C++ Objective: In this assignment, you will fill in methods for a Spaceship class. Then, you will also complete a program to

Need help with C++

Objective:

In this assignment, you will fill in methods for a Spaceship class. Then, you will also complete a program to test your new class. We have provided the skeleton code for the class and the test program (available on the class website).

Description:

You bought a spaceship at Walmart. Now you need to name it, give it a loyal crew, prepare it for battle, and make it zoom around the universe. Using the Spaceship.h header file given on the class website, implement the methods for the Spaceship class without changing anything in Spaceship.h. The description for each method is as follows:

Default constructor:

Doesnt take any arguments. Sets each instance variable to a default value.

Non-default constructor:

Takes in the name, crew size, and impulse (cruising) speed of the spaceship, and also whether or not its shields are currently up. Sets each parameter to its corresponding class variable. FYI, Star Trek Voyagers impulse speed was 74,400 km/sec

Copy constructor:

Takes in another Spaceship object as a parameter. Sets each variable of the class to equal the values in the parameter Spaceship object.

Destructor:

The destructor wont have anything in it, since there is no dynamic memory associated with the Spaceship class.

Setter functions:

Sets the corresponding instance variable to the parameter value passed in. Checks that valid values are passed in (see details in Spaceship.h)

Getter functions:

Return the corresponding instance variable value.

Print:

Prints information about the spaceship. This is the only printing that should be done inside of any method of Spaceship. All other printing should happen in the tester program.

BattleStart:

Sets the shields up to true.

BattleEnd:

Sets the shields up to false.

CurrentSpeed:

Returns the current speed of the spaceship. If the shields are up, the current speed should be the impulse (cruising) speed. Otherwise, the speed should be warp speed.

You should implement each method for the Spaceship class one at a time. As soon as you implement a method, call that method in the main program to test it with a variety of parameters. Begin with the default constructor and Print. You should write a loop around a switch statement so that you can easily call your new method.

Testing program code:

#include  #include "Spaceship.h" using namespace std; void PrintMenu() { cout << endl; cout << "OPTIONS:" << endl; cout << "0 - Quit" << endl; cout << "1 - Print my Spaceship's info" << endl; cout << "2 - Give my Spaceship a new name" << endl; cout << "3 - Give my Spaceship a new crew size" << endl; cout << "4 - Give my Spaceship a new impulse speed" << endl; cout << "5 - Set shields up" << endl; cout << "6 - Set shields down" << endl; cout << "7 - Prepare for battle" << endl; cout << "8 - End the battle" << endl; cout << "9 - Get travel time (in days)" << endl; cout << endl; } //--------------------------------------------- // Name: GetChoice // Purpose: Collect a number between 0 and 9 from the user // Do error checking to reject invalid values // Parameters: None // Returns: int, a valid value entered by the user //--------------------------------------------- int GetChoice() { char choice; // read the input into a character for safety int intChoice; do { cout << "Choose what to do next: "; cin >> choice; } while (choice < '0' || choice > '9'); // convert the character to an integer and return it intChoice = choice - '0'; return (intChoice); } //--------------------------------------------- // Name: travelDays // Purpose: Return how many days it will take to travel a given distance // formula: time = distance/speed // where speed is the spaceship's current speed // you need to convert the time from seconds to days // and divide the speed by 1000000 to get it in million km per sec // Assume that a valid value is passed in (do no error checking) // Parameters: ship - a Spaceship, const // distance - the distance to travel in million km, const // Returns: double, the number of days to complete the trip //--------------------------------------------- double travelDays (const Spaceship ship, const long distance) { const long secPerDay = 60 * 60 * 24; // the number of seconds in a day return 0.0; } int main() { string newName; int newCrewSize; long newSpeed; long distance; int choice; // Use the default constructor to create defaultSpaceship Spaceship defaultSpaceship; cout << "defaultSpaceship "; defaultSpaceship.Print(); // Use the non-default constructor to create the Excalibur with a crew of 10 // and an impulse speed of 10000 and shields down // Then print it out Spaceship mySpaceShip; // CHANGE THIS TO USE NON_DEFAULT CONSTRUCTOR // use the copy constructor to create another spaceship that is a copy // of the Excalibur (then print it out) do { PrintMenu(); choice = GetChoice(); // TODO: fill out each choice in this switch statement // by calling methods in your mySpaceship object switch(choice) { case 0: break; case 1: case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; case 8: break; case 9: cout << "How far are you going, in million kilometers (0 to 1000000)? "; cin >> distance; while ((distance < 0) || (distance > 1000000)) { cout << "We cannot travel that far. "; cout << "How far are you going, in million kilometers (0 to 1000000)? "; cin >> distance; } cout << "You will be traveling for " << travelDays(mySpaceship, distance) << " days." << endl; break; default: break; } } while (choice != 0); return 0; }

Header code for the Spaceship class (Spaceship.h):

//----------------------------------------------------- // Filename: Spaceship.h // Purpose: The header file for the Spaceship class //----------------------------------------------------- using namespace std; class Spaceship { public: // Constructors Spaceship(); Spaceship(const int crew, const string n, const long impulse, const bool shields); Spaceship(const Spaceship &otherShip); // Destructor ~Spaceship(); // Setters (aka Mutators) bool SetCrewSize(const int c); void SetName(const string n); bool SetImpulseSpeed(const long i); void SetShieldsUp(const bool shields); // Getters (aka Accessors) int GetCrewSize() const; string GetName() const; long GetImpulseSpeed() const; bool GetShieldsUp() const; // Other void Print() const; void BattleStart(); void BattleEnd(); long CurrentSpeed() const; private: const static long WARP_SPEED = 300000; // speed of light in km/sec int crewSize; string name; long impulseSpeed; // cruising speed in km/sec bool shieldsUp; };

Implementation code for the Spaceship class (Spaceship.cpp):

//----------------------------------------------------- // Filename: Spaceship.cpp // Purpose: The implementation of the Spaceship class //----------------------------------------------------- #include  #include "Spaceship.h" using namespace std; //----------- Constructors and Destructors ---------- // Default constructor Spaceship::Spaceship() { crewSize = 0; name = "NoName"; impulseSpeed = 0; shieldsUp = false; } // Non-default constructor // Copy constructor // Destructor (already completed for you) Spaceship::~Spaceship() { } //----------- Setters ------------------------------- // SetCrewSize // SetName // SetImpulseSpeed // SetShieldsUp //----------- Getters ------------------------------- // GetCrewSize // GetName // GetImpulseSpeed // GetShieldsUp //----------- Other --------------------------------- // Print void Spaceship::Print() const { locale mylocale(""); cout.imbue(mylocale); cout << "Name: " << name << endl; cout << "Crew Size: " << crewSize << " crew members." << endl; cout << "Impulse Speed: " << impulseSpeed << " km/sec." << endl; cout << "Shields Up: "; if (shieldsUp) cout << "TRUE" << endl; else cout << "FALSE" << endl; } // BattleStart // BattleEnd // CurrentSpeed 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data 29th British National Conference On Databases Bncod 2013 Oxford Uk July 2013 Proceedings Lncs 7968

Authors: Dan Olteanu ,Georg Gottlob ,Christian Schallhart

2013th Edition

3642394663, 978-3642394669

More Books

Students also viewed these Databases questions