Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the word game Mad Libs, people are asked to provide parts of speech such as n

ID: 3751770 • Letter: I

Question

In the word game Mad Libs, people are asked to provide parts of speech such as nouns, verbs, adverbs, or adjectives. These words are used to fill in the blanks of a template or to replace the same parts of speech in a sentence. In this task you're going to write a program to demonstrate how the game works for a single sentence. Consider this sentence from Ready Player One by Ernest Cline: A lich was an undead creature, usually an incredibly powerful wizard or king who had employed dark magic to bind his intellect to his own reanimated corpse, thus achieving a perverted form of immortality. Write a program that will do the following: Prompt the user for one verb, four nouns,and three adjectives Print the sentence above but with words replaced as follows: A [noun] was an undead [noun], usually an incredibly powerful [noun] or king who had employed [adjective] magic to [verb] his [noun] to his own [adjectivel corpse, thus achieving a [adjective] form of immortality. Your program should have three functions and use function calls as appropriate. It should end with a call to main (). Note that the main ) function doesn't require a return statement because the main) call doesn' t have a body or a parameter. Include the f ollowing functions: . get.sent-part ): Prompts user for part of speech; called multiple times from main) . build-sentence () : Uses string concatenation, i.e., combining string using the plus sign, as appropriate to build the sentence from the user's words and the sentence words

Explanation / Answer

import java.io.*;
import java.util.Scanner;

public class WordGame {
   static Scanner s=new Scanner(System.in);
  
  
// noun[] array is used to store nouns
   // adjectives[] array is used to store all 3 adjectives
   // verb is stored in variable verb
   static String noun[]=new String[4];
   static String adjectives[]=new String[4];
   static String verb="";
  
/*
   * Function to get one verb, 4 nouns and 3 adjectives from user
   */
   static void get_sent_part(){
      
       System.out.print("Enter the verb: ");
       verb=s.next();
       System.out.print("Enter 4 nouns: ");
       for(int i=0;i<4;i++){
           noun[i]=s.next();
       }
       System.out.println("Enter 3 adjectives: ");
       for(int i=0;i<3;i++){
           adjectives[i]=s.next();
       }
   }
  
/*
   * Function to replace verbs, nouns and adjectives from string
   * here we use replaceAll method in java to find a replace a string from sentence
   */
   static String build_sentence(String sentence){
      
// replace verb in sentence
       sentence=sentence.replaceAll(verb, "[verb]");

        // replace noun in sentence
       for(int i=0;i<4;i++){
           sentence=sentence.replaceAll(noun[i], "[noun]");
       }

        // replace adjectives in sentence
       for(int i=0;i<3;i++){
           sentence=sentence.replaceAll(adjectives[i], "[adjectives]");
       }
      
       return sentence;
   }

   public static void main(String[] args) {
      
       String sentence="A lich was an undead creature, ususally an incredibly powerfull"
               + " wizard or king who had employed dark magic to bind his intellect to his own reanimated corpse,"
               + " thus achieving a perverted form of immortality.";
       System.out.println("Initial Sentence ======="+sentence);
  
       get_sent_part();
       String final_sentence=build_sentence(sentence);
      
       System.out.println("Resultant Sentence ======="+final_sentence);
  
      
   }

}

//########################################## PGM END ###############


OUTPUT
#########

Initial Sentence =======A lich was an undead creature, ususally an incredibly powerfull wizard or king who had employed dark magic to bind his intellect to his own reanimated corpse, thus achieving a perverted form of immortality.

Enter the verb: bind
Enter 4 nouns: lich
creature
wizard
intellect
Enter 3 adjectives:
dark
reanimated
perverted

Resultant Sentence =======A [noun] was an undead [noun], ususally an incredibly powerfull [noun] or king who had employed [adjectives] magic to [verb] his [noun] to his own [adjectives] corpse, thus achieving a [adjectives] form of immortality.