From 68789af7ab2b058a4ed169c872cc4cd3326c72fd Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:27:13 +0100 Subject: [PATCH] docs(cars-assemble): add cast example, fix formatting --- concepts/numbers/introduction.md | 11 +++++++++++ exercises/concept/cars-assemble/.docs/hints.md | 7 +++++-- exercises/concept/cars-assemble/.docs/instructions.md | 11 ++++++++--- exercises/concept/cars-assemble/.docs/introduction.md | 11 +++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md index 0dfe935f3a..a0c3b2888a 100644 --- a/concepts/numbers/introduction.md +++ b/concepts/numbers/introduction.md @@ -15,3 +15,14 @@ C# has two types of numeric conversions: 2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required. As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion. + +```csharp +int softCool = 1358938113; +double notLost = 4.8151623; + +// implicit cast: no loss of information +double macDebug = softCool; // 1358938113.0 + +// explicit cast: possible loss of information +int somethingLost = (int)notLost; // 4 +``` diff --git a/exercises/concept/cars-assemble/.docs/hints.md b/exercises/concept/cars-assemble/.docs/hints.md index 2844a387c1..28aa1e768e 100644 --- a/exercises/concept/cars-assemble/.docs/hints.md +++ b/exercises/concept/cars-assemble/.docs/hints.md @@ -11,12 +11,15 @@ ## 2. Calculate the production rate per second - Use the `AssemblyLine.SuccessRate()` method you wrote earlier to determine the success rate. -- C# allows for multiplication to be applied to two different number types (such as an `int` and a `double`). It will automatically return the "largest" data type. +- C# allows for multiplication to be applied to two different number types (such as an `int` and a `double`). + It will automatically return the "largest" data type. - Numbers can be compared using the built-in [comparison-][comparison-operators] and [equality operators][equality-operators]. ## 3. Calculate the number of working items produced per second -- Whereas an `int` can be automatically converted to a `double`, the reverse does not hold. The reason for this is that an `int` has less precision than a `double` so rounding has to be applied, also the range of numbers an `int` can represent is smaller than a `double`. To force this conversion, one can either use one of the [`Convert` class' methods][convert-class] or [cast to an int][cast-int]. +- Whereas an `int` can be automatically converted to a `double`, the reverse does not hold. + The reason for this is that an `int` has less precision than a `double` so rounding has to be applied, also the range of numbers an `int` can represent is smaller than a `double`. + To force this conversion, one can either use one of the [`Convert` class' methods][convert-class] or [cast to an int][cast-int]. [convert-class]: https://docs.microsoft.com/en-us/dotnet/api/system.convert [cast-int]: https://www.dotnetperls.com/cast-int diff --git a/exercises/concept/cars-assemble/.docs/instructions.md b/exercises/concept/cars-assemble/.docs/instructions.md index ebb6c0f8a2..3413c51598 100644 --- a/exercises/concept/cars-assemble/.docs/instructions.md +++ b/exercises/concept/cars-assemble/.docs/instructions.md @@ -1,14 +1,19 @@ # Instructions -In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from `0` (off) to `10` (maximum). +In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. +The assembly line's speed can range from `0` (off) to `10` (maximum). -At its lowest speed (`1`), `221` cars are produced each hour. The production increases linearly with the speed. So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. +At its lowest speed (`1`), `221` cars are produced each hour. +The production increases linearly with the speed. +So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. +However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. You have three tasks. ## 1. Calculate the success rate -Implement the (_static_) `AssemblyLine.SuccessRate()` method to calculate the ratio of an item being created without error for a given speed. The following table shows how speed influences the success rate: +Implement the (_static_) `AssemblyLine.SuccessRate()` method to calculate the ratio of an item being created without error for a given speed. +The following table shows how speed influences the success rate: - `0`: 0% success rate. - `1` to `4`: 100% success rate. diff --git a/exercises/concept/cars-assemble/.docs/introduction.md b/exercises/concept/cars-assemble/.docs/introduction.md index 0d259919e0..78617e93b7 100644 --- a/exercises/concept/cars-assemble/.docs/introduction.md +++ b/exercises/concept/cars-assemble/.docs/introduction.md @@ -18,6 +18,17 @@ C# has two types of numeric conversions: As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion. +```csharp +int softCool = 1358938113; +double notLost = 4.8151623; + +// implicit cast: no loss of information +double macDebug = softCool; // 1358938113.0 + +// explicit cast: possible loss of information +int somethingLost = (int)notLost; // 4 +``` + ## If Statements In this exercise you must conditionally execute logic. The most common way to do this in C# is by using an `if/else` statement: