Exercise: Variables and Data Types
Solve 28 small problems with variables, data types and expressions, and check every answer with tests.
Novice · C# · Updated
Time to put the whole chapter to work. In this exercise you solve 28 small problems: declaring variables, choosing data types, and calculating with expressions. Tests check your answers as you go.
Don't rush to finish. Take time over each variable's name and type, and keep your code tidy. Good habits are the real goal of this exercise, and they are easiest to build at the start.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-variables-and-data-types, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your bootcamp folder. Or clone it with Git.
The folder holds two projects:
VariablesExercise: the program you will write, inProgram.csVariablesExercise.Tests: the tests that check your answers. You don't need to change these.
Open VariablesExercise.slnx in Visual Studio, or open the folder in VS Code.
What to do
Each problem in Program.cs is a comment followed by a few empty lines. Write your code under the comment. Two examples at the top show the format:
// Example ii. Store the speed limit in a school zone.
int speedLimit = 20;
Console.WriteLine("ii) speedLimit: " + speedLimit);
ii) speedLimit: 20
For every problem:
- Declare the variables you need, with meaningful names and the right data type.
- Print the answer on one line: the problem number,
), the answer variable's name,:, then its value.
The problems come in four sections:
- Declaring variables (1 to 4): the names and types are given.
- Choosing data types (5 to 12): you pick the name and the type. Some values need looking up, such as the population of the world. Does it fit in an
int? - Backyard Basketball (13 to 18): word problems about Pete and Pat's games.
- Claire's Cookies (19 to 28): sharing cookies out, buying by the dozen, and working out prices.
Run it and check it
Run your program from the exercise folder:
dotnet run --project VariablesExercise
Then check your answers:
dotnet test
At first all 28 tests fail. That is expected: each problem you solve turns one test green. In Visual Studio, use Test > Run All Tests to see each test's result. A failing test's message tells you which problem it is and what it expected.
Tips
- No magic numbers. In "Pete made 7 shots and missed 3", give 7 and 3 names before you add them.
- Dividing two ints throws away the decimals. For a percentage, make one of the numbers a
decimalordoublefirst. - Money is a decimal. Write prices with an
m, such as12.99m. - Some things are bought whole. Claire only sells full dozens, so round up with
Math.Ceiling. - Stuck for more than 15 minutes? Move on to the next problem and come back later. The lessons in this chapter have an example for every idea you need.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-variables-and-data-types), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. If you downloaded the ZIP:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-variables-and-data-types.git git push -u origin mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-variables-and-data-types.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-variables-and-data-types, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
You have used every idea in this chapter to solve real problems, and the tests told you right away when an answer was right. You will use the same loop of write, run and test in every exercise from here on.