You will create the ShoutBox class for your Virtual World. Your ShoutBox class w
ID: 3581961 • Letter: Y
Question
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.
Explanation / Answer
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
public class ShoutBox {
private static Map<Integer, String> getCannedMessagesMap() {
Map<Integer, String> cannedMessages = new LinkedHashMap<Integer, String>();
cannedMessages.put(1, "So many books, so little time.");
cannedMessages.put(2,
"Be the change that you wish to see in the world.");
cannedMessages.put(3, "I love java the program.");
cannedMessages.put(4,
"You only live once, but if you do it right, once is enough.");
cannedMessages.put(5, "A little knowledge is the dangerous thing.");
cannedMessages.put(6, "Software is eating the world.");
cannedMessages.put(7,
"I am not the person whom you are looking for.Get off");
cannedMessages.put(8,
"Don't cry because it's over, smile because it happened.");
cannedMessages.put(9, "You won't die of hard work!");
cannedMessages.put(10, "I love java the drink.");
return cannedMessages;
}
public static String shoutOutCannedMessage() {
int id;
String message = "";
Iterator<Entry<Integer, String>> msgIterator = getCannedMessagesMap()
.entrySet().iterator();
while (msgIterator.hasNext()) {
Map.Entry msgPair = (Map.Entry) msgIterator.next();
System.out.println("[" + msgPair.getKey() + "] "
+ msgPair.getValue());
}// End while
try {
System.out.print("Please select your one favourite message : ");
Scanner sc = new Scanner(System.in);
id = sc.nextInt();
System.out.println(" ");
System.out.println("You select this message:");
message = getCannedMessagesMap().get(id);
} catch (Exception e) {
System.out.println("Error with " + e.getMessage());
}
return message;
}
public static String shoutOutRandomMessage() {
int g;
// Below holds the words to be generated.
String[] subject = { " You", " Someone", " A relative" };
String[] verb = { " is", " was", " will be", " might be" };
String[] adjective = { " rich", " successful", " surprised",
" educated" };
String[] object = { " with a new Java course", " with future kids",
" with new pets", " with a new house" };
String[] adverb = { " soon. ", " in due time. ", " someday. ",
" in some years. " };
Random k = new Random(); // this initializes a Random
int chosenPiece = k.nextInt(subject.length);
String randomMessage = "";
for (g = 1; g <= 1; g++)
{
randomMessage = subject[k.nextInt(subject.length)]
+ verb[k.nextInt(verb.length)]
+ adjective[k.nextInt(adjective.length)]
+ object[k.nextInt(object.length)]
+ adverb[k.nextInt(adverb.length)];
}
return randomMessage;
}
public static void main(String[] args) {
System.out.println("shoutOutCannedMessage()-->"
+ shoutOutCannedMessage());
System.out.println("shoutOutRandomMessage()-->"
+ shoutOutRandomMessage());
}
}
OUTPUT:
[1] So many books, so little time.
[2] Be the change that you wish to see in the world.
[3] I love java the program.
[4] You only live once, but if you do it right, once is enough.
[5] A little knowledge is the dangerous thing.
[6] Software is eating the world.
[7] I am not the person whom you are looking for.Get off
[8] Don't cry because it's over, smile because it happened.
[9] You won't die of hard work!
[10] I love java the drink.
Please select your one favourite message : 5
You select this message:
shoutOutCannedMessage()-->A little knowledge is the dangerous thing.
shoutOutRandomMessage()--> Someone might be educated with future kids in some years.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.