CSCI-1320 Homework 11: Madlibs

Instructor: Todd Dole

Purpose Statement

This assignment gives opportunity to practice with methods and parameters. We will create several methods, with parameters and return values, and use them to play the children's game "Mad-libs"

Instructions

Construct a program that will play a game of Mad-libs with the user.

Example Mad-libs are available on the madlibs.com website. A sample is found here.

The idea is that we ask the player for a series of words of specific types (noun, adjective, verb etc) and use the randomly picked words to fill in the blanks in a story or essay, creating a funny result.

We will implement this in Java using two methods, which we will call from our main method:

Create one method which will prompt the user for a word, and then use Scanner to read their response and return the word as a String. We will pass the method one parameter - A string with the part of speech for the word we need.

Create a second method which will print a madlib. We will "fill in the blanks" by passing at least three String parameters, representing three words to replace in a short story. (More is acceptable if you want to create a longer madlib!)

Our main method will contain a loop that will call these other methods, ask the player if they want to play again, and repeat the game if they answer yes.


For example:

                Please write a adjective: hairy              <-- In method one, the first time
                Please write a adjective: delicious          <-- In method one, the second time
                Please write a noun: pizza                   <-- In method one, the third time
                Here is your madlib:                         <-- In method two
                A vacation is when you take a trip to some hairy place with your delicious family.  Usually you go to some place that is near a pizza.

                Do you want to play again? Yes/No: Yes

                Please write a adjective: groovy             <-- In method one, the first time (for round 2)
                Please write a adjective: enormous           <-- In method one, the second time (for round 2)
                Please write a noun: shark                 <-- In method one, the third time (for round 2)
                Here is your madlib:                         <-- In method two (for round 2)
                A vacation is when you take a trip to some groovy place with your enormous family.  Usually you go to some place that is near a shark.

                Do you want to play again? Yes/No: No

            

Call your class MadLibs, and save your file as MadLibs.com

Your two methods should be placed inside the MadLibs class, but outside of the main() method.

Because they will be called from the main() method, they should both use the static keyword.

A tip on comparing Strings: in order to compare two Strings in java, we cannot use == like with primitive data types. Instead, we must use the .equals String method, like this:

                Scanner scan;
                String userInput;
                scan = new Scanner(System.in);

                    ...

                    // Somewhere in our while loop

                    System.out.println("Play again? Yes/No:");
                    userInput = scan.nextLine();
                    if (userInput.equals("No")) break;  // Breaks out of the while loop and ends the program

            

One final comment: You will want to create one Scanner variable for the whole class. You should declare it just after the class declaration. Then it can be accessed by any of your methods. Be sure you initialize it (scan = new Scanner...) in the main method before calling your other methods.

                public class MadLibs {
                    static Scanner scan;

                ...

                publis static void main(String[] args) {
                    scan = new Scanner(System.in);
                    ...