Skip to content
Open
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
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@
],
"status": "beta"
},
{
"slug": "cars-assemble",
"name": "Cars, Assemble!",
"uuid": "4949e5b9-c685-4380-9d70-46ae970035cf",
"concepts": [
"comparison-operators",
"if-control-structures"
],
"prerequisites": [
"booleans",
"integers",
"floating-point-numbers",
"arithmetic-operators"
],
"status": "beta"
},
{
"slug": "windowing-system",
"name": "Windowing System",
Expand Down
25 changes: 25 additions & 0 deletions exercises/concept/cars-assemble/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hints

## General

- Review the [comparison operators][comparison-operators] and [control structures][if-statement] documentation.

## 1. Calculate the success rate

- Determining the success rate can be done through a [conditional statement][if-statement].
- Numbers can be compared using the built-in [comparison operators][comparison-operators].

## 2. Calculate the production rate per hour

- Use the `CarsAssemble.successRate()` method you wrote earlier to determine the success rate.
- PHP allows multiplication between integers and floating-point numbers.
The result will be a floating-point number when either operand is a float.

## 3. Calculate the number of working items produced per minute

- Converting a floating-point number to an integer discards the fractional part (truncation toward zero).
- You can cast to an integer with `(int)` or use [`intval()`][intval].

[comparison-operators]: https://www.php.net/manual/en/language.operators.comparison.php
[if-statement]: https://www.php.net/manual/en/control-structures.if.php
[intval]: https://www.php.net/manual/en/function.intval.php
58 changes: 58 additions & 0 deletions exercises/concept/cars-assemble/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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).

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 `CarsAssemble.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.
- `5` to `8`: 90% success rate.
- `9`: 80% success rate.
- `10`: 77% success rate.

```php
<?php

$assembly_line = new CarsAssemble();
$assembly_line->successRate(10);
// => 0.77
```

## 2. Calculate the production rate per hour

Implement the `CarsAssemble.productionRatePerHour()` method to calculate the assembly line's production rate per hour, taking into account its success rate:

```php
<?php

$assembly_line = new CarsAssemble();
$assembly_line->productionRatePerHour(6);
// => 1193.4
```

Note that the value returned is a floating-point number.

## 3. Calculate the number of working items produced per minute

Implement the `CarsAssemble.workingItemsPerMinute()` method to calculate how many working cars are produced per minute:

```php
<?php

$assembly_line = new CarsAssemble();
$assembly_line->workingItemsPerMinute(6);
// => 19
```

Note that the value returned is an integer.
93 changes: 93 additions & 0 deletions exercises/concept/cars-assemble/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Introduction

## Comparison Operators

PHP has ten built in comparison operators:

| Name | Example | Result |
| ----- | ---------- | -------------------------------------------------- |
| Equal | `$a == $b` | true if `$a` is equal to `$b` after type juggling. |
| Identical | `$a === $b` | true if `$a` is equal to `$b` and the same type. |
| Not Equal | `$a != $b` | true if `$a` is not equal to `$b` after type juggling. |
| Not Equal | `$a <> $b` | true if `$a` is not equal to `$b` after type juggling. |
| Not identical | `$a !== $b` | true if `$a` is not equal to `$b` or not of the same type. |
| Less than | `$a < $b` | true if `$a` is strictly less than `$b`. |
| Greater than | `$a > $b` | true if `$a` is strictly greater than `$b`. |
| Less than or equal to | `$a <= $b` | true if `$a` is less than or equal to `$b`. |
| Greater than or equal to | `$a >= $b` | true if `$a` is greater than or equal to `$b`. |
| Spaceship | `$a <=> $b` | returns an integer less than, equal to, or greater than `0`i when `$a`. |

PHP has distinct definitions that differentiate between `equal` and `identical`.
Sometimes `identical` is also referred to as `strictly equal`.

```php
<?php

// Comparisons between integer and numeric string values
1 == "1"; // => true, equal
1 === "1"; // => false, not identical

// Comparisons between integers and floating point values
1 == 1.0; // => true
1 === 1.0; // => false

// Comparisons between object instances
new stdClass() == new stdClass(); // => true, properties are equal
new stdClass() === new stdClass(); // => false, references are not identical
```

## If, Else, Elseif

Conditional statements using `if`, `elseif`, and `else` are a fundamental parts of program control flow.
The `if` statement evaluates an expression, and if `true`, will execute the code branch.

```php
<?php

if ($expression) {
// .. executed if $expression is true
}
```

If the expression is not a boolean value, the evaluation determines the value's "truthiness" or "not falsiness".
In PHP, the following values are considered equal to false:

- boolean `false`
- integer `0`
- float `0.0` and `-0.0`
- an empty array `[]`
- an empty string `""` or a numeric string `"0"`
- `null`

All other values are considered true.

### Responding to multiple conditions

Following an `if` statement, you may chain multiple conditions using `elseif` and `else`.

```php
<?php

if ($value === 0) {
// .. do something
} elseif ($value === 1) {
// .. do something else
} else {
// ,, do this if nothing else
}
```

Only one conditional statement evaluated as true will be executed.
So if multiple conditions may evaluate to true, the order they are written is important.

```php
<?php

if (true) {
// .. always do something
} elseif (true)
// .. will never be executed
} else {
// .. will never be executed
}
```
5 changes: 5 additions & 0 deletions exercises/concept/cars-assemble/.docs/introduction.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

%{concept:comparison-operators}

%{concept:if-control-structures}
20 changes: 20 additions & 0 deletions exercises/concept/cars-assemble/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"pablo-miralles"
],
"files": {
"solution": [
"CarsAssemble.php"
],
"test": [
"CarsAssembleTest.php"
],
"exemplar": [
".meta/exemplar.php"
]
},
"forked_from": [
"csharp/cars-assemble"
],
"blurb": "Learn about comparisons and if statements by analyzing a car assembly line!"
}
40 changes: 40 additions & 0 deletions exercises/concept/cars-assemble/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Design

## Goal

This exercise teaches students how to use comparison operators and `if` control structures to branch logic based on numeric ranges.

## Learning objectives

- Know how to compare values using comparison operators (`==`, `===`, `<`, `>`, `<=`, `>=`).
- Know the difference between equal (`==`) and identical (`===`) comparisons.
- Know how to conditionally execute code using `if` statements.
- Know how to chain conditions with `elseif` / multiple `if` statements.
- Know how to convert a floating-point number to an integer by truncation.

## Out of scope

- `switch` / `match` expressions.
- The ternary operator.
- Type declarations.
- `declare(strict_types=1)`.

## Concepts

- `comparison-operators`: know how to compare values; know the difference between equal and identical comparisons.
- `if-control-structures`: know how to conditionally execute code using `if` / `elseif` / `else`.

## Prerequisites

- `booleans`: know how to use boolean values and operators.
- `integers`: know how to work with whole numbers.
- `floating-point-numbers`: know how to work with floating-point numbers.
- `arithmetic-operators`: know how to multiply and divide numbers.

## Analyzer

This exercise could benefit from the following rules:

- `actionable`: If the student did not reuse `successRate` inside `productionRatePerHour`, instruct them to do so.
- `informative`: If the solution repeatedly hard-codes `221`, suggest storing it in a constant.
- `informative`: If the solution uses `if`/`elseif` with early returns, note that trailing `else` branches may be redundant.
37 changes: 37 additions & 0 deletions exercises/concept/cars-assemble/.meta/exemplar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

class CarsAssemble
{
private const CARS_PER_HOUR = 221;

public function successRate($speed)
{
if ($speed === 10) {
return 0.77;
}

if ($speed === 9) {
return 0.8;
}

if ($speed >= 5) {
return 0.9;
}

if ($speed >= 1) {
return 1.0;
}

return 0.0;
}

public function productionRatePerHour($speed)
{
return self::CARS_PER_HOUR * $speed * $this->successRate($speed);
}

public function workingItemsPerMinute($speed)
{
return (int) ($this->productionRatePerHour($speed) / 60);
}
}
19 changes: 19 additions & 0 deletions exercises/concept/cars-assemble/CarsAssemble.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class CarsAssemble
{
public function successRate($speed)
{
throw new \BadFunctionCallException("Implement the function");
}

public function productionRatePerHour($speed)
{
throw new \BadFunctionCallException("Implement the function");
}

public function workingItemsPerMinute($speed)
{
throw new \BadFunctionCallException("Implement the function");
}
}
Loading