The Challenge4:09 with James Churchill
In this video, we'll explain the challenge that you'll be working to complete.
This practice session assumes that you've completed stage 4 of the C# Basics course. If you haven't done that yet, go check it out, then come back to this workshop.
This practice session also builds upon the skills that are covered in these two practice sessions that cover stages 1 and 2 of the C# Basics course.
If you haven't completed these practice sessions yet, you might consider doing that first before attempting this practice session.
If you get stuck on any of the following topics or simply need a refresher, click on a topic in list below to view the associated video in the C# Basics course.
[MUSIC] 0:00 Hi everyone, this is James. 0:04 In this C# practice session, you'll get a chance to practice validating input and 0:06 catching exceptions using C#. 0:11 It reinforces what you learned in stage four of the C# basics course. 0:13 If you find this practice session too challenging to complete, 0:19 you might need to review that course. 0:22 See the teacher's notes for a link. 0:24 I've attached a workspace to this video. 0:26 Go ahead and open the workspace or download the project files 0:28 if you want to use an external editor or an IDE like Visual Studio. 0:33 If you open the program.cs file, 0:37 you'll see that we've provided you with some code. 0:40 This code prompts the user for 0:44 a number, calculates the square of the provided number, and displays the result. 0:46 While this is a functional program, 0:51 there are some improvements that we'd like to make. 0:53 Each improvement is represented by a TODO code comment. 0:56 To complete this practice session, you'll read through the TODO code comments and 1:00 complete each one. 1:04 Let's see the completed program in action. 1:06 Remember, to compile a C# file, we use the mcs command. 1:10 And to run a C# program, we use the mono command. 1:19 See the teacher's notes if you need a refresher on either of those commands. 1:26 Okay, the program prompts me for a number, I'll enter 2, 1:30 then displays the square of my provided number. 1:34 2 multiplied by itself is equal to 4. 1:37 We can then enter another number or 1:41 type the text quit to terminate execution of the program. 1:43 Let's clear the console by typing clear, Enter. 1:47 Then run the program again. 1:51 Notice that if we don't enter a number, the program will crash. 1:54 Or if we enter a fractional number like 4.5, the program again will crash. 2:02 And to quit the program, we need to type the text quit in all lowercase characters. 2:13 If we do any variation of that, 2:18 like in all uppercase characters, the program will crash. 2:20 As you can see, we've got some work to do. 2:25 Let's review each of the TODO comments in the program.cs file. 2:29 Our first task is to switch to using continue and 2:33 break to control the flow of execution through our program. 2:36 That will allow us to get rid of the keepGoing variable that you see defined 2:40 right here. 2:43 For our next task, we just need to update this prompt from Enter a number 2:44 to Enter a number between 1 and 1,000. 2:49 For our next task, we wanna force the user's provided value 2:52 in the entry variable to lowercase letters. 2:55 That will allow the user to user to type the text quit however they want. 2:59 For this task, 3:02 we need to add a try/catch statement to catch FormatException exceptions. 3:03 This will keep the app from crashing if the user 3:08 enters something that can't be parsed to a number. 3:11 For this task, we want to allow the user to enter a fractional value. 3:14 So instead of just whole numbers, like 1, 2, or 3, 3:19 we want them to be able to enter 2.4, for instance. 3:22 For the next couple of tasks, we wanna validate the user's input. 3:26 For the first one, if the user enters a value less than or equal to 0, 3:30 we wanna display the message, Please enter a number greater than 0. 3:34 And for the second validation, if the user enters a value greater than 1,000, 3:39 we wanna display the message, Please enter a number less than or equal to 1,000. 3:43 And that's it. 3:49 If you get stuck at any point, see the teacher's notes for hints and 3:50 links to videos that cover what you need to know in order to complete the session. 3:54 And if you aren't able to fully complete the program, don't worry, 3:58 I'll show you how I completed the TODOs in the next video. 4:02 Good luck and we'll see you in just a bit. 4:06
You need to sign up for Treehouse in order to download course files.Sign up