diff --git a/src/main/kotlin/exercise6/task1/SimpleGenerics.kt b/src/main/kotlin/exercise6/task1/SimpleGenerics.kt index edf9790..ebd905b 100644 --- a/src/main/kotlin/exercise6/task1/SimpleGenerics.kt +++ b/src/main/kotlin/exercise6/task1/SimpleGenerics.kt @@ -14,9 +14,9 @@ package exercise6.task1 /** * A binary tree node that holds some value. */ -private data class TreeNode(val value: Any) { - var left: TreeNode? = null - var right: TreeNode? = null +private data class TreeNode(val value: T) { + var left: TreeNode? = null + var right: TreeNode? = null } /** @@ -42,8 +42,7 @@ private fun printNodes(): String { val decisionI = Decision("Do you choose a red or a blue pill?", "Red", "Blue") val decisionTree = TreeNode(decisionI) - TODO("Uncomment code bellow and make the file compilable") -// return "${genealogyTree.value.name} ${decisionTree.value.answerLeft}" + return "${genealogyTree.value.name} ${decisionTree.value.answerLeft}" } fun main() { diff --git a/src/main/kotlin/exercise6/task2/UpperBoundConstraints.kt b/src/main/kotlin/exercise6/task2/UpperBoundConstraints.kt index 53e84c0..788dd5d 100644 --- a/src/main/kotlin/exercise6/task2/UpperBoundConstraints.kt +++ b/src/main/kotlin/exercise6/task2/UpperBoundConstraints.kt @@ -38,9 +38,8 @@ private data class SetIntersection(val size: Int): CostElement { * nodes. This code is not yet compilable, but it will become compilable as soon as you add an upper bound to the generic * type. */ -private fun calculateTreeCost(root: TreeNode): Double { - TODO("Uncomment code bellow and make the file compilable") -// return root.value.cost + -// (root.left?.let { calculateTreeCost(it) } ?: 0.0) + -// (root.right?.let { calculateTreeCost(it) } ?: 0.0) +private fun calculateTreeCost(root: TreeNode): Double { + return root.value.cost + + (root.left?.let { calculateTreeCost(it) } ?: 0.0) + + (root.right?.let { calculateTreeCost(it) } ?: 0.0) } \ No newline at end of file diff --git a/src/main/kotlin/exercise6/task3/UpperBoundConstraintPartTwo.kt b/src/main/kotlin/exercise6/task3/UpperBoundConstraintPartTwo.kt index 72a660a..edd5032 100644 --- a/src/main/kotlin/exercise6/task3/UpperBoundConstraintPartTwo.kt +++ b/src/main/kotlin/exercise6/task3/UpperBoundConstraintPartTwo.kt @@ -49,7 +49,7 @@ private data class SetIntersection(val size: Int, val lowerBound: S?, val upp * no intersection at all. If the intersection is likely to be not empty, it estimates the intersection size as the mean * of the set sizes. */ -private fun estimateIntersectionSize(treeNode: TreeNode>): Int { +private fun > estimateIntersectionSize(treeNode: TreeNode>): Int { val leftNode = treeNode.left ?: return treeNode.value.size val rightNode = treeNode.right ?: return treeNode.value.size @@ -60,9 +60,8 @@ private fun estimateIntersectionSize(treeNode: TreeNode>) return 0 } - TODO("Uncomment code bellow and make the file compilable") -// if (leftNode.value.upperBound < rightNode.value.lowerBound || leftNode.value.lowerBound > rightNode.value.upperBound) { -// return 0 -// } -// return (leftNode.value.size + rightNode.value.size)/2 + if (leftNode.value.upperBound < rightNode.value.lowerBound || leftNode.value.lowerBound > rightNode.value.upperBound) { + return 0 + } + return (leftNode.value.size + rightNode.value.size)/2 } \ No newline at end of file diff --git a/src/main/kotlin/exercise6/task4/UpperBoundConstraintPartThree.kt b/src/main/kotlin/exercise6/task4/UpperBoundConstraintPartThree.kt index 4614d38..ef5d459 100644 --- a/src/main/kotlin/exercise6/task4/UpperBoundConstraintPartThree.kt +++ b/src/main/kotlin/exercise6/task4/UpperBoundConstraintPartThree.kt @@ -89,29 +89,27 @@ private data class SetIntersection>( * no intersection at all. If the intersection is likely to be not empty, it estimates the intersection size as the mean * of the set sizes. */ -private fun estimateIntersectionSize(treeNode: TreeNode) : Int { - TODO("Uncomment code bellow and make the file compilable") -// val leftNode = treeNode.left ?: return treeNode.value.size -// val rightNode = treeNode.right ?: return treeNode.value.size -// -// if (leftNode.value.lowerBound == null || rightNode.value.lowerBound == null) { -// return 0 -// } -// if (leftNode.value.upperBound == null || rightNode.value.upperBound == null) { -// return 0 -// } -// -// if (leftNode.value.upperBound!! < rightNode.value.lowerBound!! || leftNode.value.lowerBound!! > rightNode.value.upperBound!!) { -// return 0 -// } -// return (leftNode.value.size + rightNode.value.size)/2 +private fun estimateIntersectionSize(treeNode: TreeNode) : Int where T: Range, T: Measurable, S : Comparable{ + val leftNode = treeNode.left ?: return treeNode.value.size + val rightNode = treeNode.right ?: return treeNode.value.size + + if (leftNode.value.lowerBound == null || rightNode.value.lowerBound == null) { + return 0 + } + if (leftNode.value.upperBound == null || rightNode.value.upperBound == null) { + return 0 + } + + if (leftNode.value.upperBound!! < rightNode.value.lowerBound!! || leftNode.value.lowerBound!! > rightNode.value.upperBound!!) { + return 0 + } + return (leftNode.value.size + rightNode.value.size)/2 } -private fun printTree(treeNode: TreeNode) { - TODO("Uncomment code bellow and make the file compilable") -// println("${treeNode.value} cost:${treeNode.value.cost}") -// treeNode.left?.let { printTree(it) } -// treeNode.right?.let { printTree(it) } +private fun printTree(treeNode: TreeNode) { + println("${treeNode.value} cost:${treeNode.value.cost}") + treeNode.left?.let { printTree(it) } + treeNode.right?.let { printTree(it) } } /** @@ -148,12 +146,11 @@ private fun run() { it.left = leftNode it.right = rightNode } - TODO("Uncomment code bellow and make the file compilable") -// val intersectionSize = estimateIntersectionSize(rootNode) -// println(intersectionSize) -// -// printTree(rootNode) -// printNodes() + val intersectionSize = estimateIntersectionSize(rootNode) + println(intersectionSize) + + printTree(rootNode) + printNodes() } fun main() { diff --git a/src/main/kotlin/exercise6/taskProjections/task1/Task.kt b/src/main/kotlin/exercise6/taskProjections/task1/Task.kt index 7905d94..5d6d0cd 100644 --- a/src/main/kotlin/exercise6/taskProjections/task1/Task.kt +++ b/src/main/kotlin/exercise6/taskProjections/task1/Task.kt @@ -11,12 +11,12 @@ package exercise6.taskProjections.task1 **/ -interface Sender { - fun send(item: Any) +interface Sender{ + fun send(item: T) } -class MailBox(private var box: Any? = null): Sender { - override fun send(item: Any) { +class MailBox (private var box: T? = null): Sender { + override fun send(item: T) { printCurrentBoxState() println("Sending the box: $item!") box = item @@ -32,8 +32,8 @@ class MailBox(private var box: Any? = null): Sender { } -class Postman(private val mailboxes: List): Sender { - override fun send(item: Any) { +class Postman(private val mailboxes: List>): Sender { + override fun send(item: T) { mailboxes.forEach { it.send(item) } } @@ -45,14 +45,16 @@ open class Postcard(open val origin: String) : Delivery data class ExpressPostcard(val priceEuro: Int, override val origin: String) : Postcard(origin) fun main() { - // TODO: This code should became compilable -// val postcardStorage = MailBox() -// val expressPostcardStorage = MailBox() + val postcardStorage = MailBox() + val expressPostcardStorage = MailBox() val expressPostcard = ExpressPostcard(15, "Serbia") val postcard = Postcard("Germany") - // TODO: add code to create topRatedPostman and juniorPostman. - // The topRatedPostman can send ONLY express postcards - // The juniorPostman can send both regular and express postcards + val topRatedPostman: Postman = Postman(listOf(expressPostcardStorage)) + val juniorPostman: Postman = Postman(listOf(postcardStorage)) + + topRatedPostman.send(expressPostcard) + juniorPostman.send(expressPostcard) + juniorPostman.send(postcard) } \ No newline at end of file diff --git a/src/main/kotlin/exercise6/taskProjections/task2/Task.kt b/src/main/kotlin/exercise6/taskProjections/task2/Task.kt index b28c6ae..c95d203 100644 --- a/src/main/kotlin/exercise6/taskProjections/task2/Task.kt +++ b/src/main/kotlin/exercise6/taskProjections/task2/Task.kt @@ -10,6 +10,12 @@ package exercise6.taskProjections.task2 * If you did everything correct, you could not create a method create which accepts nothing and returns T **/ +class Programmer() { + fun learn(language: T) { + println("I learned ${language.name}!") + } +} + interface ProgrammingLanguage { val name: String } @@ -18,16 +24,15 @@ class JvmLanguage(override val name: String) : ProgrammingLanguage class BlockBasedLanguage(override val name: String) : ProgrammingLanguage fun main() { - TODO("Uncomment this code when you finish the task") -// val programmer = Programmer() -// -// val jvmLanguages = listOf(JvmLanguage("Java"), JvmLanguage("Kotlin")) -// jvmLanguages.forEach{ programmer.learn(it) } // OK -// -// val blockBasedLanguages = listOf(BlockBasedLanguage("Scratch"), JvmLanguage("Snap")) -// blockBasedLanguages.forEach{ programmer.learn(it) } // OK -// -// (jvmLanguages + blockBasedLanguages).forEach{ programmer.learn(it) } // OK -// -//// val newLanguage: ProgrammingLanguage = programmer.create() // ERROR + val programmer = Programmer() + + val jvmLanguages = listOf(JvmLanguage("Java"), JvmLanguage("Kotlin")) + jvmLanguages.forEach{ programmer.learn(it) } // OK + + val blockBasedLanguages = listOf(BlockBasedLanguage("Scratch"), JvmLanguage("Snap")) + blockBasedLanguages.forEach{ programmer.learn(it) } // OK + + (jvmLanguages + blockBasedLanguages).forEach{ programmer.learn(it) } // OK + +// val newLanguage: ProgrammingLanguage = programmer.create() // ERROR } diff --git a/src/main/kotlin/exercise6/taskProjections/task3/Task.kt b/src/main/kotlin/exercise6/taskProjections/task3/Task.kt index 211a74f..ce9c988 100644 --- a/src/main/kotlin/exercise6/taskProjections/task3/Task.kt +++ b/src/main/kotlin/exercise6/taskProjections/task3/Task.kt @@ -36,11 +36,12 @@ class BlockBasedLanguageParser : LanguageParser { } fun main() { - TODO("Create a new HashMap and uncomment code bellow") -// parsersHashMap[JvmLanguage::class.java] = JvmLanguageParser() -// parsersHashMap[BlockBasedLanguage::class.java] = BlockBasedLanguageParser() -// -// val scratch = BlockBasedLanguage() -// val foundParser = parsersHashMap[scratch.javaClass] -// foundParser?.parse(scratch) + val parsersHashMap = HashMap, LanguageParser<*>>() + + parsersHashMap[JvmLanguage::class.java] = JvmLanguageParser() + parsersHashMap[BlockBasedLanguage::class.java] = BlockBasedLanguageParser() + + val scratch = BlockBasedLanguage() + val foundParser = parsersHashMap[scratch.javaClass] + foundParser?.parse(scratch) } diff --git a/src/main/kotlin/exercise6/taskProjections/task4/Task.kt b/src/main/kotlin/exercise6/taskProjections/task4/Task.kt index c2d86d9..0900516 100644 --- a/src/main/kotlin/exercise6/taskProjections/task4/Task.kt +++ b/src/main/kotlin/exercise6/taskProjections/task4/Task.kt @@ -7,6 +7,15 @@ package exercise6.taskProjections.task4 * that accepts two arrays with a generic type of Any and copy elements from the first array to the second one. **/ +fun copy(arr1 : Array, arr2 : Array) { + require(arr2.size >= arr1.size) { + throw Exception("Second array is shorter than first!") + } + + for (i in arr1.indices) + arr2[i] = arr1[i] +} + fun main() { val ints = arrayOf(1, 2, 3) val any = Array(3) { "" } @@ -17,8 +26,7 @@ fun main() { println("_______") println("_______") - // TODO: uncomment this code - // copy(ints, any) + copy(ints, any) ints.forEach{ print("$it ") } println() diff --git a/src/main/kotlin/exercise6/taskProjections/task5/Task.kt b/src/main/kotlin/exercise6/taskProjections/task5/Task.kt index 9123785..c7d8d72 100644 --- a/src/main/kotlin/exercise6/taskProjections/task5/Task.kt +++ b/src/main/kotlin/exercise6/taskProjections/task5/Task.kt @@ -7,6 +7,17 @@ package exercise6.taskProjections.task5 * Create a new class "Printer", that can accept only T elements and print them * - add a "print" function that prints item with type T; * + + **/ + +class Printer { + fun print(element : T) { + println(element) + } +} + +/** + * [2] * Create a new abstract class "Builder" that generates instances of T based on parameter of P type. * This class cannot produce P and consume T: @@ -14,20 +25,28 @@ package exercise6.taskProjections.task5 * Create a new class "IntegerBuilder", that implements Builder and can convert String to Int **/ +abstract class Builder { + abstract fun build(input : P) : T +} + +class IntegerBuilder() : Builder() { + override fun build(input: String): Int { + return input.toInt() + } +} + fun main() { // [1] - // TODO: uncomment me -// val integerPrinter = Printer() -// val stringPrinter = Printer() -// -// integerPrinter.print(2) -// stringPrinter.print("Bla bla bla") + val integerPrinter = Printer() + val stringPrinter = Printer() + + integerPrinter.print(2) + stringPrinter.print("Bla bla bla") println("________") // [2] - // TODO: uncomment me -// val intBuilder = IntegerBuilder() -// val x = intBuilder.build("1") -// println("We build [ $x ]") + val intBuilder = IntegerBuilder() + val x = intBuilder.build("1") + println("We build [ $x ]") } diff --git a/src/main/kotlin/exercise6/taskProjections/task6/Task.kt b/src/main/kotlin/exercise6/taskProjections/task6/Task.kt index e897e4c..518fe1b 100644 --- a/src/main/kotlin/exercise6/taskProjections/task6/Task.kt +++ b/src/main/kotlin/exercise6/taskProjections/task6/Task.kt @@ -10,24 +10,22 @@ open class A class B : A() class C : A() { fun consume(other: A): C = this } -// TODO: uncomment and fix me -//fun funny( -// source: Iterator, -// target: MutableCollection, -// base: ??, -// how: ?? -//) { -// var result: R = base -// for (value in source) { -// result = how(result, value) -// target.add(result) -// } -//} +fun funny( + source: Iterator, + target: MutableCollection, + base: R, + how: (R, T) -> S +) { + var result: R = base + for (value in source) { + result = how(result, value) + target.add(result) + } +} fun main() { - // TODO: uncomment me, it should not produce any compilation errors -// val wtf = mutableListOf() -// val src = mapOf(3.14 to B(), 2 to B(), "Hello" to B()) -// val c = C() -// funny(src.values.iterator(), wtf, c) { r, t -> r.consume(t) } + val wtf = mutableListOf() + val src = mapOf(3.14 to B(), 2 to B(), "Hello" to B()) + val c = C() + funny(src.values.iterator(), wtf, c) { r, t -> r.consume(t) } }