(java) Create the ShoutBox class. Your ShoutBox class will have a shoutOutCanned
ID: 3761110 • Letter: #
Question
(java) Create the ShoutBox class. Your ShoutBox class will have a shoutOutCannedMessage() method that returns a String type. The shoutOutCannedMessage will use an Array or an ArrayList to store 10 messages of type String. You can load this data structure with 10 messages of your choosing. For example, one message could be “I need Java!” Initialize your Array or ArrayList with the messages. 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.
Explanation / Answer
package current;
import java.util.ArrayList;
import java.util.Scanner;
public class ShoutBox {
public static void main(String[] args) {
ShoutBox sbox = new ShoutBox();
System.out.println("Selected message: "+sbox.shoutOutCannedMessage());
}
public String shoutOutCannedMessage() {
ArrayList<String> messages = new ArrayList<String>();
messages.add("I need java");
messages.add("Create the ShoutBox class");
messages.add("Your ShoutBox class will have a shoutOutCannedMessage() method that returns a String type");
messages.add("The shoutOutCannedMessage will use an Array or an ArrayList to store 10 messages of type String. ");
messages.add("You can load this data structure with 10 messages of your choosing.");
messages.add("Initialize your Array or ArrayList with the messages");
messages.add("The shoutOutCannedMessage() will loop through the data structure to first display all canned messages");
messages.add("and allow the user to select one");
messages.add("The shoutOutCannedMessage() will return the selected message String.");
messages.add("Java Code provided");
System.out.println("Choose one of the following messages:");
for(int i=0;i < messages.size(); i++) {
System.out.println((i+1)+"."+messages.get(i));
}
System.out.println("Enter your choice: [1 to 10]");
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
while(option <= 0 || option > 10) {
System.out.println("Enter valid choice: ");
option = sc.nextInt();
}
return messages.get(option-1);
}
}
-----------------------output-----------------------------
Choose one of the following messages:
1.I need java
2.Create the ShoutBox class
3.Your ShoutBox class will have a shoutOutCannedMessage() method that returns a String type
4.The shoutOutCannedMessage will use an Array or an ArrayList to store 10 messages of type String.
5.You can load this data structure with 10 messages of your choosing.
6.Initialize your Array or ArrayList with the messages
7.The shoutOutCannedMessage() will loop through the data structure to first display all canned messages
8.and allow the user to select one
9.The shoutOutCannedMessage() will return the selected message String.
10.Java Code provided
Enter your choice: [1 to 10]
-1
Enter valid choice:
0
Enter valid choice:
5
Selected message:
You can load this data structure with 10 messages of your choosing.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.