Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I having some trouble with the following assignment. I am having trouble getting

ID: 3696675 • Letter: I

Question

I having some trouble with the following assignment. I am having trouble getting the shoutOutCannedMessage method to work. the shoutOutRandomMessage works but that is where it stops.

the following is the assignment followed by my source code: 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.

package cannedmessages;

/**
*
* @author lawrencestandley
*/
import java.util.Scanner;
public class CannedMessages {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String stringmess[]={"I love Java",
"The great taste of waking up with Java in your cup",
"Java not just a drink",
"Mountain grown",
"Fill it to the rim with Java",
"Choosing moms choose Java",
"Java the quicker picker upper",
"Please dont squeeze the Java",
"Where is the Java",
"I am Java and I am JavaScript"};
String selectedMessage=shoutOutCannedMessage(stringmess);
System.out.println("You selected :"+selectedMessage);
  
}
public static String shoutOutCannedMessage(String stringmess[]){
Scanner scanner = null;
try{
scanner = new Scanner(System.in);
for (int i=0;i<stringmess.length;i++){
System.out.println((i+1)+"."+stringmess[i]);
}
System.out.print("Select Message :");
int n = scanner.nextInt();
  
if (n>0 && n <= 10)
return stringmess[n - 1];
else
return "Invalie Message Selection";
  
}catch (Exception e){
  
}
return null;
}
public static String shoutOutCannedMessage() {
String messages[] = new String[10];
//declare 10 arrays
messages[0] = "Pepperoni";
messages[1] = "Olives";
messages[2] = "Cheese";
messages[3] = "Onions";
messages[4] = "Bacon";
messages[5] = "Tomato sauce";
messages[6] = "Bell peppers";
messages[7] = "Mushrooms";
messages[8] = "Sausage";
messages[9] = "Beef";
for (int i = 0; i < messages.length; i++) {
System.out.println(i+". "+messages[i]); //Should print the messages
}
System.out.println("Select a message");
Scanner s=new Scanner(System.in);

int idx = s.nextInt();
String message = messages[idx];
System.out.println(message);
return message;
}
}
  
  

Explanation / Answer

Hi, I have implemented shoutOutRandomMessage () method as per your requirement. Please find the below updated class. Highlighted lines are the code changes.

CannedMessages.java


import java.util.Random;
import java.util.Scanner;
public class CannedMessages {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String stringmess[]={"I love Java",
"The great taste of waking up with Java in your cup",
"Java not just a drink",
"Mountain grown",
"Fill it to the rim with Java",
"Choosing moms choose Java",
"Java the quicker picker upper",
"Please dont squeeze the Java",
"Where is the Java",
"I am Java and I am JavaScript"};
String selectedMessage=shoutOutCannedMessage(stringmess);
System.out.println("You selected :"+selectedMessage);
/*this method used for find message randomly without user interface*/
String randomselectedMessage=shoutOutRandomMessage(stringmess);
System.out.println("Random selected :"+randomselectedMessage);

}
public static String shoutOutCannedMessage(String stringmess[]){
Scanner scanner = null;
try{
scanner = new Scanner(System.in);
for (int i=0;i<stringmess.length;i++){
System.out.println((i+1)+"."+stringmess[i]);
}
System.out.print("Select Message :");
int n = scanner.nextInt();
  
if (n>0 && n <= 10)
return stringmess[n - 1];
else
return "Invalie Message Selection";
  
}catch (Exception e){
  
}
return null;
}
public static String shoutOutCannedMessage() {
String messages[] = new String[10];
//declare 10 arrays
messages[0] = "Pepperoni";
messages[1] = "Olives";
messages[2] = "Cheese";
messages[3] = "Onions";
messages[4] = "Bacon";
messages[5] = "Tomato sauce";
messages[6] = "Bell peppers";
messages[7] = "Mushrooms";
messages[8] = "Sausage";
messages[9] = "Beef";
for (int i = 0; i < messages.length; i++) {
System.out.println(i+". "+messages[i]); //Should print the messages
}
System.out.println("Select a message");
Scanner s=new Scanner(System.in);
int idx = s.nextInt();
String message = messages[idx];
System.out.println(message);
return message;
}
  
public static String shoutOutRandomMessage(String stringmess[]) {
   /*Random class used to get the random value*/
   Random r = new Random();
   /*finding length of the array of messages*/
   int length = stringmess.length;
   /*Getting random value with in array limit. So you can get random value from 0 to array (length -1)*/
   int randomNumber = r.nextInt(length+1);
   return stringmess[randomNumber - 1];
}

}
  
Output:

1.I love Java
2.The great taste of waking up with Java in your cup
3.Java not just a drink
4.Mountain grown
5.Fill it to the rim with Java
6.Choosing moms choose Java
7.Java the quicker picker upper
8.Please dont squeeze the Java
9.Where is the Java
10.I am Java and I am JavaScript
Select Message :5
You selected :Fill it to the rim with Java
Random selected :Choosing moms choose Java