I am completely lost and need help with this assignment. I cant get code to comp
ID: 3696049 • Letter: I
Question
I am completely lost and need help with this assignment. I cant get code to compile without error. 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 shoutbox;
import java.util.Scanner;
import java.util.Scanner;
/**
*
* @author lawrencestandley
*/
public class ShoutBox {
private static Object scanner;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
shoutOutRandomMessage();
shoutOutCannedMessage();
}
public static void shoutOutRandomMessage() {
String shoutSubject;
String shoutObject;
String shoutVerb;
String shoutAdverb;
String shoutAdjective;
int length; // instance variable that determines the length of the array
Scanner input = new Scanner(System.in); // the Scanner utility is added
System.out.println("You may choose how many words you want to enter. "
+ "Please write a number between 1 and 100 and hit Enter: ");
length = input.nextInt(); // takes user input of integer
int number = 0;
// create array to store user words
String[] subject = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Subject " + (counter+1) + ": ");
subject[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Subjects of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(subject[counter]);
}
// create array to store user words
String[] object = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Object " + (counter+1) + ": ");
object[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Objects of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(object[counter]);
}
// create array to store user words
String[] verb = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Verb " + (counter+1) + ": ");
subject[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Verbs of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(verb[counter]);
}
// create array to store user words
String[] adverb = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Adverb " + (counter+1) + ": ");
adverb[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Adverbs of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(adverb[counter]);
}
// create array to store user words
String[] adjective = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Adjective " + (counter+1) + ": ");
adjective[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Adjectives of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(adjective[counter]);
}
// retrieve random number that equals index and returns array element
int random = (int) (Math.random()*length);
int i = 0;
System.out.println( random == i);
subject[random] = subject[i]; // <<<<<<<<<<<<<------- I am lost
object[random] = object[i];
verb[random] = verb[i];
adverb[random] = adverb[i];
adjective[random] = adjective[i];
String message;
message[i] = subject[i], object[i], verb[i], adverb[i], adjective[i];
System.out.print(<message>.toString());
}
public 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");
int idx = scan.nextInt();
String message = messages[idx];
System.out.println(message);
}
}
}
Explanation / Answer
please check code . you have done program very well. i removed error in your program.please check it once.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cheggprograms;
import java.util.Scanner;
import java.util.Scanner;
/**
*
* @author lawrencestandley
*/
public class ShoutBox {
private static Object scanner;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
shoutOutRandomMessage();
System.out.println(shoutOutCannedMessage());
}
public static void shoutOutRandomMessage() {
String shoutSubject;
String shoutObject;
String shoutVerb;
String shoutAdverb;
String shoutAdjective;
int length; // instance variable that determines the length of the array
Scanner input = new Scanner(System.in); // the Scanner utility is added
System.out.println("You may choose how many words you want to enter. "
+ "Please write a number between 1 and 100 and hit Enter: ");
length = input.nextInt(); // takes user input of integer
int number = 0;
// create array to store user words
String[] subject = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Subject " + (counter+1) + ": ");
subject[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Subjects of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(subject[counter]);
}
// create array to store user words
String[] object = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Object " + (counter+1) + ": ");
object[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Objects of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(object[counter]);
}
// create array to store user words
String[] verb = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Verb " + (counter+1) + ": ");
subject[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Verbs of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(verb[counter]);
}
// create array to store user words
String[] adverb = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Adverb " + (counter+1) + ": ");
adverb[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Adverbs of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(adverb[counter]);
}
// create array to store user words
String[] adjective = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Please enter Adjective " + (counter+1) + ": ");
adjective[counter] = input.next(); // reads user input and stores the words in array
}
System.out.println("The Adjectives of your choice are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(adjective[counter]);
}
// retrieve random number that equals index and returns array element
int random = (int) (Math.random()*length);
int i = 0;
System.out.println( random == i);
subject[random] = subject[i];
object[random] = object[i];
verb[random] = verb[i];
adverb[random] = adverb[i];
adjective[random] = adjective[i];
String[] message=null;
message[i] = subject[i]+object[i]+ verb[i]+ adverb[i]+ adjective[i];
System.out.print(message[i].toString());
}
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.