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

Using C++ Each fortune cookie will have an array of 10 strings that will hold di

ID: 3743272 • Letter: U

Question

Using C++

Each fortune cookie will have an array of 10 strings that will hold different fortunes. You can make up the 10 fortunes. One of those fortunes will be the active one. That active fortune will be selected by generating a random number for an index in the array. The UML class diagram for the Fortune Cookie class looks like the following:

FortuneCookie

-rand_index : int

- fortunes[10] : string

+ openFortuneCookie() : void

+generateNewFortune() : void

+<<constructor>>FortuneCookie() :

The fortune cookie will have the following methods:

void generateNewFortune();

Summary: This function creates a new random index that

represents a different fortune. The fortunes are stored

in a string array called fortunes, of size 10.

void openFortuneCookie();

Summary: This function displays the fortune at rand_index in the array     of strings. Sample output

shown below:

|=========================|

| You will get great news!|

|=========================|

FortuneCookie();

Summary: The default constructor assigns a fortune to each

index in the fortunes array. It also initializes the rand_index

to a random number from 0 to 9.

FortuneCookie

-rand_index : int

- fortunes[10] : string

+ openFortuneCookie() : void

+generateNewFortune() : void

+<<constructor>>FortuneCookie() :

Explanation / Answer

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

class FortuneCookie {

int rand_index;

string fortunes[10] = {

"You get great news",

"A closed mouth gathers no feet.",

"Flattery will go far tonight.",

"This is a cookie",

"Is this a cookie",

"No snowflake in an avalanche ever feels responsible",

"About time I got out of that cookie",

"Someone in your life needs a letter form you",

"You receive one from your loved one",

"Never do anything halfway"

};

public:

FortuneCookie() {

srand(time(0)); //Generates new seed

rand_index = rand() % 10; //Generates a random number between 0 to 9 inclusive

}

void generateNewFortune() {

rand_index = rand() % 10; //Generates a random number between 0 to 9 inclusive

}

void openFortune() {

std::cout<<"|===================================================|"<<std::endl;

std::cout<<"|"<<fortunes[rand_index]<<std::endl;

std::cout<<"|===================================================|"<<std::endl;

}

};

int main() {

FortuneCookie f;

f.openFortune();

std::cout<<std::endl;

f.generateNewFortune();

f.openFortune();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote