In: Computer Science
Code in C++
Objectives
Create Class: Planet
Planet will be a simple class consisting of three fields:
Your Planet class should have the following methods:
The constructor should throw an error if any of the following are true:
Here’s something important to remember: no methods should use standard input or output. They should neither pause to read in input or display any output. While you can include iostream for debugging purposes, your final file that you submit should not include the header.
Create Wrapper Class: Planets
Write a wrapper class for a listing of planets. This wrapper class will maintain a list of all planets and will return which planet has the highest number of Aliens. A wrapper class is a class that acts as decoration for another class (in this case, class vector from the STL). There’s only one field to this class:
Your Planets should have the following methods:
Create the Driver Application
Create a driver application that demonstrates that each of the components of your program work. Create an empty planets structure with which to work. Create an interactive menu that gives the user the following choices and then performs that choice:
Tips for Everyone
Functions should be short: no longer than 20 lines. If something is longer than 20 lines, turn that into multiple functions, where each function does something slighly different. You will probably have more than 5 functions in this assignment.
Assignment 1 test Scenario
name |
madeOf |
alienPolulation |
P1 |
Iron |
20000 |
P2 |
Silver |
54000 |
P3 |
Gold |
30000 |
Output must look like: the most populated Planet is: P2 with 54000 aliens that is made of Silver
Output must look like: Number of Planet(s): 3
Output Must look like: The population of planet P3 is: 30000
Note that the underscored values must be read from the objects.
The Application is divided into three files - Planet.cpp, Planets.cpp and main.cpp (Driver Application). The code for all the files is given below:
Make sure that you create and save all three files in the same directory. For compilation, use the following commands:
g++ main.cpp -o main
And run it as:
./main
Planet.cpp
#include <string>
using namespace std;
class Planet {
private:
string name;
string madeOf;
int alienPopulation;
public:
Planet() {}
Planet(string name, string madeOf, int alienPopulation) {
if(name.empty())
throw "Planet Name Empty";
if(madeOf.empty())
throw "Made of Empty";
if(alienPopulation < 0)
throw "Alien Population Empty";
this->name = name;
this->madeOf = madeOf;
this->alienPopulation = alienPopulation;
}
string getName() {
return name;
}
string getMadeOf() {
return madeOf;
}
int getAlienPopulation() {
return alienPopulation;
}
};
Planets.cpp
#include <vector>
#include <climits>
#include "Planet.cpp"
using namespace std;
class Planets {
private:
vector<Planet> planets;
public:
void addPlanet(Planet p) {
planets.push_back(p);
}
int getCount() {
return planets.size();
}
Planet getMostPopulatedPlanet() {
int mostPop = INT_MIN;
int mostPopPlanet = -1;
for(int i = 0; i < planets.size(); i++) {
if(planets[i].getAlienPopulation() > mostPop) {
mostPop = planets[i].getAlienPopulation();
mostPopPlanet = i;
}
}
return planets[mostPopPlanet];
}
Planet get(int i) {
if(i < 0 || i > getCount() - 1)
throw "ArrayIndexOutOfBound";
return planets[i];
}
};
main.cpp
#include <iostream>
#include "Planets.cpp"
using namespace std;
Planet newPlanet( ) {
cout << "Enter the Name, MadeOf, and Alien Population of the
planet :\n";
string name, madeOf;
int population;
cin >> name >> madeOf >> population;
Planet p(name,madeOf,population);
return p;
}
int main() {
int choice;
bool run = true;
Planet temp;
Planets p;
while(run) {
cout << "*******Planets Menu*********\n";
cout << "****************************\n";
cout << "1. Add Planet\n";
cout << "2. Get Planet Count\n";
cout << "3. Get the Most Populated Planet\n";
cout << "4. Exit\n";
cout << "Enter your choice:\t";
cin >> choice;
switch(choice) {
case 1:
p.addPlanet(newPlanet());
break;
case 2:
cout << "Number of Planet(s): " << p.getCount()
<< endl;
break;
case 3:
temp = p.getMostPopulatedPlanet();
cout << "the most populated planet is: " <<
temp.getName() << " with " << temp.getAlienPopulation()
<< " that is made of " << temp.getMadeOf() <<
endl;
break;
case 4:
run = false;
break;
}
}
return 0;
}