You will create the ShoutBox class for your Virtual World. Your ShoutBox class will have two methods: Method shoutOutCannedMessage() will return type String. The shoutOutCannedMessage will use an Array or an ArrayList to store 10 messages of type String. For those of you who are more advanced with your Java skills, you could use a HashMap for the data structure. You can load this data structure with 10 messages of your choosing. You can initialize your Array or ArrayList with the messages or have the user enter the messages. The choice is yours. shoutOutCannedMessage() will loop through the data structure to first display all canned messages and allow the user to select one. The shoutOutCannedMessage() will return the selected message String. The shoutOutRandomMessage() method will return type String. The shoutOutRandomMessage() will use several Arrays or an ArrayList to store words. You will have one data structure that holds a list of words that are subjects, another data structure that holds a list of words that are objects, another that holds a list of verbs, another that holds a list of adverbs, and another that holds a list of adjectives. You can initialize your data structures with words or have the user enter the words. The choice is yours. The shoutOutRandomMessage() method will use a random number generator that selects one word from each data structure to form a random message. The shoutOutRandomMessage() method will return the random message as a String data type. Random messages will be of the form: Subject – Verb – Adjective – Object – Adverb. Document your shoutOutRandomMessage() method to explain the code.
Expert Answer
/*The source code of the above said program is given below:*/
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
public class ShoutBox {
public static String shoutOutCannedMessage()
{
/*declaring arraylist of type String*/
List<String> myArray = new ArrayList<String>();
/*storing 10 messages into arraylist*/
myArray.add(“Whatever you are, be a good one!”);
myArray.add(“May you live every day of your life!”);
myArray.add(“Try Again and Again!”);
myArray.add(“We become what we think about!”);
myArray.add(“Never give up!”);
myArray.add(“Success is the best revenge!”);
myArray.add(“Always dream big!”);
myArray.add(“Nothing is impossible!”);
myArray.add(“Always keep hope!”);
myArray.add(“All that glitters is not gold!”);
/*displaying all canned messages*/
System.out.println(“The canned messages are: “);
for (int i = 0; i < myArray.size(); i++)
{
String str = myArray.get(i);
System.out.println(i+1 + “. : ” + str);
}
/*taking the message number as input from user */
System.out.println(“nSelect the index number of message that you want to be returned: “);
Scanner in = new Scanner(System.in);
int num = in.nextInt();
in.close();
/*returning the message accordingly*/
if(num<=10 && num>=1)
{
return myArray.get(num-1);
}
else
{
System.out.println(“Message index number is invalid!!”);
return null;
}
}
public static String shoutOutRandomMessage()
{
/*declaring and initializing arrays of subjects, objects,verbs,adjectives and adverbs;
* all of them are array of Strings*/
String[] subjects = new String[]{“Alice”,”Bob”,”Tom”,”They”,”We”,”You”,”He”,”She”};
String[] objects = new String[]{“tea”,”coffee”,”answers”,”books”,”milk”,”story-books”,”puzzels”,”milk-shakes”};
String[] verbs = new String[]{“drinks”,”drink”,”knows”,”love”,”loves”,”know”,”reads”,”read”};
String[] adjectives = new String[]{“hot”,”cold”,”correct”,”incorrect”,”good”,”bad”,”interesting”,”boaring”};
String[] adverbs = new String[]{“daily”,”everyday”,”already”,”always”,”quickly”,”now”,”today”,”completely”};
/*generating different random number for each array */
/*minimum range for random number for all the arrays is same i.e.0*/
int min=0;
/*generating random number for subjects array*/
int max=subjects.length-1;
Random rn = new Random();
int range = max – min + 1;
int randomNum = rn.nextInt(range) + min;
/*choosing the random element from subjects array and putting it in string result1*/
String result1 = subjects[randomNum];
/*generating random number for verbs array*/
max=verbs.length-1;
rn = new Random();
range = max – min + 1;
randomNum = rn.nextInt(range) + min;
/*choosing the random element from verbs array and concating it with string result1 and storing in String result2*/
String result2 = result1 + ” “+verbs[randomNum];
/*generating random number for adjectives array*/
max=adjectives.length-1;
rn = new Random();
range = max – min + 1;
randomNum = rn.nextInt(range) + min;
/*choosing the random element from adjectives array and concating it with string result2 and storing in String result3*/
String result3 = result2+ ” “+ adjectives[randomNum];
/*generating random number for objects array*/
max=objects.length-1;
rn = new Random();
range = max – min + 1;
randomNum = rn.nextInt(range) + min;
/*choosing the random element from objects array and concating it with string result3 and storing in String result4*/
String result4 = result3+ ” ” +objects[randomNum];
/*generating random number for adverbs array*/
max=adverbs.length-1;
rn = new Random();
range = max – min + 1;
randomNum = rn.nextInt(range) + min;
/*choosing the random element from adverbs array and concating it with string result4 and storing in String result5*/
String result5 = result4 + ” ” +adverbs[randomNum];
//returning the final string
return result5;
}
public static void main(String[] args)
{
String Cannedmsg = shoutOutCannedMessage();
System.out.println(“Message that is returned from shoutOutCannedMessage() method : “+ Cannedmsg);
String Randommsg = shoutOutRandomMessage();
System.out.println(“Message that is returned from shoutOutRandomMessage() method : “+ Randommsg);
}
}
/*For better understanding output screenshot is attaced here*/