Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Planet class(C++, include a .h file, cpp file, and driver file. The cla

ID: 3664030 • Letter: C

Question

Create a Planet class(C++, include a .h file, cpp file, and driver file. The class should have functions to set and get the values of the following member variables: name (a string), distance (a float) - representing the planet's distance from the sun, and gravity (a float) - representing the planet's gravity in relation to the Earth's gravity. (You can find actual values at http://nssdc.gsfc.nasa.gov/planetary/factsheet/planet_table_ratio.html. The driver file should declare an array of 7 Planet objects. A function in the driver should let the user input values for the different planets. Then, the driver should have a function to ask the user for a weight in pounds and the name of a planet. Another function should look for the planet in the array of Planets and return the weight the Earth weight would be on the other planet.

Explanation / Answer

//planet.h

using namespace std;

class Planet

{

//member variables

/*Planet name*/

string name;

/*representing the planet's distance from the sun*/

float distance;

/*representing the planet's gravity in relation to the Earth's gravity. */

float gravity;

public:

//default constructor

Planet();

// member functions

void setName(string nam);

void setDistance(int d);

void setGravity(float g);

string getName();

int getDistance();

int getGravity();

};

/*------------------------------------------*/

//Planet.cpp

#include "Planet.h"

using namespace std;

Planet::Planet()

{}

// member functions

void Planet::setName(string nam)

{

name=nam;

}

void Planet::setDistance(int d)

{

distance=d;

}

void Planet::setGravity(float g)

{

gravity=g;

}

string Planet::getName()

{

return name;

}

int Planet::getDistance()

{

return distance;

}

int Planet::getGravity()

{

return gravity;

}

/*------------------------------------------*/

//driver.cpp

#include <iostream>

#include "Planet.cpp"

void inputData(Planet planets[7]);

void readData(string &planet,float &weight);

float calculateWeight(Planet planets[7],string planet,float weight);

int getPlanet(string planet);

int main()

{

Planet planets[7];

string planet;

float weight;

inputData(planets);

readData(planet,weight);

float wt=calculateWeight(planets,planet,weight);

cout<<endl<<"Weight in "<<planet<<" "<<wt<<" kg";

return 0;

}

void inputData(Planet planets[7])

{

string nam;

int d;

float g;

for(int i=0;i<7;i++)

{

cout<<endl<<"Enter Data for Planet "<<(i+1);

cout<<endl<<"Name: ";

cin>>nam;

planets[i].setName(nam);

cout<<endl<<"Distance: ";

cin>>d;

planets[i].setDistance(d);

cout<<endl<<"Gravity: ";

cin>>g;

planets[i].setGravity(g);

}

}

void readData(string &planet,float &weight)

{

cout<<endl<<"Enter your weight in earth: ";

cin>>weight;

cout<<endl<<"Enter the planet name you want <caps>: ";

cin>>planet;

}

int getPlanet(string planet)

{

string planetNames[]={"MERCURY","VENUS","EARTH","MOON","MARS","JUPITER","SATURN","URANUS","NEPTUNE","PLUTO"};

int n=-1;

for(int i=0;i<7;i++)

{

if(planet.compare(planetNames[i])==0)

{

n=i;

break;

}

}

if(n==-1)

n=0; /* set to default , incase user gave wrong name*/

return n;

}

float calculateWeight(Planet planets[7],string planet,float weight)

{

float wt;

int p=getPlanet(planet);

wt= weight *planets[p].getGravity();

return wt;

}