For each lab program you develop, make sure to include the following header – replace the dots with your section number, semester, your full name, your instructor’s name, and lab number: Class: CSE 1321L Section: Term: Instructor: Name: Lab#: Make sure to put the correct comment characters before the above lines in each file. C# & Java use // for comments. Lab2A: MadLibs. For this assignment, we’re going to code a MadLib. Your program should ask the user for two names, a verb and an adverb. These responses will get plugged into a pre- written sentence. An example output is shown below. User input is denoted in bold. Both the class and filename should be called Lab2A (.java, .cs, .cpp). Sample run: Enter a name: Tiffany Enter another name: Bubba Enter a verb: eat Enter an adverb: wickedly Once upon a time, there was a person named Tiffany who had a child named Bubba. This child would eat wickedly while singing to strangers.
The Correct Answer and Explanation is:
Here is the correctly formatted C# version of Lab2A: MadLibs, including the required header and sample implementation:
csharpCopyEdit// Class: CSE 1321L
// Section: [Your Section Number]
// Term: [Your Semester]
// Instructor: [Your Instructor's Name]
// Name: [Your Full Name]
// Lab#: Lab2A
using System;
class Lab2A
{
static void Main(string[] args)
{
Console.Write("Enter a name: ");
string name1 = Console.ReadLine();
Console.Write("Enter another name: ");
string name2 = Console.ReadLine();
Console.Write("Enter a verb: ");
string verb = Console.ReadLine();
Console.Write("Enter an adverb: ");
string adverb = Console.ReadLine();
Console.WriteLine("Once upon a time, there was a person named " + name1 +
" who had a child named " + name2 +
". This child would " + verb + " " + adverb +
" while singing to strangers.");
}
}
Explanation
This lab assignment requires the creation of a simple MadLibs-style program in C#. The program begins by displaying four prompts to the user, asking for two names, one verb, and one adverb. These inputs are then stored in variables and used to complete a fixed sentence with dynamic content.
The use of Console.Write
allows the user to input their responses on the same line as the prompt, making the interface cleaner. Each input is read using Console.ReadLine()
and stored in variables name1
, name2
, verb
, and adverb
.
The final output sentence is constructed by concatenating the static story text with the user inputs. This demonstrates how user-provided data can be integrated into prewritten content. The goal is to make the output funny or creative, depending on the words the user provides.
In terms of syntax, the header is formatted using the //
comment symbol required in C#. This ensures that metadata such as course number, section, and student name are well documented at the top of the code, which is a common requirement in academic settings.
The program illustrates several core programming concepts. These include console input and output, variable declaration, string manipulation, and basic program structure with the Main
method. For new programmers, this lab is a useful exercise in understanding how to collect input and dynamically produce output based on user data. It sets the foundation for later work with functions, conditionals, and loops.
By encouraging creativity through word choice, this lab also makes the learning process more engaging and memorable.
