4.3A Zoo Assignment

4.3A Zoo Assignment
Add window controls to increase and decrease the birthing room temperature
Acceptance Criteria
Acceptance criteria
  • Window controls increase and decrease the birthing room temperature.
1.Add window controls to increase and decrease the birthing room temperature
Problem
Solution
4.3.1 How to Utilize the Windows Presentation Foundation
A Add temperature controls to the MainWindow.
1 Add a border, two buttons and a label to the MainWindow, as specified in the wireframe below
Temperature Controls Wireframe. Reference figure for add window controls to increase and decrease the birthing room temperature.
Temperature Controls Wireframe
2 The border has the following properties
  • Background: Red
  • Height: 100
  • Width: 20
  • BorderBrush: Black
  • BorderThickness: 1
3 The buttons have the following properties
  • Height: 23
  • Width: 23
4 The label has the following properties
  • Content: Temp
B Align controls
1 Set the horizontal and vertical alignments of the border, button, and label so that they align to the bottom right corner of the window.
  • HorizontalAlignment: Right
  • VerticalAlignment: Bottom
C Check Your Work
1 Stretch the window in design view or while running.
2 Ensure that the controls remain positioned in the bottom right corner of the window.
WPF Controls. Reference figure for add window controls to increase and decrease the birthing room temperature.
WPF Controls
D Add a setter to the zoo's BirthingRoomTemperature property.
Class Diagram: BirthingRoomTemperature Property. Reference figure for add window controls to increase and decrease the birthing room temperature.
Class Diagram: BirthingRoomTemperature Property
4.3.2 How to Read Composition in Class Diagrams
E Add click events to the increase and decrease buttons.
1 Add a click event for the increase button.
Subtasks
  • A. In it, increment the birthing room temperature by 1.
  • B. Set the height of the border to twice the birthing room temperature.
  • C. Set the label's content to the birthing room temperature.
2 Add a click event for the decrease button to lower the temperature of the birthing room.
F Check Your Work
1 Run the WPF application.
2 Ensure that initially the temperature label is not set and the border height does not correspond to the birthing room's temperature.
3 Click the increase temperature button.
4 Ensure that the border height jumps to correspond with the birthing room's temperature.
G In the MainWindow's CreateComoZoo method, after instantiating the Zoo, set the height of the border to twice the BirthingRoomTemperature.

Set the label's Content to the BirthingRoomTemperature.

H Check Your Work
1 Run the WPF application.
2 Observe that the temperature label is now set and the border height corresponds to the birthing room's temperature.
3 Increase the temperature by 10 degrees.
I Make both buttons repeat buttons by typing Repeat before Button below the design view in the XAML view in MainWindow.xaml, as in the code below
<RepeatButton ... />
RepeatButton (WPF 2000 Things)
J Check Your Work
1 Run the WPF application.
2 Click and hold the increase temperature button.
3 The height of the border and the label's content should go up by 1 continuously.
K Remove duplicate code which sets the properties of the temperature controls.
4.3.3 Why we Use the DRY Principle (Don't Repeat Yourself)
1 Refactor the duplicated code into a new method named ConfigureBirthingRoomControls per the class diagram below.
Class Diagram: Configure Birthing Room Controls. Reference figure for add window controls to increase and decrease the birthing room temperature.
Class Diagram: Configure Birthing Room Controls
2 Remove the code setting the temperature controls from all three locations and call the ConfigureBirthingRoomControls method instead.
L Format the temperature display.

The temperature currently displays as a whole number (e.g. 77). We want to display the temperature as 77.0 °F. Use string.Format to accomplish this task.

4.3.4 How to Use String.Format

M. Check Your Work:

1 Run the WPF application.
2 Ensure that the birthing room temperature is displayed in the window as 77.0 °F.
M Change the color of the border to reflect the temperature.
4.3.5 Why we Use Static Fields
1 Make the BirthingRoom's minTemperature and maxTemperature public and static.
Class Diagram: BirthingRoom Min and Max Temperature. Reference figure for add window controls to increase and decrease the birthing room temperature.
Class Diagram: BirthingRoom Min and Max Temperature
2 Update all references to MinTemperature and MaxTemperature within the BirthingRoom class to reference the now static fields.
3 Add the following code to the end of the ConfigureBirthingRoomControls method.
double colorLevel = ((this.comoZoo.BirthingRoomTemperature - BirthingRoom.MinTemperature) * 255) / (BirthingRoom.MaxTemperature - BirthingRoom.MinTemperature);

this.temperatureBorder.Background = new SolidColorBrush(Color.FromRgb(
    Convert.ToByte(colorLevel),
    Convert.ToByte(255 - colorLevel),
    Convert.ToByte(255 - colorLevel)));
N Check Your Work
1 Run the WPF application.
2 Click and hold the decrease temperature button until the temperature reaches 35 degrees.
3 Ensure that the border's color changes to blue.
Temperature Controls. Reference figure for add window controls to increase and decrease the birthing room temperature.
Temperature Controls
Add a console command to change the birthing room temperature
Acceptance Criteria
Acceptance criteria
  • The console temperature command accepts a temperature parameter.
2.Add a console command to change the birthing room temperature
Problem
Solution
A After the instantiation of the command variable, create a string array variable called commandWords and set it to result of splitting the command, as shown in the code below
4.4.6 How to Use String Arrays
string[] commandWords = command.Split();
B Instead of switching on the command variable, switch on the first item in the commandWords array (commandWords[0]).

The first item in the array represents the main command the user wants to run.

C Add the code for the "temp" command.
4.4.7 How to Parse a Value 4.4.8 How to Use String Interpolation
1 Add a switch case for "temp".
Command Diagram: Temp. Reference figure for add a console command to change the birthing room temperature.
Command Diagram: Temp
2 In the case statement, store the current temperature in a variable.
3 Set the birthing room's temperature to the value of the second item in the commandWords array.

Parse the string as a double, as shown in the code below:

Double.Parse(commandWords[1]);
4 Use string interpolation to write a line to the console stating the previous temperature.

It should look like the following when it is written to the console:

Previous temperature: 70.0 °F.
5 Like the previous step, use string interpolation to write another line to the console stating the new temperature.
New temperature: 77.0 °F.
D In the new command, set the zoo's birthing room temperature to 77 after creating the zoo.
E Add a line for the temp command to the help command output.

Be sure to include directions for how to use the parameter. It should look like the following:

TEMP: Sets the birthing room temperature
F Check Your Work
1 Run the console application.
2 Type new. Run the temp command by typing temp 70. The console output should look similar to the image below
Console: Temperature Output. Reference figure for add a console command to change the birthing room temperature.
Console: Temperature Output
Provide user feedback for the birthing room temperature if designated value is out of range.
Acceptance Criteria
Acceptance criteria
  • Both front ends report requests for temperatures outside the allowed range.
3.Provide user feedback for the birthing room temperature if designated value is out of range.
Problem
Solution
A Add validation to the console's temperature command.
1 Comment out the if-statement that is being used as validation in the temperature property in the BirthingRoom class.

You must do this in order to get this step to work. We will be adding this validation back in at a later step.

2 In the temp command, check to see if the new temperature is at least 35 and no greater than 95.

This means that the temperature should only be in between those two temperatures.

3 Move the code setting the temperature and writing the previous and new temperatures inside the if block.
4 In an else block, write a line to the console telling the user that the temperature must be between 35 and 95 degrees.

It should look like the following:

The birthing room temperature must be between 35 and 95 degrees.
B Add validation to the WPF application's temperature buttons.
1 Add the same temperature check from the console to both click events for the increase and decrease temperature buttons.
2 Move the existing code in each click event inside the if block.
3 Add an else block in each click event.

Show a message box in each block telling the user that the birthing room temperature must be between 35 and 95 degrees.

C Check Your Work
1 Run the console application.
Subtasks
  • A. Type new. Try to change the temperature to 100. The temperature error message should appear.
  • B. Try to change the temperature to 30. The temperature error message should appear. The console output should look similar to the image below:
Console: Error Message. Reference figure for provide user feedback for the birthing room temperature if designated value is out of range.
Console: Error Message
2 Run the WPF application.
Subtasks
  • A. Click and hold the increase temperature button. Once the temperature gets above 95 degrees, a message box should pop up with the validation message.
  • B. Click and hold the decrease temperature button. Once the temperature gets below 35 degrees, a message box should pop up with the validation message.
Throw exceptions for the temperature in the birthing room.
Acceptance Criteria
Acceptance criteria
  • Temperature validation resides in the BirthingRoom setter and invalid values throw exceptions at this stage.
4.Throw exceptions for the temperature in the birthing room.
4.3.9 How to Throw an Exception
Problem
Solution
A Uncomment out the if-statment that was commented out in the previous step.
B Throw exceptions in temperature setter.
1 In the setter of the Temperature property, if the value is less than the minimum temperature, throw a new Exception and pass an error message as shown in the code below
throw new Exception("The temperature must be above 35 degrees.");
2 If the value is greater than the maximum temperature (else if), throw a new Exception and pass an appropriate error message.
3 Otherwise (else), if the value is within the required range, then set the temperature field to the value.
C Remove the if-else statements from the increase and decrease temperature button clicks and the console temperature command.
D Check Your Work
1 Run the WPF application.
Subtasks
  • A. Click and hold the increase temperature button. When the temperature gets above 95 degrees, the application should crash with the exception thrown in the birthing room's temperature property.
2 Run the console application.
Subtasks
  • A. Type new. Attempt to change the temperature to 100. The application should crash with the exception thrown in the birthing room. Attempt to change the temperature to 30. The application should crash again.
Catch thrown exceptions
Acceptance Criteria
Acceptance criteria
  • The front ends catch temperature exceptions and display understandable error messages.
5.Catch thrown exceptions
Problem
Solution
4.3.10 How to Catch an Exception
A Add a try...catch block around the code in the temperature console command.

Catch Exceptions and write the exception's message to the console, as shown in the code below:

case "temp":
    try
    {
      // existing code here
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    break;
B Add try...catch blocks to both the increase and decrease temperature button clicks.

Move the existing code in each inside the try block. Show a message box with the exception's message inside the catch block.

C Check Your Work
1 Run the console application.
Subtasks
  • A. Attempt to change the temperature to 100. A user-friendly error message should appear. Attempt to change the temperature to 30. An error message should appear. Change the temperature to 70. The change should be successful.
2 Run the WPF application.
Subtasks
  • A. Attempt to increase the temperature to above 95 degrees. A message box with a user-friendly error message should appear. Attempt to decrease the temperature to below 35 degrees. A message box should appear. Changing the temperature otherwise should work.
Catch multiple exceptions in one try...catch block
Acceptance Criteria
Acceptance criteria
  • Separate catch blocks report distinct input and temperature errors.
6.Catch multiple exceptions in one try...catch block
Problem
Solution
4.3.11 How to Catch Multiple Exceptions
A In the birthing room's temperature setter, throw an ArgumentOutOfRangeException instead of an Exception for both thrown exceptions.

Use the constructor that takes a parameter name and message, passing in the following values (or similar ones):

  • paramName: "temperature"
  • message: "The temperature must be above 35 degrees."
B In the catch block of the console's temperature command and the increase and decrease temperature button clicks, catch an ArgumentOutOfRangeException instead of an Exception, as shown in the code below
catchcatch (ArgumentOutOfRangeException ex)
{
)
{
    // code here
}}
C Add another catch block to the console try...catch block that catches a FormatException, as shown in the code below.

Display a message to the user in the console stating that a number must be submitted as a parameter in order to change the temperature. No variable is needed for the FormatException because it would not be used.

try
{
    // code here
}
catch (ArgumentOutOfRangeException ex)
{
    // code here
}
catch (FormatException)
{
    // code here
}
D Add another catch block to the console try...catch blocks that catches an IndexOutOfRangeException, as shown in the code below.

Display a message to the user stating that a parameter must be entered in order for the temperature command to work. No variable is needed for the IndexOutOfRangeException because it would not be used.

try
{
    // code here
}
catch (ArgumentOutOfRangeException ex)
{
    // code here
}
catch (FormatException)
{
    // code here
}
catch (IndexOutOfRangeException)
{
   // code here
}
E Add another catch block to the console try...catch blocks that catches a NullReferenceException.

Display a message to the user stating that a new zoo must be created before they can change the temperature.

F Check Your Work
1 Run the console application.
Subtasks
  • A. Attempt to change the temperature without entering the 'new' command first. Ensure that the NullReferenceException is caught and the message about creating a new Zoo is shown to the user.
  • B. Enter the 'new' command. Attempt to change the temperature by typing in 'temp fifty'. The FormatException should be caught and a message stating that a number must be entered as a parameter should appear.
  • C. Attempt to change the temperature to 100. The error message specified when throwing the ArgumentOutOfRangeException should appear. Change the temperature to 70. The change should be successful.
  • D. Type in just 'temp' to the console. The error message specified when an IndexOutOfRange Exception is caught should appear.
  • E. Change the temperature to 70. The change should be successful.
Allow zoo creation code to be shared by WPF and console applications
Acceptance Criteria
Acceptance criteria
  • Both front ends use the shared static zoo-creation method.
7.Allow zoo creation code to be shared by WPF and console applications
Problem
Solution
4.3.12 How to Use Static Methods
A Refactor the existing code in the CreateComoZoo method in the MainWindow.
1 Select all of the code inside the method, right-click the selection, go to Quick Actions, and choose Extract Method.

Name the method NewZoo.

2 Change the newly-created method so that it is static and it returns a Zoo.
3 Cut and paste the NewZoo method from the MainWindow.xaml.cs into the Zoo class.
4 Instead of referring to the comoZoo field throughout the method, instantiate a zoo variable and refer to that.

Then return the variable.

5 Remove the call to ConfigureBirthingRoomControls in the NewZoo method.
B Generate a zoo when the WPF window loads
4.3.13 How to Use Window Events
1 Remove the CreateComoZoo method and any references to it from the MainWindow.
2 Give the WPF window an event that fires when it loads.
Subtasks
  • A. Navigate to the MainWindow.xaml and select the window.
  • B. In the properties window, give the Window the Name: window.
Subtasks
  • C. While the Window is selected, click the Events section of the Properties window.
  • D. Scroll down to the Loaded event and double click it. This will add the window_Loaded event to the MainWindow.xaml.cs.
Class Diagram: window_Loaded. Reference figure for allow zoo creation code to be shared by wpf and console applications.
Class Diagram: window_Loaded
Subtasks
  • E. Inside the loaded event, call the static NewZoo method on the Zoo class.
this.comoZoo = Zoo.NewZoo();
Subtasks
  • F. Call the ConfigureBirthingRoomControls method.
C Generate a zoo when the Console window loads.
1 In the Main method of Program.cs before the while loop, assign the zoo field to the result of calling the static NewZoo method.
2 Change the "new" command to be the "restart" command.

Instead of setting the variable to a new zoo, set it to the result of calling the static NewZoo method.

3 Update the help command by changing the entry for the new command to the one below.
RESTART: Creates a new zoo.
4 Remove the case for the NullReferenceException in the 'temp' command.

We no longer need it since we've ensured a new zoo will always be instantiated.

D Check Your Work
1 Run the console application.
2 Change the temperature to 70.

The command should run successfully without first running the 'new' or 'restart' command.

3 Run the WPF application. Ensure that the temperature controls are working as intended.
Add validation to the admission of guests
Acceptance Criteria
Acceptance criteria
  • Guest admission reports invalid tickets instead of failing silently.
8.Add validation to the admission of guests
Problem
Solution
A In the AddGuest method in the zoo, throw a NullReferenceException if the ticket is null.

Pass in a message stating that the guest couldn't be admitted because they did not have a ticket.

B In the admitGuestButton_Click, catch the NullReferenceException. Show the exception's message in the message box.
C Check Your Work
1 Run the WPF application.
2 Click the Admit guest button four times.

The guest should be added the first three times, but the message about a missing ticket should pop up the fourth time.

Demonstrate your work
9.Record a video of your applications showing
A Your WPF Application
1 Hold the increaseTemperatureButton until it reaches 95.0 °F
Subtasks
  • A. Click it one more time
  • B. An message box with an error message should appear
2 Hold the decreaseTemperatureButton until it reaches 35.0 °F
Subtasks
  • A. Click it one more time
  • B. An message box with an error message should appear
3 Click the Admit guest button 4 times
Subtasks
  • A. A message about a missing ticket should pop up the 4th time
B Your Console Application
1 The 'temp fifty' command should write an error message specified when a FormatException is caught
2 The 'temp 100' command should write an error message specified when an ArgumentOutOfRangeException is caught
3 The 'temp' command should write an error message specified when an IndexOutOfRangeException is caught
4 The 'temp 45' command should be successful and a message about the previous and new temperature should be written
5 The 'restart' command should create a new zoo
6 The 'help' command should include the 'restart' and 'temp' command and no longer have the 'new' command
Prepare and submit your work
10.Submission Steps
A Ensure that your application has no compiler errors or warnings
B Ensure that your code is StyleCop compliant
C Remove all bin and obj folders from your solution
D Zip your solution
E Submit your zipped solution to the Feedback System
F Submit your Feedback System results link to Canvas
G Submit your video recording to your f-channel
Source credit

© 2026 Northcentral Technical College