Question

In: Computer Science

Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...

Code in C++

Objectives

  • Use STL vector to create a wrapper class.

Create Class: Planet

Planet will be a simple class consisting of three fields:

  • name: string representing the name of a planet, such as “Mars”
  • madeOf: string representing the main element of the planet
  • alienPopulation: int representing if the number of aliens living on the planet

Your Planet class should have the following methods:

  • Planet(name, madeOf, alienPopulation) // Constructor
  • getName(): string // Returns a planet’s name
  • getMadeOf(): String // Returns a planet’s main element
  • getAlienPopulation(): int // Returns a planet’s Aliens’ population

The constructor should throw an error if any of the following are true:

  • The name is an empty string.
  • The madeOf is an empty string
  • The alienPopulation is less than zero

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:

  • planets: vector of type Planet

Your Planets should have the following methods:

  • addPlanet(Planet): void // Adds a planet to the listing of planets
  • getCount(): int // Returns the number of planets currently in this data structure
  • getMostPopulatedPlanet(): planet // Returns the planet with the highest number of aliens. If there is a tie among multiple planets, return the planet that was entered first. If the list is empty, throw an error.
  • get(int i): planet // Returns the planet which is at position i.

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:

  • Add Planet. The program will prompt the user for a name, a madeOf, and a alienPopulation. The program will create that object and pass it to the data structure via a call to addPlanet.
  • Get count. Prints to the screen the number of planets in the data structure.
  • Get the most populated planet. Prints to the screen the name, madeOf, and number of aliens of the current most populated planet in the data structure.
  • Quit. Quits the application.

Tips for Everyone

  • In your Driver application, try to make everything a function.
    • Reading in a planet name should be a function.
    • Reading in a planet’s madeOf should be a function.
    • Reading in a planet’s alienPopulation should be a function.
    • Building a planet should be a function.
    • Requesting a new planet and adding to a vector of planets should be a function.

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

  1. Create three planets as following

name

madeOf

alienPolulation

P1

Iron

20000

P2

Silver

54000

P3

Gold

30000

  1. Then call the getMostPopulatedPlanet() method: It must return the “P2” planet. Print the name of that planet by calling the getName() method.

Output must look like: the most populated Planet is: P2 with 54000 aliens that is made of Silver

  1. Call the getCount() method and print the returned value (output)

Output must look like: Number of Planet(s): 3

  1. Call the getAlienPopulation() for planet P3 and print the returned value (output)

Output Must look like: The population of planet P3 is: 30000

Note that the underscored values must be read from the objects.

Solutions

Expert Solution

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;
}


Related Solutions

Create a C++ program using native C++ (STL is acceptable) that produces Huffman code for a...
Create a C++ program using native C++ (STL is acceptable) that produces Huffman code for a string of text entered by the user. Must accept all ASCII characters. Provide an explanation for how you got frequencies of characters, how you sorted them, and how you got codes.
Discuss tradeoffs between using the C++ STL list and vector.
Discuss tradeoffs between using the C++ STL list and vector.
C++ How to make this code take a class object instead of int for the vector...
C++ How to make this code take a class object instead of int for the vector queue and be able to enqueue and dequeue the object? #include <iostream> #include <float.h> #include <bits/stdc++.h> using namespace std; void print_queue(queue<int> Q) //This function is used to print queue { while (!Q.empty()) { cout<< Q.front() << " "; Q.pop(); } } int main() { int n = 10; vector< queue<int> > array_queues(n); //Create vector of queues of size 10, each entry has a queue...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a rectangular matrix A of dimensions 4X2 (4- rows, 2-columns); Create a matrix transpose to the above matrix A. Call that matrix AT; Calculate matrix products: A*AT and AT*A. Present the results. What are the dimensions of those two product matrices; Square matrixes sometimes have an inverse matrix. Try calculating inverse matrices (or matrixes, if you prefer) of above matrices (matrixes) A*AT and AT*A; Extend...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
Language: C++ Conduct an evaluation of the performance of STL containers (vector, list, set, unordered_set) in...
Language: C++ Conduct an evaluation of the performance of STL containers (vector, list, set, unordered_set) in inserting and finding elements, with a relatively large data set. What you'll be measuring is the execution time. Obviously it's dependent on the speed of the computer one uses, so what matters is the relative speed (relative to the vector's speed in percentage) create a vector of 100,000 elements of integers, with the values initially set as 1~100,000, in this order. randomize the ordering...
Code in C++. Using ONE of the following themes to create a class and demonstrate the...
Code in C++. Using ONE of the following themes to create a class and demonstrate the idea. Use this to create a solution (two ADTs and a driver program) to demonstrate the following concepts. Create a one page summary to describe where you are using each of these concepts. Themes:(PICK ONE) a house has a door a semester has a holiday break a cell phone has a shatterproof case a chair has a cushion Concepts to include: composition separating interface...
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
Please use c++ Consider the code on the next page. It creates a vector of five...
Please use c++ Consider the code on the next page. It creates a vector of five strings of vegetable names and you want to make the vector contain only vegetable names that you like. In the spaces designated, perform the following: Declare an iterator for a vector of strings, move it to the position of 'Tomato', and erase it because it's not a vegetable. With the same iterator, use .begin() to point it to the beginning of the list, and...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT