Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
[Kotlin] #1716. Post-review fixes for concept exercise basic
Browse files Browse the repository at this point in the history
  • Loading branch information
dector committed Jul 22, 2020
1 parent c3ec730 commit aefa73e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions languages/kotlin/exercises/concept/basics/.docs/after.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# What's next?

## Submitting your solution
## Type inference

_Hint_: Use `Code --> Reformat` in [IntelliJ IDEA][intellij-idea-ic] to automatically format your code.
Kotlin compiler can infer types for functions and variables in most of the cases. However, declaring function return types explicitly for **public API** is a good practice.

## Entry point

Expand Down
1 change: 1 addition & 0 deletions languages/kotlin/exercises/concept/basics/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## General

- An [integer (`Int`) value][numbers] can be defined as one or more consecutive digits.
- Use variables to give nice and readable names for your values. Remember that code readability is much more important than amount of code lines.

## 1. Calculate remaining baking time in minutes

Expand Down
20 changes: 9 additions & 11 deletions languages/kotlin/exercises/concept/basics/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Owner of your local pizza place asked you to help her to count cooking time for different pizzas. In this exercise you're going to write few functions to calculate that.
The owner of your local pizza place has asked you to help her to calculate cooking times for different pizzas. In this exercise you're going to write few functions to calculate that.

You have three tasks.

## 1. Calculate remaining baking time in minutes

Define and implement `remainingMinutesInOven()` function that will count how many more minutes you need to keep pizza in the oven.
Define and implement the `remainingMinutesInOven()` function that will count how many more minutes you need to keep the pizza in the oven.

Function should accept one **optional** parameter (with default value) for the amount of minutes that passed since you've started baking pizza. If parameter is missing - assume that you've just put pizza into the oven and **0 (zero)** minutes passed already.
The function should accept one **optional** parameter (with default value) for the amount of minutes that passed since you've started baking pizza. If the parameter is missing - assume that you've just put pizza into the oven and **0 (zero)** minutes passed already.

For the simplicity - let's assume that all types of pizzas requires exactly 40 minutes in the oven to be ready.
For simplicity - let's assume that all types of pizzas require exactly 40 minutes in the oven to be ready.

```kotlin
remainingMinutesInOven() // => 40
Expand All @@ -18,11 +18,11 @@ remainingMinutesInOven(10) // => 30

## 2. Calculate the preparation time in minutes

Before baking pizza you need to prepare dough, roll it out and put toppings onto it.
Before baking the pizza, you need to prepare the dough, roll it out and put toppings onto it.

Define and implement `preparationTimeInMinutes()` function that will count how many minutes it will take to prepare pizza before putting it into the oven.
Define and implement the `preparationTimeInMinutes()` function that will count how many minutes it will take to prepare pizza before putting it into the oven.

Function should accept one **required** parameter (`numberOfIngredientTypes`) for the number of topping ingredient types that will be used.
The function should accept one **required** parameter (`numberOfIngredientTypes`) for the number of topping ingredient types that will be used.

Assume that you need 5 minutes to make and roll out dough and 2 minutes for using **each** kind of topping.

Expand All @@ -34,14 +34,12 @@ preparationTimeInMinutes(1) // => 7

## 3. Calculate the elapsed time in minutes

Define and implement `elapsedCookingTimeInMinutes()` function that will count how many minutes it will take to prepare, bake and serve pizza. Use functions from previous steps while making the calculations.
Define and implement the `elapsedCookingTimeInMinutes()` function that will count how many minutes it will take to prepare, bake and serve the pizza. Use the functions from the previous steps while making the calculations.

Function should accept two parameters. First one is **required** and is the number of topping ingredient types. Second one is **optional** and is the serving time in minutes (assume that is 0 if not provided).
The function should accept two parameters. The first one is **required** and is the number of topping ingredient types. The second one is **optional** and is the serving time in minutes (assume that is 0 if not provided).

```kotlin
elapsedCookingTimeInMinutes(1) // => 47

elapsedCookingTimeInMinutes(2, 3) // => 52
```

_Hint_: Use variables to give nice and readable names for your values. Remember that code readability is much more important than amount of code lines.
18 changes: 8 additions & 10 deletions languages/kotlin/exercises/concept/basics/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Kotlin is **statically typed** programming language developed by JetBrains. This means that type of variables is defined on compile-time.
Kotlin is a **statically typed** programming language developed by JetBrains. This means that the type of variables is defined at compile-time.

## Variables

Similarly to other statically-typed programming languages type of each variable should be defined in compile time. Similarly to other **modern** programming languages you can avoid explicit type declarations where they can be interfered by compiler from context. Use this ability to make your code more readable. But remember that there are cases where it's better to specify type explicitly - e.g. in public APIs.
Similarly to other statically-typed programming languages, the type of each variable should be defined at compile time. You can avoid explicit type declarations where they can be inferred by the compiler from their context.

Kotlin has immutable (`val`) and mutable (`var`) variable. Immutable variable means that once it has it's value assigned - it can't be changed. Most of the time you will use this type of variable.
Kotlin has immutable (`val`) and mutable (`var`) variables. The value of an immutable variable can't be changed after it's initial value is assigned. Most of the time you will use this type of variable.

```kotlin
val robotName = "HAL-9000"
userId = "T-1000" // This will not compile
```

Mutable variable means that it's value can be changed one or more times:
A mutable variable's value can be changed one or more times:

```kotlin
var index = 12
Expand All @@ -21,19 +21,19 @@ index = 100
print(index) // 100
```

BTW probably you have already noticed that semicolons in Kotlin are not used (they are optional but not needed except few special cases we will explore later in other exercises).
Semicolons in Kotlin are optional, except for a few special cases that will be covered later.

## Functions

Functions in Kotlin are defined with keyword **`fun`** and are [first-class citizens][wiki-fcc] (not related to OOP). It means that you can declare (so-called `top-level functions`) them right in files (e.g. in Java you can define methods only in classes, not in files):
Functions in Kotlin are defined with the `fun` keyword and are _first-class citizens_ (not related to OOP). It means that you can declare (so-called `top-level functions`) them right in files (e.g. in Java you can define methods only in classes, not in files):

```kotlin
// This is content of the Hello.kt file

fun hello() {}
```

Functions can receive arguments. Each argument have a name and a type. Probably you remember that compiler is smart enough to understand type of variable In many cases, but you always need to write types for function arguments by yourself as you are declaring certain contract. Functions can have zero or more arguments:
Functions can receive arguments. Each argument has a name and a type. Unlike variables, the type of arguments can't be inferred. Functions can have zero or more arguments:

```kotlin
fun hello() {}
Expand All @@ -53,7 +53,7 @@ fun countBonuses(user: User): Bonuses
fun run() {}
```

As you can see, to return value from function you need to use keyword **`return`**:
To return a value from a function, the `return` keyword is used:

```kotlin
fun getName(): String {
Expand Down Expand Up @@ -90,5 +90,3 @@ or `/*` and `*/` to define multi-line comments:
for a multiline comment
*/
```

[wiki-fcc]: https://en.wikipedia.org/wiki/First-class_citizen
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ fun remainingMinutesInOven(actualMinutesInOven: Int = 0): Int {
return cookingTime - actualMinutesInOven
}

fun preparationTimeInMinutes(numberOfIngredientTypes: Int): Int =
5 + numberOfIngredientTypes * 2
fun preparationTimeInMinutes(numberOfIngredientTypes: Int): Int {
return 5 + numberOfIngredientTypes * 2
}

fun elapsedCookingTimeInMinutes(numberOfIngredientTypes: Int, servingTimeInMinutes: Int = 0): Int {
val preparationTime = preparationTimeInMinutes(numberOfIngredientTypes)
Expand Down

0 comments on commit aefa73e

Please sign in to comment.