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

Each fortune cookie will have an array of 10 strings that will hold different fo

ID: 3743258 • Letter: E

Question

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

import java.util.Random;

public class FortuneCookie {

   private int rand_index;
   private String[] fortunes={"1","2","3","4","5","6","7","8","9","10"};
  
   FortuneCookie()
   {
       rand_index = new Random().nextInt(10);
   }
  
   void openFortuneCookie()
   {
       System.out.println("|=============|");
       System.out.println(this.fortunes[this.rand_index]);
       System.out.println("|=============|");
   }
   void generateNewFortune()
   {
       rand_index = new Random().nextInt(10);
   }
   public static void main(String args[])
   {
      
       FortuneCookie fc = new FortuneCookie();
       fc.openFortuneCookie();
       fc.generateNewFortune();
       fc.openFortuneCookie();
      
   }
}

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