# A11-Keeping Lists

## Objectives

• Code vectors to store data.
• Implement common vector algorithms.
• Explore code reuse with classes
• Write member functions for classes that read data from the console.
• Write member functions for classes that write data to the console.
• Write non-member functions to manipulate vectors.

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

• You are encouraged to work with one other student of this class following the rules of Pair Programming for Homework Assignments. If you choose to pair program, there is a bonus applied.
• You may not give a copy of your code to your designated pair-programming partner if you did not develop the code together.
• You may not look at another person's code until you complete and submit this assignment, except for code you develop code together with your pair-programming partner.
• You may get help from people other than your pair-programming partner if you get stuck, but only if they do not show or tell you the code to type.

## Preparation

1. Make sure you have completed the exercises from lesson 10 and lesson 11.
2. Complete the Review Exercises in CodeLab 10 and 11. These exercises will help prepare you for the problem-solving programs and should be completed first.

## Project Specifications

Your solutions to these projects must use only techniques we have covered so far.

#### Programming Style

For all programs, remember to follow all the style rules we have covered, as well as the newer rules, including:

1. Class naming conventions (See: Class Names)
2. Indentation in classes and placement of curly braces
3. Every function declaration (prototype) in the class, including constructor functions, have a function comment block
4. Every file has a file comment block
5. No magic numbers (See: No Magic Numbers)

#### Project 1: Vector Function Worksheet

Use this worksheet to improve your understanding of vectors and functions. Refer to lesson 10 for more information on vectors. See lesson 1.3.5 on how to compile with C++11.

##### Project Specifications
2. You must name the source code file `vectorwork.cpp` and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

3. Add your name and the date to the file comment block at the top of the file.
4. Have no user input for this project. Testing code for the program is hard-coded in `main()`. Do NOT use any keyboard input streams, like `cin`, in this project. If you have a `cin` statement in your code, you will get a low score.
5. Instructions for each function are presented in the comment block before the function declaration (prototype). As you write each of functions, uncomment the test code in main to verify your work. Do NOT change any of the function signatures or any code in `main()` beyond removing comments. Also, make sure you are compiling correctly:
```g++ -Wall -Wextra -Wpedantic -std=c++11 -o vectorwork vectorwork.cpp
```
6. Submit this project with the rest of the assignment as described in Deliverables.

#### Project 2: Star Catalog

When developing this program, use your `Star` class from Assignment 9 to develop a program to catalog the stars near Earth. Use a vector of `Star` objects to hold information about the nearby stars. Visualize the vector as a series of entries in a catalog. The entries are identified by a number as shown below:

`stars = `
 `string name` `int planets` `double distance`
`[0]`
 `string name` `int planets` `double distance`
`[1]`
 `string name` `int planets` `double distance`
`[2]`

The vector holds each `Star` object, one object to each slot (element) as shown. We can add or delete stars in the catalog by adding or removing `Star` objects to and from the vector. Each object listed above holds three pieces of data. To access the data inside an object we use member functions like `read()`, `print()`, `getName()`, `setName()` and so on.

##### Project Specifications
1. Using your `Star` class from Assignment 9, develop an application that catalogs the nearby stars.

Keep the `Star` class name. Do NOT add any new member variables to the class (including vectors) and do not remove or modify any of the member functions specified in the last assignment, unless you had an error that you are correcting. If you had problems with the `Star` class, contact the instructor for help in getting started. Make sure to `#include <climits>` if you use `INT_MAX` and `#include <cstdlib>` if you use `exit()` to ensure the program compiles on all computers.

2. You must name the source code file `stellar.cpp` and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

3. Add a `read()` member function (and no other function) to your `Star` class. The function has no parameters and returns nothing. The `read()` function reads data from `cin` and loads it into the member variables of the `Star` object in the following order:
1. `name`
2. `planets`
3. `distance`

The `read()` function must operate like the following:

```Enter the name of the star: Barnard's Star
Enter the number of planets for Barnard's Star: 1
Enter the distance in miles to Earth: 3.51e13
```

In the above example, the user entered the values shown in italics (for emphasis). The user must be able to enter spaces in the star name as shown.

4. Use a vector to store your `Star` objects and add at least three of the stars from the previous assignment to the vector.

Thus, your stellar catalog will have at least three stars included automatically when your program starts.

5. Develop a menu to manage the stellar catalog like the following:
```Please choose one of the following operations:
0. Exit program
1. List stars
2. Add a new star
3. Delete a star
4. Change number of planets
Choice (0-4): _
```

The user enters the number of the operation on the line labeled "Choice" where the underbar "`_`" is shown. The underbar "_" is shown for information only and is not part of the program display. Entering a `0` exits the program. Do NOT change the number of a menu operation.

Hint: Call a function to perform each menu option.

6. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. Data entry is shown in italics for information only and is not part of the program display. For the input shown you must get the same output. However, the output must change properly if the inputs are different.

1. List the stars in the stellar catalog, which includes the information from the `print()` function with a heading as shown below.
```Choice (0-4): 1

Stellar Neighbors:
Distance from Earth In
# Star               # Planets      Miles  Light Years
1 Sun                        8   9.31e+07     1.58e-05
2 Proxima Centauri           1   2.48e+13     4.22e+00
3 Epsilon Eridani            1   6.17e+13     1.05e+01
```
2. Add a star to the stellar catalog by calling the `read()` member function of the `Star` class and inserting the new entry into the requested location.
```Choice (0-4): 2

Adding a new Star:
Enter the name of the star: Barnard's Star
Enter the number of planets for Barnard's Star: 0
Enter the distance in miles to Earth: 3.51e13
Place star at (before) star number (1-4): 3
```

Notice that the user can enter spaces for the star name.

3. Delete a star from the catalog by removing the `Star` object from the vector.
```Choice (0-4): 3

Deleting a Star:
Distance from Earth In
# Star               # Planets      Miles  Light Years
1 Sun                        8   9.31e+07     1.58e-05
2 Proxima Centauri           1   2.48e+13     4.22e+00
3 Barnard's Star             0   3.51e+13     5.97e+00
4 Epsilon Eridani            1   6.17e+13     1.05e+01
Enter the number of the Star: 3
```

Notice that the program inserted the star from the previous step into the system at the requested location.

4. Changing the number of planets for a star.
```Choice (0-4): 4

Change number of planets for a star:
Distance from Earth In
# Star               # Planets      Miles  Light Years
1 Sun                        8   9.31e+07     1.58e-05
2 Proxima Centauri           1   2.48e+13     4.22e+00
3 Epsilon Eridani            1   6.17e+13     1.05e+01
Enter the number of the Star: 1
New number of planets: 9
```

Which updates the number of planets for the star.

7. In addition to the `main()` and the `Star` class member functions, define between three and eight non-member functions and call all the non-member functions at least once.

A non-member function does not have a prototype declared inside the class.

8. Structure your code such that you declare non-member function prototypes before `main()` and non-member function definitions after `main()` for all your functions.
9. Submit this project with the rest of the assignment as described in Deliverables.
Hints:
1. For the menu, use a sentinel controlled loop with conditional statements to select the operations. See Exercise 10.4 step 8.
2. To set the three initial objects in a vector, see the "Partial Listing of `prodvector.cpp`" in Exercise 10.4. Another way is to use the `push_back()` function on an empty vector like:
```vector<Product> list;
Product milk("Milk", 3.95);
list.push_back(milk); ```
3. To update the number of planets:
1. Ask the user for the position of the star in the vector:
`cin >> position;`

Adjust the position entered as needed to match the vector index.

2. Use the `setPlanets()` function to update the number of planets:
```catalog[position].setPlanets(input);
```
4. To see an example of how the program operates, download and run the executable file from the command line: Windows Cygwin stellar.exe

## Extra Credit

The following are worth extra credit points if the main program works well:

1. Complete the assignment using pair programming. (1 point)
2. Write the non-member function specified by the following header and call the function at least twice in the program. (2 points)
```/**
Displays a specified prompt message and then reads a valid
int from cin between min and max.

@param prompt a string with the prompt text.
@param min the minimum acceptable value.
@param max the maximum acceptable value
@return a valid int number entered by the user.
*/
int readIntInRange(string prompt, int min, int max);
```
The function must recover from the wrong type of input, like the user entering "three" instead of 2, and ensure the integer number is within the specified range like:
```Enter the number of the Star: three
Error: enter integer numbers only!
Enter the number of the Star: 300
Error: enter integer numbers between 1 and 3.
Enter the number of the Star: 3
```

This function is intended for use with non-member functions only. Do NOT call this function from a member function like `read()`.

Hint: See lessons 5.3.5 to 5.3.7.

Describe any extra credit attempted in your `README.txt` file.

## Tutorial Lab

To prepare for the next lesson, complete the following exercises.

1. Type the `babynames.cpp` program from the textbook on pages 354-357 (332 -333) into a text editor, and then compile and run the program. Submit the source code file (`babynames.cpp`) to Canvas for grading. Use the babynames.txt file to test your program.
2. Complete the Tutorial Exercises in CodeLab 10 and 11 before the specified due date. Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first attempt by clicking the "Solution" tab.

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

#### Lesson Exercises

• 2: All lesson exercises attempted and turned in
• 1: Some lesson exercises completed and turned in
• 0: No lesson exercises completed or turned in

#### Vector Function Worksheet Functionality

• 4: All functions completed and program generates the correct output without error
• 3: All functions completed but has a minor error
• 2: Most functions completed or has some small errors
• 1: Few functions completed or has many errors
• 0: Does not compile or wrong file turned in

#### Stellar Catalog Functionality

• 10: Demonstrates mastery of the assignment
• Applies concepts from the lessons appropriately
• Meets all specifications (see above) with particularly elegant solutions
• Runs to completion with no abnormal error conditions
• Generates correct output given correct input
• Behaves in a reasonable way in response to incorrect data
• 8: Has all the functionality expected of the assignment
• Demonstrates many techniques from the lesson
• Attempts to meet all specifications (see above)
• Implementation seems more complicated than necessary.
• May have one minor error
• 6: Has most of the functionality expected of the assignment
• Demonstrates some techniques from the lesson
• Attempts to meet all but one of the specifications (see above)
• Implementation seems excessively complicated.
• May have 2-3 minor errors
• 4: Has some of the functionality expected of the assignment
• Demonstrates some techniques from the lesson
• Attempts to meet at least 1/2 of the specifications (see above)
• Implementation seems excessively complicated.
• May have more than 3 minor errors
• 2: Serious functional problems but shows some effort and understanding
• Attempts to meet less than 1/2 of the of the specifications (see above)
• Has a major error or many minor errors
• Implementation seems very convoluted
• Demonstrates few techniques from the lesson
• 1: Does not compile or wrong file turned in
• 0: Does not execute, not turned in or uses techniques not covered in course

#### Program Style

• 4: Code is well-documented including:
• 3: Code has a minor documentation error
• 2: Code has some documentation errors
• 1: Code has many documentation errors
• 0: No apparent attempt to follow documentation standards or write documentation comments

#### CodeLab and Other Tutorial Exercises

• Number CodeLab completed correctly / number exercises * 8 and rounded up to the nearest integer.
• -1 if the tutorial exercise file does not compile
• -2 if the tutorial exercise file is not turned in

#### `README.txt` File

• 2: `README.txt` file submitted following the instructions
• 1: `README.txt` file submitted but some information was missing
• 0: No `README.txt` file submitted

Total possible: 30, plus extra credit

## Deliverables

Submit your assignment to Canvas, in the assignment folder that matches the name of this assignment, following the instructions for submitting homework. Include the following items for grading:

1. `README.txt` file
2. All the exercise files from lesson 10 and lesson 11
3. `vectorwork.cpp`
4. `stellar.cpp`
5. `babynames.cpp` from the Tutorial Lab Exercises

You must submit all the files needed to complete your assignment. Do not assume that the instructors has any files. Your assignment must work as submitted.

Last Updated: May 08 2017 @14:25:01