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