How could I create the ShoutBox class for my project. I want this ShoutBox class
ID: 3764804 • Letter: H
Question
How could I create the ShoutBox class for my project. I want this ShoutBox class to 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.I want this to load this data structure with 10 messages. This can initialize an Array or ArrayList with the messages or have the user enter the messages. 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. Then I 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.
I would like to initialize the data structures by having the user enter the words.
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. After all this, how could I explain this on simple words?
Explanation / Answer
//ShoutBox.java
/**
* The class ShoutBox contains two methods shoutOutCannedMessage
* and . The method shoutOutCannedMessage that prompts user to enter
* */
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()
{
//Create a Scanner class object
Scanner keyboard = new Scanner(System.in);
//Declare an ArrayList to store 10 strings values
ArrayList<String> messages = new ArrayList<String>(10);
//declare 10 arrays
messages.add("india");
messages.add("america");
messages.add("england");
messages.add("australia");
messages.add("newzealand");
messages.add("japan");
messages.add("Ireland");
messages.add("Bangladesh");
messages.add("Singapore");
messages.add("srilanka");
//print array list values
for (int index = 0; index < messages.size(); index++)
System.out.println((index+1)+". "+messages.get(index));
System.out.println("Select a message");
//prompt for user input to select a number
int userChoice = keyboard.nextInt();
//Get the string at user selected string
String country = messages.get(userChoice-1);
//return message
return country;
}
/**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()
{
//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 of rand object on the size of subjects
int subjectRand=random.nextInt(subjects.size());
//call nextInt method of rand object on the size of verbs
int verbRand=random.nextInt(verbs.size());
//call nextInt method of rand object on the size of adjectives
int adjectiveRand=random.nextInt(adjectives.size());
//call nextInt method of rand object on the size of objects
int objectRand=random.nextInt(objects.size());
//call nextInt method of rand object on the size of adverbs
int adverbRand=random.nextInt(adverbs.size());
//Create random message of with array list data strucures
randomMessage+=
subjects.get(subjectRand)+" "+
verbs.get(verbRand)+" "+
adjectives.get(adjectiveRand)+" "+
objects.get(objectRand)+" "+
adverbs.get(adverbRand)+" ";
//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();
//Calling shoutOutCannedMessage() method
System.out.println("From shoutOutCannedMessage method ");
System.out.println("String : "+box.shoutOutCannedMessage());
//Calling shoutOutRandomMessage() method
System.out.println("From shoutOutRandomMessage method ");
System.out.println("Random Sentence : "+box.shoutOutRandomMessage());
}
}
----------------------------------------------------------
Sample Output:
From shoutOutCannedMessage method
1. india
2. america
3. england
4. australia
5. newzealand
6. japan
7. Ireland
8. Bangladesh
9. Singapore
10. srilanka
Select a message
2
String : america
From shoutOutRandomMessage method
Random Sentence : I Walk black us here
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.