Java Question: Assignment requires the creation of the ShoutBox class with two m
ID: 3582425 • Letter: J
Question
Java Question: Assignment requires the creation of the ShoutBox class with two methods:
Method shoutOutCannedMessage() will return type String. The shoutOutCannedMessage will use an Array or an ArrayList to store 10 messages of type String. 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. One data structure will store the canned messages. You can load this data structure with canned messages of your choosing. The shoutOutCannedMessage() method 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.
Action taken: I copied the following code from Chegg and I created a java main file, a ShoutBox file and two java classes (ShoutBoxTester.java) under the same Source Package.:
Java Program:
import java.util.*;
//Program that displays some messages and prints the user selected, generates randomly formed message
class ShoutBox
{
//Method that displays some messages and prompts the user to select one and displays it
public String shoutOutCannedMessage()
{
Scanner sc = new Scanner(System.in);
//Storing some messages
String[] messages = {" Who we were is not who we are, but who we were made who we are. it is unimportant, and essential...",
" The people who choose easy things, often end up saying life is tough..",
" As mountains were once a granule, reality was once a dream ",
" If you don't care about your job, then why do you care about your paycheck?",
" When pain ceases pride increases!",
" Make each day of your life better than yesterday",
" You fail only when you stop trying.....",
" If you're going to live your life, make sure it's yours...",
" Strange but true, sometimes listening sad songs actually makes you happy",
" I'm a human with a heart not a engine to restart..!!"};
//Looping over the messages to print messages
for(int i=0; i<10; i++)
{
System.out.println(" (" + (i+1) + ") " + messages[i]);
}
int choice;
//Accept a number from user. Number should be between 1-10
do
{
System.out.print(" Select a message number (1-10): ");
choice = sc.nextInt();
}while(choice<1 || choice>10);
//Return the message
return messages[choice-1];
}
//Created another Java file under the same Source package as above java main file
//Method that returns randomly formed message
public String shoutOutRandomMessage()
{
Scanner sc = new Scanner(System.in);
//Arrays that holds subject, verb, adjective, object, adverb
String[] subjects = {"John","Mike","They","You","Everything","Everyone","anybody","Crocodiles","It","Jury"};
String[] verbs = {"accept","bless","bow","carry","deliver","fetch","grab","increase","melt","pour"};
String[] adjectives = {"good","new","first","last","long","great","little","high","different","small"};
String[] objects = {"football","the poem","soccer","the dogs","to leave","his car", "cricket","the books","a postcard","was singing"};
String[] adverb = {"always","deliberately","finally","kindly","loosely","wildly","quickly","safely","poorly","mysteriously"};
//Random class
Random rand = new Random();
//Generate five random numbers
int s_r = rand.nextInt(10);
int v_r = rand.nextInt(10);
int a_r = rand.nextInt(10);
int o_r = rand.nextInt(10);
int ad_r = rand.nextInt(10);
//Forming a string
String gen = subjects[s_r] + " " + verbs[v_r] + " " + adjectives[a_r] + " " + objects[o_r] + " " + adverb[ad_r];
//Return the string
return gen;
}
//Created a Java class file (VirtualWorld) under the same Source package as above java main file
//}
//class VirtualWorld
//{
//Main Method
public static void main(String[] args)
{
//Create an object of ShoutBox class
ShoutBox sb = new ShoutBox();
//Get the user selected message
String mesg = sb.shoutOutCannedMessage();
System.out.println(" Message Selected: " + mesg);
//Get the randomly formed message
String gen_mesg = sb.shoutOutRandomMessage();
System.out.println(" Random Message generated: " + gen_mesg);
}
}
Results: Using NetBeans, I ran the second java class. I received the following Error message: Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet. ShoutBox.shoutOutCannedMessage(ShoutBox.java:15) at Filename.main
Request: Please provide solution or how to eliminate error message. Thank you.
Explanation / Answer
import java.util.*;
//Program that displays some messages and prints the user selected, generates randomly formed message
class ShoutBox
{
//Method that displays some messages and prompts the user to select one and displays it
public String shoutOutCannedMessage()
{
Scanner sc = new Scanner(System.in);
//Storing some messages
String[] messages = {" Who we were is not who we are, but who we were made who we are. it is unimportant, and essential...",
" The people who choose easy things, often end up saying life is tough..",
" As mountains were once a granule, reality was once a dream ",
" If you don't care about your job, then why do you care about your paycheck?",
" When pain ceases pride increases!",
" Make each day of your life better than yesterday",
" You fail only when you stop trying.....",
" If you're going to live your life, make sure it's yours...",
" Strange but true, sometimes listening sad songs actually makes you happy",
" I'm a human with a heart not a engine to restart..!!"};
//Looping over the messages to print messages
for(int i=0; i<10; i++)
{
System.out.println(" (" + (i+1) + ") " + messages[i]);
}
int choice;
//Accept a number from user. Number should be between 1-10
do
{
System.out.print(" Select a message number (1-10): ");
choice = sc.nextInt();
}while(choice<1 || choice>10);
//Return the message
return messages[choice-1];
}
//Created another Java file under the same Source package as above java main file
//Method that returns randomly formed message
public String shoutOutRandomMessage()
{
Scanner sc = new Scanner(System.in);
//Arrays that holds subject, verb, adjective, object, adverb
String[] subjects = {"John","Mike","They","You","Everything","Everyone","anybody","Crocodiles","It","Jury"};
String[] verbs = {"accept","bless","bow","carry","deliver","fetch","grab","increase","melt","pour"};
String[] adjectives = {"good","new","first","last","long","great","little","high","different","small"};
String[] objects = {"football","the poem","soccer","the dogs","to leave","his car", "cricket","the books","a postcard","was singing"};
String[] adverb = {"always","deliberately","finally","kindly","loosely","wildly","quickly","safely","poorly","mysteriously"};
//Random class
Random rand = new Random();
//Generate five random numbers
int s_r = rand.nextInt(10);
int v_r = rand.nextInt(10);
int a_r = rand.nextInt(10);
int o_r = rand.nextInt(10);
int ad_r = rand.nextInt(10);
//Forming a string
String gen = subjects[s_r] + " " + verbs[v_r] + " " + adjectives[a_r] + " " + objects[o_r] + " " + adverb[ad_r];
//Return the string
return gen;
}
//Created a Java class file (VirtualWorld) under the same Source package as above java main file
//}
//class VirtualWorld
//{
//Main Method
public static void main(String[] args)
{
//Create an object of ShoutBox class
ShoutBox sb = new ShoutBox();
//Get the user selected message
String mesg = sb.shoutOutCannedMessage();
System.out.println(" Message Selected: " + mesg);
//Get the randomly formed message
String gen_mesg = sb.shoutOutRandomMessage();
System.out.println(" Random Message generated: " + gen_mesg);
}
}
output :
1) Who we were is not who we are, but who we were made who we are. it is unimportant, and essential...
(2) The people who choose easy things, often end up saying life is tough..
(3) As mountains were once a granule, reality was once a dream
(4) If you don't care about your job, then why do you care about your paycheck?
(5) When pain ceases pride increases!
(6) Make each day of your life better than yesterday
(7) You fail only when you stop trying.....
(8) If you're going to live your life, make sure it's yours...
(9) Strange but true, sometimes listening sad songs actually makes you happy
(10) I'm a human with a heart not a engine to restart..!!
Select a message number (1-10):
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.