Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

java. I will upvote this. please mention what info is missing or pass to someone else. I am also attaching my program but it is

java. I will upvote this. please mention what info is missing or pass to someone else. I am also attaching my program but it is not giving the required output. please send me correct one as soon as possible

Problem description

Mad Libs is a game where participants are asked for a list of strings. The strings are then used to fill in the blanks in a sentence and the sentence is read out loud. Write a program called MadLibs that will simulate a game of Mad Libs, with some slight modifications. The strings for the sentences are stored in separate text files. The program will need to read the strings and generate (and print) sentences, the number of which is determined by the user.

Additional details

The sentence The sentence template that should be generated is "Six ---------- ---------- sit in the ----------. Do they fit? ----------". Intuitively, each blank should be filled with a word of suitable type: The first blank is filled with an adjective. The second blank line is filled with the name of an animal. The third blank is filled with an object, and the fourth blank is filled with an answer. However, because this assignment uses random numbers, it is okay to fill the blanks with words that come in any order. For example, the first blank is filled with words from strings2.txt, the second blank is filled with words from strings3.txt, etc.

Strings stored in separate files The strings that should be used in the blanks are stored in four separate text files. Each text file will contain multiple strings that are placed in separate lines. Preceded by these strings is a number specifying how many strings are stored in the text file. For example, the following is the contents of a text file named strings4.txt containing five strings. The first line is the number five, signifying that there are five strings after it (i.e., "No!", "Yes!", "Not a bit.", "Are you kidding?", and "Splat!"). Note that a single string for the Mad Lib sentence can contain multiple words (e.g., "Are you kidding?").

5 No! Yes! Not a bit. Are you kidding? Splat! 

Store strings in arrays Each time the program generates a sentence, it should randomly choose a string for each blank. To facilitate this, the strings in the text files should be stored in separate arrays. That is, store all the possible adjectives in one array, then store the possible animal names in another array, and so on. This is where the number on the first line of each file comes in handy. You can use these values to declare and initialize each array.

Input value validation You may assume that the file names entered by the user will not have any spaces in them. You may assume that the values entered for the seed and the number of sentences will be integer values. However. make sure that the seed value that the user enters is positive. Positive in this case does not include zero. Also, make sure that the value the user enters for the number of sentences is between 1-50 (inclusive).

Choosing a string Use a random number to determine which string in each type should be used. Seeing that the strings are stored in an array, the random numbers generated should be values that can be used to index into the array. Hence, each time a string is needed from the array, generate a random number between zero and the last valid index value of that array.

Don't waste random numbers! Each random number generated should be used to access an element in one of the arrays. Related to this, for each sentence, the strings for that sentence should be retrieved from the files and fill the blanks in the same order that the file names are entered by the user. That is, if the user provides strings1.txt, strings2.txt, strings3.txt, and strings4.txt in this order, generate a random number to retrieve an adjective first, then generate another random number to retrieve an animal name, then another random number for an object, and finally another random number for an answer.

Printing the sentences Allocate two spaces for the number value in each output of a sentence. Also, when only one sentence will printed, use the header "Here is the sentence:" rather than "Here are the sentences:".

Sample run 1

The input strings1.txt is for the file containing adjectives. The input strings2.txt is for the file containing animal names. The input strings3.txt is for the file containing objects. The input strings4.txt is for the file containing answers. The input 1101 is for the seed value and the input 10 is for the number of sentences.

Input:

strings1.txt strings2.txt strings3.txt strings4.txt 1101 10 

Run:

Enter file name for adjectives: strings1.txt strings1.txt successfully found. Enter file name for animals: strings2.txt strings2.txt successfully found. Enter file name for objects: strings3.txt strings3.txt successfully found. Enter file name for answers: strings4.txt strings4.txt successfully found. Enter a seed: 1101 Enter number of sentences to produce: 10 Here are the sentences: Sentence 1: Six big pigs sit in the mitt. Do they fit? No! Sentence 2: Six big worms sit in the mitt. Do they fit? Yes! Sentence 3: Six hip worms sit in the lid. Do they fit? Yes! Sentence 4: Six sick dogs sit in the van. Do they fit? No! Sentence 5: Six big chickens sit in the van. Do they fit? Splat! Sentence 6: Six little pigs sit in the mitt. Do they fit? No! Sentence 7: Six little chickens sit in the lid. Do they fit? Splat! Sentence 8: Six sick chickens sit in the van. Do they fit? Yes! Sentence 9: Six mad dogs sit in the mitt. Do they fit? No! Sentence 10: Six hip chickens sit in the mitt. Do they fit? No! 

Sample run 2

Since there are several (many) input values, the following table summarizes which input value is associated with which prompt. The last value in each list is the valid value.

First file Second file Third file Fourth file Seed value Number of sentences
test camel.txt class.txt yes.txt -1 -2
hello chickens rooms.txt no -1101 0
strings1.txt cats strings3.txt maybe.txt 0 51
strings2.txt okay 1101 100
well.txt 5
strings4.txt

Input:

test hello strings1.txt camel.txt chickens cats strings2.txt class.txt rooms.txt strings3.txt yes.txt no maybe.txt okay well.txt strings4.txt -1 -1101 0 1101 -2 0 51 100 5 

Run:

Enter file name for adjectives: test File does not exist, try again: hello File does not exist, try again: strings1.txt strings1.txt successfully found. Enter file name for animals: camel.txt File does not exist, try again: chickens File does not exist, try again: cats File does not exist, try again: strings2.txt strings2.txt successfully found. Enter file name for objects: class.txt File does not exist, try again: rooms.txt File does not exist, try again: strings3.txt strings3.txt successfully found. Enter file name for answers: yes.txt File does not exist, try again: no File does not exist, try again: maybe.txt File does not exist, try again: okay File does not exist, try again: well.txt File does not exist, try again: strings4.txt strings4.txt successfully found. Enter a seed: -1 Not a positive number, try again: -1101 Not a positive number, try again: 0 Not a positive number, try again: 1101 Enter number of sentences to produce: -2 Not between 1 and 50, try again: 0 Not between 1 and 50, try again: 51 Not between 1 and 50, try again: 100 Not between 1 and 50, try again: 5 Here are the sentences: Sentence 1: Six big pigs sit in the mitt. Do they fit? No! Sentence 2: Six big worms sit in the mitt. Do they fit? Yes! Sentence 3: Six hip worms sit in the lid. Do they fit? Yes! Sentence 4: Six sick dogs sit in the van. Do they fit? No! Sentence 5: Six big chickens sit in the van. Do they fit? Splat! 

Required program decomposition

For this exercise, the size of any methods in terms of the number of lines used will be restricted. A test case will check that each method you implemented (including the main method) contains at most 20 lines of code. Empty lines, comment lines, and lines containing only an ending curly bracket are not counted.

only these are the acceptable txt files.

string1.txt

5 big little mad hip sick

string2.txt

4 pigs dogs chickens worms

string3.txt

4 lid van class mitt

string4.txt

5 No! Yes! Not a bit. Are you kidding? Splat!

my program not correct.

import java.util.*; import java.io.*; public class MadLibs { public static void main(String[] args) throws FileNotFoundException { Scanner readInput = new Scanner(System.in); //for (int i = 0; i++) { System.out.print("Enter file name for adjectives: " + readInput + " successfully found."); System.out.println("Enter file name for animal: " + readInput + " successfully found."); System.out.println("Enter file name for animals: " + readInput + " successfully found."); System.out.println("Enter file name for aobjects: " + readInput + " successfully found."); String fileName = readInput.nextLine(); File inputFile = new File(fileName); while(!inputFile.exists()) { System.out.print("File not found. Try again: "); fileName = readInput.nextLine(); inputFile = new File(fileName); } System.out.print("Output file name: "); String outputFile = readInput.nextLine(); System.out.println(); PrintStream output = new PrintStream(new File(outputFile)); madLibGame(inputFile, output, readInput); System.out.println("Your mad-lib file has been created! You can access your " + outputFile + "" + "in your " + System.getProperty("user.dir")); } public static void madLibGame(File inputFile, PrintStream output, Scanner readInput) throws FileNotFoundException { Scanner input = new Scanner(inputFile); int lineCount = 0; int wordCount = 0; while (input.hasNextLine()) { // has next line String line = input.nextLine(); Scanner lineToken=new Scanner(line); ++lineCount; while (lineToken.hasNext()) { //has next word int placeHolderLength = 0; String word = lineToken.next(); ++wordCount; if (word.substring(0, 1).equals("<")) { //placeholdercheck placeHolderLength = wordLength(word); // if placeHolderLength < 2 then its not a valid one if (placeHolderLength>2) { //Remove "<" and ">" String placeHolder = word.substring(1, placeHolderLength-1); //Convert to lower case as well remove the "-" String currPlaceHolder = placeHolder.toLowerCase().replaceAll("-", " "); System.out.print("Please type "); boolean vowel = isVowel(placeHolder); if (vowel) { System.out.print("an "+ currPlaceHolder + ":"); } else { System.out.print("a "+ currPlaceHolder + ":"); } String nextPlaceHolder = readInput.nextLine(); //write this in file if it really is a placeholder System.out.print(nextPlaceHolder + " "); } else output.print(word +" "); } else output.print(word +" "); } output.println(""); } System.out.println(); System.out.println("Your story is made out of " + lineCount + " lineswith " + wordCount + " word.");} public static int wordLength(String word) { int length = word.length(); if (word.substring(length-1).equals(">")) { // Check if the wordends with ">" return (length); } else return 1;} public static boolean isVowel(String word) { if (word.startsWith("a") || word.startsWith("A")|| word.startsWith("e") || word.startsWith("E") || word.startsWith("i") || word.startsWith("I") || word.startsWith("o") || word.startsWith("O") || word.startsWith("u") || word.startsWith("U")) { return true; } else return false; } }

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 Accounting questions