Create a java program to produce random sentences using arrays. There should be
ID: 3717381 • Letter: C
Question
Create a java program to produce random sentences using arrays. There should be 3 arrays. One for noun (20 different words). Second for verbs ( 20 different words). Third for prepostions (20 different ones).
Must have more more than the main method. Include a method to display 4 strings. This method should take the pararmeter of the 3 arrays and return a random sentece uing the strings. The strings must be random. The format of the sentence is -----> (noun)(verb)(prepostion)(noun).
Hints: Use methods, for loops, arrayVariblelength to accesss random indexes, and random from math class ---> (int)(Math.random() * variavle.legnth)
Explanation / Answer
import java.util.*;
public class RandomSentence{
public static String makeSentence(string nouns[], string verbs[], string prepositions[]){
Random rand = new Random();
//Selecting random noun, verb and preposition
String noun1 = nouns[rand.nextInt(nouns.length)];
String verb = verbs[rand.nextInt(verbs.length)];
String preposition = prepositions[rand.nextInt(prepositions.length)];
String noun2 = nouns[rand.nextInt(nouns.length)];
//Returning the formed sentences
return noun1 + " " + verb + " " + preposition + " " + noun2;
}
public static void main(String args[]){
//20 nouns, 20 verbs, 20 prepositions
String[] nouns={" Ram"," Shyam"," dog"," town"," car","face","church", "Mohan","Temple", "Ravi", "Mohit", "Dabba", "Gaurav", "Actor","question", "room","school","state","story","student"};
String[] verbs={"ask", "be","become","begin","call","can","come","could","do","feel","find","get","give","go","have","hear","help","keep","know","leave"};
String[] prepositions={"as","at","but","by","down","for","from","in","into","like","near","next","of","off","on","onto","out","over","past","plus"};
//Printing 4 random sentences. however this can be changed to any number
for(int i=1;i<5;i++)
{
System.out.println(makeSentence(nouns, verbs, prepositions));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.