π» 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.
- 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!
- 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! - 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
- 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!β
- 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;
- 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();
- Use string concatenation to combine the userβs input with your story.
Example:
String story = "It was a " + adjective1 + " day";
- 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
- Manipulate some of the string variables with
String
class methods such as.substring()
. Use at least 3 String class methods total. - Take numerical input (
int
anddouble
) by using theScanner
class methods:scan.nextInt()
orscan.nextDouble()
, for at least 2 of the variables in your story. - 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 possiblyMath.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.