Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cars-assemble): add cast example, fix formatting #2321

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions concepts/numbers/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
7 changes: 5 additions & 2 deletions exercises/concept/cars-assemble/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions exercises/concept/cars-assemble/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
11 changes: 11 additions & 0 deletions exercises/concept/cars-assemble/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading