Skip to content

Commit 2993124

Browse files
authored
docs(cars-assemble): add cast example, fix formatting (#2321)
1 parent 7727138 commit 2993124

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

concepts/numbers/introduction.md

+11
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ C# has two types of numeric conversions:
1515
2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required.
1616

1717
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.
18+
19+
```csharp
20+
int softCool = 1358938113;
21+
double notLost = 4.8151623;
22+
23+
// implicit cast: no loss of information
24+
double macDebug = softCool; // 1358938113.0
25+
26+
// explicit cast: possible loss of information
27+
int somethingLost = (int)notLost; // 4
28+
```

exercises/concept/cars-assemble/.docs/hints.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
## 2. Calculate the production rate per second
1212

1313
- Use the `AssemblyLine.SuccessRate()` method you wrote earlier to determine the success rate.
14-
- 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.
14+
- C# allows for multiplication to be applied to two different number types (such as an `int` and a `double`).
15+
It will automatically return the "largest" data type.
1516
- Numbers can be compared using the built-in [comparison-][comparison-operators] and [equality operators][equality-operators].
1617

1718
## 3. Calculate the number of working items produced per second
1819

19-
- 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].
20+
- Whereas an `int` can be automatically converted to a `double`, the reverse does not hold.
21+
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`.
22+
To force this conversion, one can either use one of the [`Convert` class' methods][convert-class] or [cast to an int][cast-int].
2023

2124
[convert-class]: https://docs.microsoft.com/en-us/dotnet/api/system.convert
2225
[cast-int]: https://www.dotnetperls.com/cast-int

exercises/concept/cars-assemble/.docs/instructions.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
# Instructions
22

3-
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).
3+
In this exercise you'll be writing code to analyze the production of an assembly line in a car factory.
4+
The assembly line's speed can range from `0` (off) to `10` (maximum).
45

5-
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.
6+
At its lowest speed (`1`), `221` cars are produced each hour.
7+
The production increases linearly with the speed.
8+
So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour.
9+
However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded.
610

711
You have three tasks.
812

913
## 1. Calculate the success rate
1014

11-
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:
15+
Implement the (_static_) `AssemblyLine.SuccessRate()` method to calculate the ratio of an item being created without error for a given speed.
16+
The following table shows how speed influences the success rate:
1217

1318
- `0`: 0% success rate.
1419
- `1` to `4`: 100% success rate.

exercises/concept/cars-assemble/.docs/introduction.md

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ C# has two types of numeric conversions:
1818

1919
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.
2020

21+
```csharp
22+
int softCool = 1358938113;
23+
double notLost = 4.8151623;
24+
25+
// implicit cast: no loss of information
26+
double macDebug = softCool; // 1358938113.0
27+
28+
// explicit cast: possible loss of information
29+
int somethingLost = (int)notLost; // 4
30+
```
31+
2132
## If Statements
2233

2334
In this exercise you must conditionally execute logic. The most common way to do this in C# is by using an `if/else` statement:

0 commit comments

Comments
 (0)