Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP282 – Advanced Object-Oriented C Languages

Project Overview

You will create a small interactive program to input data about countries, manipulate the data, and display the data. The project consists of 3 parts: one on creating the country files (both .cpp and .h), one on creating the method menu() in the MenuPart2 class, which should be implemented in a menupart2.cpp file you should create and one on creating the method menu() in the MenuPart3, which should be implemented in a menupart3.cpp file you should create. It is likely easier to do the tasks in order (and besides that, as mentioned, some of the tests for some of the tasks assume this has been done). Each task builds on the previous ones. Do as much as you can and then package your project for submission. Begin by downloading the Visual Studio project template for this assessment.

Read through this entire document before you start coding, so you’re aware of all parts and the overall structure of the  program. Your solution should demonstrate your knowledge of C++. Important: Each task requires you to add extra code. You shouldn’t need to remove any code. Keep the code from previous tasks in your program so we can see it and mark it. In particular, menu() in a menupart2.cpp should work correctly even when you are done with menu() in a menupart3.cpp!

Part 1 (Worth 15% in total)

Task 1 – Country Class Definition – 5%

Create a Country class (incl. both a .h file and a .cpp file) that stores a name and a population size. The name should be stored as a string, and the population size as a long. Declare and define a public constructor Country(std::string name, long pop) that

stores the parameters in the object. Also declare and define a default constructor that sets the name to an empty string and the population size to zero.

Create also two public methods: getName() and getPop(), that should return name and population size respectively.

The  public  tests  for  this  are  in  the  template,  in  mainmethod.cpp  as  methods Task1Test1(), Task1Test2(), Task1Test3().

The tests are simply testing if the objects are constructed correctly (or at least the get methods output the correct thing after construction)

Task1Test1() has the following implementation:

Country c("UK", 67081234);

return c.getName() == "UK" && c.getPop() == 67081234;

Task1Test2() has the following implementation:

Country c("Germany", 83222442);

return c.getName() == "Germany" && c.getPop() == 83222442;

Task1Test3() has the following implementation:

Country c;

return c.getName() == "" && c.getPop() == 0;

Task 2 – Country I/O - 5%

Implement the << and >> operators so you can output and input a Country object with the following string format.

In other words, the name of the country is output followed by the population. When the user types a similar string as input, the first token (up to the space) should be stored as the name, and the second (after the space) as the population. For this task you do not need to do any input validation or error handling. Assume the user will always type the correct format.  In particular, the name of the country (for the purpose of this assignment) is always 1 word.

The  public  tests  for  this  are  in  the  template,  in  mainmethod.cpp  as  methods Task2Test1(), Task2Test2(), Task2Test3() and Task2Test4().

Note, the tests uses stringstream. Think of it as cout in the first two (and the tests are then testing if the output in cout is correct when outputting the countries) and cin in the last two (and the tests are then testing if the countries are constructed correctly when the user” inputs them)

Task2Test1() has the following implementation:

Task2Test2() has the following implementation:

Task2Test3() has the following implementation:

Task2Test4() has the following implementation:

Task 3 – Comparison Operators – 5%

Implement comparison operators (<, >, and ==) for the Country class. These should work numerically, based on the populations of the countries involved.

The public test methods for this task, Task3Test1 and Task3Test2 are somewhat convoluted, because they are running many tests.

Specifically, Task3Test1 runs all the 4*4*3=48 tests (i.e. each pair of countries and each of the three operators) for the countries:

And Task3Test2 does the same for the countries

Part 2 (Worth 45% in total)

menu() in the class MenuPart2, as defined in menupart2.h and which you should implement in menupart2.cpp (later on, part 3 will require similar setup but with 3 instead of 2) is meant to be the main function of a simple command-line app (ala the calculator in the second lab session) based on a loop that displays a menu and takes user choices as text input.

The menu should have these options at the end of Part 2:

The user is then asked to enter an option as follows:

And the corresponding option is then picked. If Q is entered, the method should return. Otherwise, the tasks in this part will explain what you need to implement for each option. Whenever an option finishes execution, loop back to the menu.

Task 4 Adding & Listing Countries (Fixed Storage) – 20%

In this task, you should implement menu option 1. Add country and option 2. List countries. We need both to be able to test anything really…

Whenever option 1 is chosen by the user, they should be asked to input a country (unless there are currently 9 countries, see below), like the following:

After the user enters a country, it should then be stored in some suitable way.

If there are currently 9 countries when option 1 is chosen an error message should be displayed instead of more countries being added. The error message should be:

Whenever option 2 is picked by the user, the current list (i.e. the ones entered, but not yet removed see task 6) should be outputted in the order inputted. E.g. if the countries in the current list are UK 67081234 and Germany 83222442 (entered in that order), then output

NOTE: The indices start at 1!

The public tests will be shown over the next few pages – there was not quite enough space on this one

 

The public test will test the following runs (there are more on the next page):


 

Example 2 from above after being shorten

 

will be shortened to [MENU] in the remaining run. The output from your code is still supposed to be full menu as output in the example above. Boldface will otherwise not be used in the run. Hence, the second run would be as above.

Run in left column continues in right

Task 5 – Finding the Most Populous Country - 10%

This task is about implementing option 3. Find largest country. You should find the country with the largest population size and then display

Largest: [LARGEST COUNTRY]                      

Where [LARGEST COUNTRY] is the output to cout of the largest country. If there are

no countries in the list, the error message

There are no countries in the index currently    

should be shown instead. The algorithm for this task should be straightforward using e.g. the already defined < operator on countries. You may assume for the purpose of this task that the sizes of the populations of the different countries are distinct (i.e. this option will only be run in the tests when the different countries each have a distinct population size).

There is just one public test, shown below. Like the last public test in Task 4, the menu will be displayed as [MENU], but you are still expected to output the real menu!

Task 6 – Removing Countries - 15%

We will here implement 4. Remove country. After the option has been picked, the user is asked for an index:

If there is no country with that index (i.e. the index is too high or too low) the error message

should be displayed and then we should go back to the menu. On the other hand, if there is such an index, the country with that index should be removed from the list and the countries with higher index should have their index decremented (to remove gaps). Note, this means that you can e.g. keep on removing the same index each time it will apply to the one that had 1 higher index before. Also, if you had 9 and removed 1, then you can add a country again (i.e. without getting the warning from having too many countries)

The public tests for this task is as follows (like in the last run in Task 4, [MENU] is used to improve readability it should still be the original menu!):

The second public test run is a continuation of the last one from task 4, replacing the last line: “Enter Option Q” with the following:

Part 3 (Worth 40% in total)

In this part, you are meant to implement menu() for the class MenuPart3 as defined in menupart3.h. You are meant to do the implementation in menupart3.cpp, which you should create. It should look a lot like menu() for MenuPart2, but there is one more menu option:

Task 7 – Refactored Program (Dynamic Data) – 25%

Refactor the program so it uses a dynamic data structure. In other words, there is no limit  on  the  number  of  countries  the  program  could  store  (assuming  unlimited memory).  The  actual  behaviour  of  the  program  won’t  change  from  the  user’s perspective, but internally it will be very different.

You should use a dynamic data structure from the Standard Template Library, such as a vector or a list (you should NOT use set or map they sort automatically, and things will then be out of order i.e. not ordered by the order they were entered - unless you do things in an ugly way). You should also use iterators and algorithms. E.g. containers have an erase() function to remove items (which, as you saw in the

lectures, happens to do pretty much exactly what Remove country should do…). Make full use of functions and algorithms from the STL where appropriate 

A simple rule of thumb is that you should be able to replace your container type by most any other (not-sorted container) by only changing the line declaring the data structure (assuming both files are included). E.g. if you are using vector or deque can you change to list? If you are using list or forward_list can you change to vector or deque? The only place where this is non-trivial is for 4. Remove country if you were doing vectors/deques, since you will likely need to use advance – since advance does not return the iterator, it costs a few lines of code Note, this is before doing Task 8, since sorting for a list has a different syntax from sorting for a container with random access iterators (like vector, array and deque) as shown in lecture 6.

In general, you have seen algorithms or techniques that can handle the options you are being asked to implement in a few lines of code and, while there is no limit on how much code you may use, see if you can get it down to a few lines per option.

This task will be partly checked by hand (because testing it seems very tricky) and partly checked by having a test inserting a high number of countries and checking if things seem to be working correctly.

There are no real public tests directly for this. If I specified a number that would be good  enough,  you  could just  do  an  array solution  e.g.  using  a  bit  larger  array. CodeGrade is running all the public tests from part 2 on part 3 (changed to the new menu though) as well though, so you have an idea if you made a mistake as part of the refactoring.

You will get 5% of the mark for being able to still do the test from part 2 in part 3.

Task 8 – Sorting the Data - 15%

In this task you should implement 5. Sort countries. Specifically, you should sort them so that the one with the smallest population size has index 1, the second smallest has index 2 and so on, counting from 1 (like option 2. List countries). Note, the option is only meant to sort and not display. If display is wanted as well, one can choose that option afterwards.

You may assume that the countries have distinct populations for the purpose of this task.

You should use the full range of functions and algorithms provided by the Standard Template Library, and implement any necessary helper functions, as explained in the relevant lecture material.

In  the  public  test  runs,  like  previously,  we  omit  the  menu  and  instead  write [MENU+SORT]. The change in name is due to the fact that the menu here in part 3 also contains option 5. Sort countries. I.e. [MENU+SORT] should be:

Public test runs for Task 8: