You will create the ShoutBox class for your Virtual World. Your ShoutBox class w
ID: 3763178 • 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
//ShoutBox.java
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ShoutBox
{
/**The method shoutOutCannedMessage that prompts user to enter
* a choice of list and then returns the string stored at the index*/
public String shoutOutCannedMessage()
{
ArrayList<String> messages = new ArrayList<String>();
//declare 10 arrays
messages.add("india");
messages.add("america");
messages.add("england");
messages.add("australia");
messages.add("newzealand");
messages.add("japan");
Scanner keyboard = new Scanner(System.in);
for (int index = 0; index < messages.size(); index++)
System.out.println((index+1)+". "+messages.get(index));
System.out.println("Select a message");
int userChoice = keyboard.nextInt();
String message = messages.get(userChoice-1);
return message;
}
/**The method shoutOutRandomMessage that creates five array list
* of subjects, verbs, objects, adjectives and adverbs and
* returns a random sentence of order subject+verb+adjective+object +adverb
* */
public String shoutOutRandomMessage()
{
//Create a Scanner class object
Scanner keyboard = new Scanner(System.in);
//declare an arraylist of subject
ArrayList<String> subjects = new ArrayList<String>();
subjects.add("I");
subjects.add("He");
subjects.add("She");
subjects.add("We");
subjects.add("They");
//declare an arraylist of verb
ArrayList<String> verbs = new ArrayList<String>();
verbs.add("eat");
verbs.add("think");
verbs.add("talk");
verbs.add("Walk");
verbs.add("sing");
//declare an arraylist of verb
ArrayList<String> adjectives = new ArrayList<String>();
adjectives.add("active");
adjectives.add("bad");
adjectives.add("basic");
adjectives.add("better");
adjectives.add("black");
//declare an arraylist of objects
ArrayList<String> objects = new ArrayList<String>();
objects.add("me");
objects.add("him");
objects.add("her");
objects.add("us");
objects.add("them");
//declare an arraylist of adverbs
ArrayList<String> adverbs = new ArrayList<String>();
adverbs.add("here");
adverbs.add("slowly");
adverbs.add("quickly");
adverbs.add("outside");
adverbs.add("inside");
//Create an instacne of Random class
Random random=new Random();
String randomMessage="";
//call nextInt method on the size of subjects
//all array list are same size
int r=random.nextInt(subjects.size());
//Create random message of with array list index
randomMessage+=
subjects.get(r)+" "+
verbs.get(r)+" "+
adjectives.get(r)+" "+
objects.get(r)+" "+
adverbs.get(r)+" ";
//return random message
return randomMessage;
}
}
-----------------------------------------------
/**The java class that creates an instnce of ShoutBox and
* calls the two methods shoutOutCannedMessage and
* shoutOutRandomMessage*/
//ShoutBoxTester.java
public class ShoutBoxTester
{
public static void main(String[] args)
{
//Create a ShoutBox class object
ShoutBox box = new ShoutBox();
System.out.println("String : "+box.shoutOutCannedMessage());
System.out.println("Random Sentence : "+box.shoutOutRandomMessage());
}
}
-----------------------------------------------
sample output:
1. india
2. america
3. england
4. australia
5. newzealand
6. japan
Select a message
1
String : india
Random Sentence : We Walk better us outside
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.