0.3 First Day Assignment

Overview
1.Overview
A Decisions and Debugging

You will continue working in the same First Day project you used in 0.2. This week, you will add one button, follow a simple true/false decision, use the debugger to watch the program choose a path, and then connect that decision to an It is raining checkbox.

You do not need previous programming experience. Follow each step in order and use the checkboxes to keep track of where you are.

B Task List
  • Continue from your 0.2 First Day Solution
  • Find MainWindow.xaml and MainWindow.xaml.cs
  • Add the Choose Clothing button
  • Trace a simple if statement
  • Add else and trace both paths
  • Add the rain checkbox
  • Submit to the Feedback System
3.Continue From Your 0.2 First Day Solution
A Rename the workspace
B Close Visual Studio if your 0.2 project is open
C Open File Explorer and locate your completed 0.2 First Day Solution folder. If you do not already have it on this computer, first download the completed 0.2 First Day Solution and extract it
D Right-click the workspace folder and choose Rename
E Change only the module number. Example: LastName 0.2 becomes LastName 0.3
F Open the renamed LastName 0.3 folder
G Double-click the Visual Studio solution file, the file whose name ends in .sln

Important: Do not rename the project files or classes. For this assignment, you are only renaming the outer workspace folder from 0.2 to 0.3.

4.Find MainWindow.xaml and MainWindow.xaml.cs
  • MainWindow.xaml describes what appears in the window.
  • MainWindow.xaml.cs contains the C# code that runs when the user interacts with the window.
A Open Solution Explorer
B Look for Solution Explorer, usually on the right side of Visual Studio
C If you do not see it, choose View → Solution Explorer from the Visual Studio menu
D In Solution Explorer, expand the FirstDayScenario project if it is collapsed
E Open the XAML file
F Double-click MainWindow.xaml
G If Visual Studio shows a designer and XAML together, locate the XAML text area. This is where the window controls are described
H Open the C# code-behind file
I In Solution Explorer, expand the arrow next to MainWindow.xaml if needed
J Double-click MainWindow.xaml.cs
K Scroll through the file and locate the existing button handlers such as NewStudentButton_Click, HaveBreakfastButton_Click, and FillBackpackButton_Click

A button handler is a method that runs when a button is clicked. You will add the button in XAML first, then double-click it in the designer so Visual Studio creates the handler for you.

5.Add the Choose Clothing Button
A Add the XAML button
B In Solution Explorer, double-click MainWindow.xaml
C If Visual Studio opens the XAML text view, find the existing StackPanel that contains the New Student, Have Breakfast, and Fill Backpack buttons
D Find the XAML for the existing fillBackpackButton
E Click at the end of that button's XAML, press Enter, and paste the new button directly below it
<Button x:Name="ChooseClothingButton" Content="Choose Clothing" Width="300" Margin="5" />
F Create the C# click handler
G In MainWindow.xaml, switch to the visual designer if it is not already visible
H Find the new Choose Clothing button in the window preview
I Double-click the Choose Clothing button
J Visual Studio should open MainWindow.xaml.cs and create a new click-handler method for that button
K Confirm the new method is named ChooseClothingButton_Click

The new handler should look similar to this:

private void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
}

The button in MainWindow.xaml is now connected to the ChooseClothingButton_Click method in MainWindow.xaml.cs. You will add the decision logic inside this method in the next step.

L Check the button
M Choose Build → Build Solution. Correct any typing or casing errors before continuing
N Press F5 to run the application
O Verify that the Choose Clothing button appears in the window
P Click the button. Nothing needs to happen yet because the handler is still empty
Q Close the running application
6.Trace a Simple if Statement
A Start with true
B Open MainWindow.xaml.cs
C Find the empty ChooseClothingButton_Click handler you added
D Replace that empty handler with the version below
private void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
    bool isRaining = true;

    if (isRaining)
    {
        this.textOutput.Text = "Wear a rain jacket.";
    }
}
E Set a breakpoint
F Find the line if (isRaining)
G Move your mouse to the gray margin on the far left of that line in the code editor
H Click once. A red breakpoint marker should appear next to the line
I Press F5 to start the application
J Click Choose Clothing. Visual Studio should pause on the breakpoint
K Watch the true path
L When Visual Studio pauses, hover your mouse over isRaining. Confirm that its value is true
M Press F10 once. Watch the yellow highlight move into the if block
N Continue pressing F10 until the line that displays Wear a rain jacket. has executed
O Press F5 to continue running the application
P Verify that the output area displays Wear a rain jacket
Q Stop debugging or close the running application

What happened? Because isRaining was true, the statements inside the if block ran.

R Change the value to false
S In MainWindow.xaml.cs, change only bool isRaining = true; to bool isRaining = false;
T Leave the breakpoint on if (isRaining)
U Press F5, then click Choose Clothing
V Hover over isRaining and confirm that it is now false
W Press F10. Notice that the program skips the statements inside the if block
X Continue the program and observe that no new clothing message is produced by this handler
Y Stop debugging

This is an important result: an if block runs only when its condition is true. Right now there is no instruction telling the program what to do when the condition is false.

7.Add else and Trace Both Paths
A Add the else block
B Open MainWindow.xaml.cs and find the ChooseClothingButton_Click handler
C Keep bool isRaining = false; for the first test
D Click immediately after the closing brace of the if block
E Paste the else block below
else
{
    this.textOutput.Text = "Wear a hoodie.";
}

Your complete handler should now look like this:

private void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
    bool isRaining = false;

    if (isRaining)
    {
        this.textOutput.Text = "Wear a rain jacket.";
    }
    else
    {
        this.textOutput.Text = "Wear a hoodie.";
    }
}
F Debug the false path
G Keep the breakpoint on if (isRaining)
H Press F5 and click Choose Clothing
I Confirm isRaining is false
J Press F10 and watch the program skip the first block and move into else
K Continue until the output displays Wear a hoodie
L Stop debugging
M Debug the true path again
N Change bool isRaining = false; back to bool isRaining = true;
O Press F5, click Choose Clothing, and stop at the same breakpoint
P Use F10 to watch the program enter the first block instead of else
Q Verify that the output displays Wear a rain jacket
R Stop debugging

What to notice: The same if/else can follow either path. The value of the condition determines which path runs.

8.Add the It is Raining Checkbox
A Add the checkbox in MainWindow.xaml
B In Solution Explorer, double-click MainWindow.xaml
C Find the ChooseClothingButton line you added earlier
D Click at the beginning of that line and press Enter so there is a blank line directly above the button
E Paste the checkbox XAML below on that blank line
<CheckBox x:Name="rainCheckBox" Content="It is raining" Width="300" Margin="5" />

The checkbox is named rainCheckBox. C# will use that exact name to check whether the box is selected.

F Connect the checkbox to the Boolean
G Open MainWindow.xaml.cs
H Find the line bool isRaining = true; inside ChooseClothingButton_Click
I Replace only that line with the line below
bool isRaining = this.rainCheckBox.IsChecked == true;

Your finished handler should now be:

private void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
    bool isRaining = this.rainCheckBox.IsChecked == true;

    if (isRaining)
    {
        this.textOutput.Text = "Wear a rain jacket.";
    }
    else
    {
        this.textOutput.Text = "Wear a hoodie.";
    }
}
J Debug with the checkbox unchecked
K Build the solution
L Keep the breakpoint on if (isRaining)
M Press F5
N Leave It is raining unchecked and click Choose Clothing
O At the breakpoint, hover over isRaining. It should be false
P Use F10 to follow the else path
Q Verify the application displays Wear a hoodie
R Debug with the checkbox checked
S Continue or restart the application as needed
T Check It is raining
U Click Choose Clothing
V At the breakpoint, hover over isRaining. It should be true
W Use F10 to follow the first path
X Verify the application displays Wear a rain jacket
Y Stop debugging when both paths work

Final flow: checkbox state → isRaining → if/else → clothing message

9.Submit to the Feedback System
A Final check
B Choose Build → Build Solution and make sure the project builds
C Run with It is raining unchecked and verify Wear a hoodie
D Run with It is raining checked and verify Wear a rain jacket
E Verify the features you completed in 0.2 still work
F Close the running application and close Visual Studio
G Create the ZIP file
H In File Explorer, locate your entire LastName 0.3 workspace folder
I Right-click the LastName 0.3 folder and choose Compress to ZIP file or Send to → Compressed (zipped) folder, depending on your version of Windows
J Open the ZIP file and confirm it contains your Visual Studio solution file and the FirstDayScenario project folder
K Upload to FBS
L Open the course Feedback System (FBS)
M Choose the First Day 0.3 assignment
N Upload the ZIP file you just created
O Review the feedback. If FBS identifies an in-scope problem, correct it in Visual Studio, create a new ZIP, and submit again

You're finished when your 0.3 workspace has been submitted successfully to FBS.