πŸ’» PROJECT #2.1: MadLibs

Overview & Setup

In this project, you’ll be creating your own MadLibs game using Java! A MadLibs story is a short story where certain words are left out, and players fill in the blanks with their own words (like nouns, verbs, adjectives, etc.), making the story fun and creative. You will write a story, replace some words with variables, and ask the user to fill in those blanks by gathering input using a Scanner object.

  1. Go to the CS2 Project 2.1 assignment on Blackbaud and follow the provided GitHub Classroom link.

    πŸ“ Clicking the link generates a private repository for your project with the appropriate starter code. Note that projects are stored within the BWL-CS Organization, so you cannot access it from the β€œYour Repositories” page!

  2. Open the repository in a Codespace whenever you spend time working on the program, in class or at home.

    ⚠️ Always remember to commit changes after every coding session!

  3. When your project is complete, submit the link to your repository page (not the Codespace!) in the CS2 Project 2.1 assignment on Blackbaud.

Instructions & Requirements

PART A: Create a MadLibs Story

  1. Come up with a fun story to use for MadLibs. Yours should be long enough to include at least 10 variables. Choose nouns, verbs, adjectives, etc., that you want the user to fill in, and decide how many blanks you’ll need.

    Example: β€œToday, I went to the PLACE to VERB with my ADJECTIVE NOUN. It was a ADJECTIVE day, and we had lots of fun!”

  2. In your main method, declare variables for the parts of the story that the user will input. For each blank in the story, you’ll need a variable.

    Example: String adjective1;

  3. Use a Scanner object to take user input, using the code example below for help.
    // Add to TOP of program before the class:
    import java.util.Scanner;
    // In the main method:
    // Construct the Scanner - only do this once
    Scanner scan = new Scanner(System.in);
    // Take input for each variable!!!
    System.out.print("Enter an adjective: ");
    adjective1 = scan.nextLine();
    
  4. Use string concatenation to combine the user’s input with your story.

    Example: String story = "It was a " + adjective1 + " day";

  5. Print out your story!

    It may be easier to separate each sentence into its own variable, then use the appropriate method to print them out (either all together or on separate lines).

PART B: Mix it up with Methods

  1. Manipulate some of the string variables with String class methods such as .substring(). Use at least 3 String class methods total.
  2. Take numerical input (int and double) by using the Scanner class methods: scan.nextInt() or scan.nextDouble(), for at least 2 of the variables in your story.
  3. Before including those numbers in your story, run a Math class method on them to manipulate the numbers however you’d like. You should use at least 2 Math class methods total here, and possibly Math.random() to include a random number somewhere in the story.

Extensions

  • Add more variables to make your story longer and funnier.
  • Format the output to make it look like a paragraph using \n for new lines.
  • Include emojis to make the story visually interesting!

Reminders

  • Strings in Java are objects of the String class that hold sequences of characters.
  • String objects can be created by using string literals (String s = β€œhi”;) or by calling the String class constructor (String t = new String(β€œbye”);).
  • String objects can be concatenated using the + or += operator, resulting in a new String object.
  • Primitive values can be concatenated with a String object. This causes implicit conversion of the values to String objects.

Acknowledgement

Content on this page is adapted from .