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

Modify the fortune teller program from Homework #4. All requirements of Homework

ID: 3806804 • Letter: M

Question

Modify the fortune teller program from Homework #4. All requirements of Homework #4 apply. In addition, make the following changes described below. Create a String array of 5 random fortunes for Red. Create a String array of 5 random fortunes for Green. Create a String array of 5 random fortunes for Blue. When the program chooses a fortune based on the color picked by the user, generate a random number from 0-4. Use this to index into the String array corresponding to the color the user picked. Output the fortune at that index in the array.

Explanation / Answer

package myProject;
import java.util.*;
//Class RandomColor definition
public class RandomColor
{
   //Instance variables
   String [] Red = new String[5];
   String [] Green = new String[5];
   String [] Blue = new String[5];
   //To display colors
   void displayColor()
   {
       System.out.println(" Red String Array");
       for(int x = 1; x < 5; x++)
           System.out.println("Position " + x + " = " + Red[x]);
       System.out.println(" Green String Array");
       for(int x = 1; x < 5; x++)
           System.out.println("Position " + x + " = " + Green[x]);
       System.out.println(" Blue String Array");
       for(int x = 1; x < 5; x++)
           System.out.println("Position " + x + " = " + Blue[x]);      
   }//End of function
  
   //To set the color based on the random number generated
   void selectColor()
   {
       //Creates an object of Random class
       Random randomGenerator = new Random();
       //Creates an object of scanner class
       Scanner sc = new Scanner(System.in);
       int ch;
       //Loops till user choice is 0
       do
       {
           //Displays menu
           System.out.println("Select a color 1 for Red 2 for Green 3 for Blue 0 to Exit : ");
           //Accepts user choice
           ch = sc.nextInt();
           //Generates random number between 1 and 4
           int randomInt = randomGenerator.nextInt(4) + 1;
           System.out.println(randomInt + " ");
           switch(ch)
           {
           case 0:
               break;
           case 1:
               Red[randomInt] = "RED";
               break;
           case 2:
               Green[randomInt] = "GREEN";
               break;
           case 3:
               Blue[randomInt] = "BLUE";
               break;
           default:
               System.out.println("Invalid Choice. Re - enter.");
           }//End of switch
       }while(ch != 0);//End of while
   }//End of method
  
   //Main method
   public static void main(String ss[])
   {
       //Creates an object of RandomColor class
       RandomColor rc = new RandomColor();
       //To generate random number
       rc.selectColor();
       //To display colors
       rc.displayColor();
   }//End of method
}//End of class

Output:

Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
1
3
Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
1
2
Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
2
1
Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
2
2
Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
3
3
Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
3
4
Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
3
2
Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
3
3
Select a color
1 for Red
2 for Green
3 for Blue
0 to Exit :
0
2

Red String Array
Position 1 = null
Position 2 = RED
Position 3 = RED
Position 4 = null

Green String Array
Position 1 = GREEN
Position 2 = GREEN
Position 3 = null
Position 4 = null

Blue String Array
Position 1 = null
Position 2 = BLUE
Position 3 = BLUE
Position 4 = BLUE

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