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

Part 3 - Modify the following Program. Convert the following program as per comm

ID: 3702473 • Letter: P

Question

Part 3 - Modify the following Program. Convert the following program as per comments

// program colors

// -> Convert class into .h header file and .ccp implemenation file and a main file.

// Keep int main() funciton same...

#include <iostream>

using namespace std;

#include <string>

/* srand example */

#include <ctime>

#include <stdio.h>      /* NULL */

#include <stdlib.h>     /* srand, rand */

class colorPicker {

private:

string colorArray[7];

public:

colorPicker() {

// Defalut Consructor assign values to array

// Use 7 assignment statements to assign each color to the color array

colorArray[0] ="Red";

colorArray[1] ="Green";

colorArray[2] ="Purple";

colorArray[3] ="Yellow";

colorArray[4] ="Orange";

colorArray[5] ="Indigo";

colorArray[6] ="Pink";

}

void printAllColors() {

// use for loop to print out all colors

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

{ cout << colorArray[i] << endl;}

}

string randomColor() {

srand((unsigned)time(0));

// pick a random number between 1 and 7

int i =0;

i = rand()%7;

return colorArray[i];

}

};

int main()

{

colorPicker P;

//

P.printAllColors();

cout << "Random Color: " << P.randomColor();

system("pause");

return 0;

}

Explanation / Answer

Here is the code you wanted. Splitted the program into three files – color.h, color.cpp and main.cpp, where color.h contains the header file definition, color.cpp contain implementations and main.cpp has the main method to test the program. Drop a comment if you have doubts. Thanks.

//color.h

#ifndef color_h //defining header file

#define color_h

#include <iostream>

using namespace std;

#include <string>

#include <ctime>

#include <stdlib.h>     /* srand, rand */

class colorPicker {

private:

string colorArray[7];

public:

                //defining all the method prototypes

                colorPicker();

                void printAllColors();

                string randomColor();

};

#endif //end of header file definition

//color.cpp

#include "color.h"

//implementation of all methods in header file

colorPicker::colorPicker() {

                // Defalut Consructor assign values to array

                // Use 7 assignment statements to assign each color to the color array

                colorArray[0] ="Red";

                colorArray[1] ="Green";

                colorArray[2] ="Purple";

                colorArray[3] ="Yellow";

                colorArray[4] ="Orange";

                colorArray[5] ="Indigo";

                colorArray[6] ="Pink";

}

void colorPicker::printAllColors() {

                // use for loop to print out all colors

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

                { cout << colorArray[i] << endl;}

}

string colorPicker::randomColor() {

                srand((unsigned)time(0));

                // pick a random number between 1 and 7

                int i =0;

                i = rand()%7;

                return colorArray[i];

}

//main.cpp

#include "color.h"

int main(){

                //demonstrating the colorpicker class methods

                colorPicker P;

                P.printAllColors();

                cout << "Random Color: " << P.randomColor()<<endl;

                system("pause");

                return 0;

}

//output

Red

Green

Purple

Yellow

Orange

Indigo

Pink

Random Color: Red

Press any key to continue . . .