A property in C# is a member of a class that provides access to data within that class. Callers can set or retrieve (get) the data. Properties can be either auto-implemented or have a backing field. They comprise a set accessor and/or a get accessor. In some other languages a "mutator" is roughly equivalent to a set accessor and an "accessor" is roughly equivalent to a get accessor although the composition of the syntax is completely different.
When setting a property the input value can be validated, formatted or otherwise manipulated and in fact any programmatic operation accessible to code in the class can be executed.
Similarly when retrieving a property data can be calculated or formatted and again any programmatic operation available to the class can be executed.
Properties have access modifiers (public
, private
etc.) in the same way as other
class members but the set accessor may have an access level independent of the retrieve (get)
accessor and vice versa. A property doesn't have to have both accessors, it can have just one (either get or set).
The basic syntax to express properties can take two forms:
private int myField;
public int MyProperty
{
get { return myField; }
set { myField = value; }
}
public int MyProperty { get; private set; } = 42;
Initialization is optional.
In this exercise you'll be modelling a weighing machine with Kilograms as a Unit.
You have 6 tasks each of which requires you to implement one or more properties:
To cater to different demands, we allow each weighing machine to be customized with a precision (the number of digits after the decimal separator).
Implement the WeighingMachine
class to have a get-only Precision
property set to the constructor's precision
argument:
var wm = new WeighingMachine(precision: 3);
// => wm.Precision == 3
Implement the WeighingMachine.Weight
property to allow the weight to be get and set:
var wm = new WeighingMachine(precision: 3);
wm.Weight = 60.5;
// => wm.Weight == 60.5
Clearly, someone cannot have a negative weight.
Add validation to the WeighingMachine.Weight
property to throw an ArgumentOutOfRangeException
when trying to set it to a negative weight:
var wm = new WeighingMachine(precision: 3);
wm.Weight = -10; // Throws an ArgumentOutOfRangeException
The tare adjustment can be any value (even negative or a value that makes the display weight negative)
Implement the WeighingMachine.TareAdjustment
property to allow the tare adjustment to be set:
var wm = new WeighingMachine(precision: 3);
wm.TareAdjustment = -10.6;
// => wm.TareAdjustment == -10.6
After some thorough testing, it appears that due to a manifacturing issue all weighing machines have a bias towards overestimating the weight by 5
.
Change the WeighingMachine.TareAdjustment
property to 5
as its default value.
var wm = new WeighingMachine(precision: 3);
// => wm.TareAdjustment == 5.0
Implement the WeighingMachine.DisplayWeight
property which should return a string showing the weight after tare adjustment, with the correct precision applied, and the unit added.
Note that:
display-weight = input-weight - tare-adjustment
var wm = new WeighingMachine(precision: 3);
wm.Weight = 60.567;
wm.TareAdjustment = 10;
// => wm.DisplayWeight == "50.567 kg"
Sign up to Exercism to learn and master C# with 62 concepts, 171 exercises, and real human mentoring, all for free.