Exercise: Classes
Write six classes from a specification (Rectangle, Circle, MathTest, Printer, Elevator and Airplane), choosing each property's access, and let the tests check them.
Apprentice · C# · OOP · Updated
In this exercise you don't fill in methods: you write whole classes, starting from an empty file, the way a developer works from a specification. For each class you'll decide on properties, constructors and methods, and choose who may change what.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-classes, 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.
ClassesExerciseis where your classes go. It starts almost empty.ClassesExercise.Testschecks each class. You don't need to change it.README.mdis the specification: a table for each class.
Open ClassesExercise.slnx in Visual Studio.
Reading the specification
Each class has a table of properties. The Set column tells you who may change each one:
| Set | Means | Write it as |
|---|---|---|
| X | anyone can set it | public int Width { get; set; } |
| private | only the class can set it | public int Paper { get; private set; } |
| (empty) | calculated from other properties | public int Area => Width * Height; |
For example, here is the start of the Rectangle class, with one property of each kind:
namespace ClassesExercise;
public class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public int Area => Width * Height;
}
Work one test at a time
- Run all the tests (Test > Run All Tests). Every one fails: no class exists yet.
- Add a class: right-click the
ClassesExerciseproject, Add > Class..., and name it after the class. Start the file withnamespace ClassesExercise;and make the classpublic. - Write just enough to pass one test. Run the tests again.
- Repeat until the class's tests are green, then move on to the next class.
When a test fails, its message says what's missing, such as Rectangle needs a public constructor that takes int, int, or Printer.Paper should have a private set.
Working this way, one small test at a time, has a name: test-driven development. You'll write tests of your own in Module 2.
The classes
| Class | What it practices |
|---|---|
Rectangle |
properties anyone can set, a calculated property, two constructors |
Circle |
calculated properties with Math.PI |
MathTest |
private set, and a calculated letter grade |
Printer |
methods that change state under rules |
Elevator |
state that only methods may change, and rules that refuse a move |
Airplane (challenge) |
putting it all together |
Hints
- Integer division: in
MathTest,Score / PossiblePointswithints is 0 for any score below a perfect score. Multiply by 100 first. - Starting values: a new
Elevatorstarts on floor 1. Set it in the constructor, or give the property a starting value:{ get; private set; } = 1;. - Refuse, don't break: when
GoUpcan't go, orReserveSeatscan't book, the object stays exactly as it was. - Constants:
private const int MaxPaper = 500;names a number the class uses.
When you're done
Every test is green. Look at each class: could any code outside the class put it into a state that makes no sense? If so, which property's set should be private?
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-classes), 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-classes.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-classes.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-classes, 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
Writing a class from a specification, deciding what it knows, what it does and who may change it, is the everyday work of a C# developer.