Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Complete creatStory method to replace symbols in template with randomly selected nouns, verbs, and adjectives -Parameter story contains a templace - replace # with a

Complete creatStory method to replace symbols in template with randomly selected nouns, verbs, and adjectives

-Parameter story contains a templace

- replace # with a random noun - must use the value returned by getRandomNoun()

- replace @ with a random verb - must use the value returned by getRandomVerb()

- replace & with a random adjective - must use the value returned by getRandomAdjective()

import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import static java.lang.System.*;

public class MadLib { private ArrayList verbs; private ArrayList nouns; private ArrayList adjectives; // no changes needed to the following constructor public MadLib() { verbs = new ArrayList(); nouns = new ArrayList(); adjectives = new ArrayList(); loadVerbs(); loadNouns(); loadAdjectives(); }

// no changes needed to the following method public void loadNouns() { try{ Scanner file = new Scanner(new File("nouns.dat")); while(file.hasNext()) { nouns.add(file.next()); } } catch(Exception e) { System.out.println("Couldn't load nouns!"); } } // no changes needed to the following method public void loadVerbs() { try{ Scanner file = new Scanner(new File("verbs.dat")); while(file.hasNext()) { verbs.add(file.next()); } } catch(Exception e) { System.out.println("Couldn't load verbs!"); } } // no changes needed to the following method public void loadAdjectives() { try{ Scanner file = new Scanner(new File("adjectives.dat")); while(file.hasNext()) { adjectives.add(file.next()); } } catch(Exception e) { System.out.println("Couldn't load adjectives!"); } }

public void createStory(String story) { // replace # with a random noun // replace @ with a random verb // replace & with a random adjective // otherwise print the character String out= ""; System.out.println(out); }

public String getRandomVerb() { // select a verb in the verbs list // by randomly generating a number // between 0 and the size of the list // use that number to select the value at that spot // return the value as a string

String verbStr=""; int num = 0; num = (int)(Math.random()*(verbs.size()-1)); verbStr = verbs.get(num); return verbStr; } public String getRandomNoun() { // select a noun in the nounss list // by randomly generating a number // between 0 and the size of the list // use that number to select the value at that spot // return the value as a string

String noun = ""; int num = 0; if(nouns == null){ loadNouns(); } double rand = (Math.random()); num = (int)(rand * (nouns.size()-1)); out.println(num); noun = nouns.get((int) num); out.print(noun); return noun; } public String getRandomAdjective() { // select an adjective in the adjectives list // by randomly generating a number // between 0 and the size of the list // use that number to select the value at that spot // return the value as a string

String adjStr=""; int num = 0; num = (int)(Math.random()*(adjectives.size()-1)); adjStr = adjectives.get(num); return adjStr; }

// no changes needed to the following method public String toString() { return " "; } }

Runner code:

import static java.lang.System.*;

public class MadLibRunner { public static void main( String args[] ) { MadLib story = new MadLib(); for(int i=1; i<6; i++){ System.out.println("Story " + i + " :: "); story.createStory("The # @ after the & & # while the # @ the #"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions