diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 4a7fe1e..271387b 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -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)) diff --git a/src/main/kotlin/ru/otus/homework/homeworkFunctions.kt b/src/main/kotlin/ru/otus/homework/homeworkFunctions.kt new file mode 100644 index 0000000..60ea8d8 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/homeworkFunctions.kt @@ -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) + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt index 93a36cf..08eb0b5 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -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 { @@ -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 = ',') + ) + } } \ No newline at end of file