Java (basic intro level-so I can understand the code) Create a Java Method shout
ID: 3764136 • Letter: J
Question
Java (basic intro level-so I can understand the code)
Create a Java Method shoutOutCannedMessage() will return type String.
The shoutOutCannedMessage will use an Array or an ArrayList to store 10 messages of type String.
Load this data structure with 10 messages of your choosing.
You can initialize your Array or ArrayList with the messages "readysetgo".
The 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.
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.
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.
Please document the code for the, shoutOutRandomMessage() method to explain the code.
thanks,
Explanation / Answer
String shoutOutCannedSubject(){
String subject[] = {"Subject1", "Subject2", "Subject2", "Subject3", "Subject4", "Subject5", "Subject6", "Subject7", "Subject8", "Subject9", "Subject10"};
String object[] = {"Object1", "Object2", "Object2", "Object3", "Object4", "Object5", "Object6", "Object7", "Object8", "Object9", "Object10"};
String adjective[] = {"Adjective1", "Adjective2", "Adjective2", "Adjective3", "Adjective", "Adjective5", "Adjective6", "Adjective7", "Adjective8", "Adjective9", "Adjective10"};
String verb[] = {"Verb1", "Verb2", "Verb2", "Verb3", "Verb4", "Verb", "Verb6", "Verb7", "Verb8", "Verb9", "Verb10"};
String adverb[] = {"Adverb1", "Adverb2", "Adverb2", "Adverb3", "Adverb4", "Adverb5", "Adverb6", "Adverb7", "Adverb8", "Adverb9", "Adverb10"};
System.out.println("The subjects are:");
for(int i = 0; i < 10; i++){
System.out.print(subject[i] + " ");
}
Scanner in = new Scanner(System.in);
System.out.print("Select a subject number to send(1-10): ");
int num = in.nextInt();
String str = subject[num - 1];
System.out.println("The verbs are:");
for(int i = 0; i < 10; i++){
System.out.print(verb[i] + " ");
}
System.out.print("Select a verb number to send(1-10): ");
num = in.nextInt();
str += verb[num - 1];
System.out.println("The adjectives are:");
for(int i = 0; i < 10; i++){
System.out.print(adjective[i] + " ");
}
System.out.print("Select a adjective number to send(1-10): ");
num = in.nextInt();
str += adjective[num - 1];
System.out.println("The objects are:");
for(int i = 0; i < 10; i++){
System.out.print(subject[i] + " ");
}
System.out.print("Select an object number to send(1-10): ");
num = in.nextInt();
str += object[num - 1];
System.out.println("The adverbs are:");
for(int i = 0; i < 10; i++){
System.out.print(adverb[i] + " ");
}
System.out.print("Select an adverb number to send(1-10): ");
num = in.nextInt();
str += adverb[num - 1];
return str;
}
String shoutOutRandomMessage(){
String subject[] = {"Subject1", "Subject2", "Subject2", "Subject3", "Subject4", "Subject5", "Subject6", "Subject7", "Subject8", "Subject9", "Subject10"};
String object[] = {"Object1", "Object2", "Object2", "Object3", "Object4", "Object5", "Object6", "Object7", "Object8", "Object9", "Object10"};
String adjective[] = {"Adjective1", "Adjective2", "Adjective2", "Adjective3", "Adjective", "Adjective5", "Adjective6", "Adjective7", "Adjective8", "Adjective9", "Adjective10"};
String verb[] = {"Verb1", "Verb2", "Verb2", "Verb3", "Verb4", "Verb", "Verb6", "Verb7", "Verb8", "Verb9", "Verb10"};
String adverb[] = {"Adverb1", "Adverb2", "Adverb2", "Adverb3", "Adverb4", "Adverb5", "Adverb6", "Adverb7", "Adverb8", "Adverb9", "Adverb10"};
Random r = new Random();
String str = subject[r.nextInt() % 10];
str += verb[r.nextInt() % 10];
str += adjective[r.nextInt() % 10];
str += object[r.nextInt() % 10];
str += adverb[r.nextInt() % 10];
return str;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.