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
26 changes: 26 additions & 0 deletions src/main/kotlin/ru/otus/homework/functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@ package ru.otus.homework
import java.time.LocalDate

fun main() {
val hmFun = HomeworkFunctions()

println("1. Функция сумма параметров")
val firstValue = 1
val secondValue = 2
val numbers = intArrayOf(3, 4, 5)
val result1 = hmFun.sum(firstValue, secondValue,*numbers)
println("Input: first = $firstValue, second = $secondValue, others = ${numbers.joinToString()}")
println("Output: $result1")
println()

println("2. Функция с необязательным параметром")
val customStrings = arrayOf("hello", "my", "dear", "teacher")
val customSeparator = '-'
val result2 = hmFun.joinStrings(*customStrings, separator = customSeparator)
println("Input: strings = ${customStrings.joinToString()}, separator = '$customSeparator'")
println("Output: $result2")
println()

println("4. Функция, измеряющая время")
val executionTime = hmFun.measureExecutionTime {
hmFun.longTask()
}

println("Время выполнения: $executionTime мс")

println(calculate(10, 20))
println(calculate(10, 20.5F))
println(calculate(30.1F, 40.2F, 50.3F, 60.4F))
Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/ru/otus/homework/homeworkFunctions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.otus.homework

class HomeworkFunctions {

fun sum(first: Int, second: Int, vararg others: Int): Int {
return first + second + others.sum()
}

fun joinStrings(vararg strings: String, separator: Char = ' '): String {
return strings.joinToString(separator.toString())
}

fun measureExecutionTime(block: () -> Unit): Long {
val startTime = System.currentTimeMillis()
block()
val endTime = System.currentTimeMillis()
return endTime - startTime
}

fun longTask() {
for (i in 1..5) {
println("Шаг $i")
Thread.sleep(500)
}
}
}
14 changes: 14 additions & 0 deletions src/test/kotlin/ru/otus/homework/FunctionsTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.otus.homework

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class FunctionsTest {
Expand All @@ -11,4 +12,17 @@ class FunctionsTest {
calculate(1, 2)
)
}

@Test
fun joinStringsTest() {
val hmFun = HomeworkFunctions()
assertEquals(
"str1 str2 str3",
hmFun.joinStrings("str1", "str2", "str3")
)
assertEquals(
"str1,str2,str3",
hmFun.joinStrings("str1", "str2", "str3", separator = ',')
)
}
}