Exercise: Methods and Conditions
Write 13 methods for three small businesses, using parameters, return values and conditions, and check each one with tests.
Apprentice · C# · Updated
Three small businesses need your help: a backyard basketball league, Claire's cookie shop, and Elliot's lawn-care business. Each needs a few calculations, and each calculation is a method for you to write. Tests check every method as you go.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-methods-and-conditions, 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:
MethodsExercise: your code. Each business is a class of methods in its own file:BackyardBasketball.cs,ClairesCookies.csandElliotsYardCare.cs.MethodsExercise.Tests: the tests that check your methods. You don't need to change these.
Open MethodsExercise.slnx in Visual Studio, or open the folder in VS Code.
What to do
Every method is already declared, with its parameters and return type, and returns 0 for now. The comment above it says what it should return, with examples:
/*
* Return the percentage of games won, as a whole number.
*
* CalculateWinningPercentage(7, 3) -> 70
* CalculateWinningPercentage(5, 5) -> 50
*/
public static int CalculateWinningPercentage(int gamesWon, int gamesLost)
{
return 0;
}
Replace return 0; with your own code. The first line of each file, namespace MethodsExercise;, groups the project's code together; leave it as it is.
Check your work
In Visual Studio, choose Test > Run All Tests. Or, in Git Bash, from the folder with the .slnx file:
dotnet test
At first almost every test fails. Each method you finish turns its tests green. When a test fails, its message says which values it used and what it expected, for example A team with 5 wins and 10 losses has won 33% of its games.
You can also try a method in Program.cs and run the project with Ctrl+F5.
Hints
- Work out an example by hand first. If you can't get 33 from 5 wins and 10 losses with a calculator, code won't help yet.
- Integer division drops the fraction.
5 * 100 / 15is33. Multiply before you divide. Math.Ceilingrounds up andMath.Round(value, 2)rounds to cents. You met both in Arithmetic and Expressions in C#.- Methods can call methods.
CalculateProfitneeds the cost, andCalculateCostalready works it out. - Write your own helper. Three of Claire's methods add the same sales tax. A small method of your own, such as
AddTax(decimal subtotal), keeps that rule in one place. - Pick the right decision. A true/false choice suits
?:orif; a list of exact names suits a switch; ranges suitifandelse if.
When you're done
Every test is green. Read your code again, as if someone else wrote it: are the names clear? Is any calculation written twice? Tidy it up and run the tests again; they'll tell you if you broke anything.
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-methods-and-conditions), 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-methods-and-conditions.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-methods-and-conditions.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-methods-and-conditions, 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
These are the same steps every real feature takes: a method with a clear name, the values it needs, a decision or two, and tests that prove it works.