diff --git a/Programming Basics/Booleans/Exercise 2/src/Task.kt b/Programming Basics/Booleans/Exercise 2/src/Task.kt new file mode 100644 index 00000000..9b3bae90 --- /dev/null +++ b/Programming Basics/Booleans/Exercise 2/src/Task.kt @@ -0,0 +1,25 @@ +package booleans2 + +fun showAnd(first: Boolean, second: Boolean) { + println("$first && $second == ${first && second}") +} + +fun showOr(first: Boolean, second: Boolean) { + println("$first || $second == ${first || second}") +} + +fun showTruthTable() { + showAnd(true, true) + showAnd(true, false) + showAnd(false, true) + showAnd(false, false) + + showOr(true, true) + showOr(true, false) + showOr(false, true) + showOr(false, false) +} + +fun main() { + showTruthTable() +} diff --git a/Programming Basics/Booleans/Exercise 2/task-info.yaml b/Programming Basics/Booleans/Exercise 2/task-info.yaml new file mode 100644 index 00000000..7ad1b827 --- /dev/null +++ b/Programming Basics/Booleans/Exercise 2/task-info.yaml @@ -0,0 +1,6 @@ +type: edu +files: +- name: src/Task.kt + visible: true +- name: test/Tests.kt + visible: false diff --git a/Programming Basics/Booleans/Exercise 2/task.md b/Programming Basics/Booleans/Exercise 2/task.md new file mode 100644 index 00000000..dade8594 --- /dev/null +++ b/Programming Basics/Booleans/Exercise 2/task.md @@ -0,0 +1,16 @@ +## Booleans (#2) + +Create a "truth table" for `&&` and `||`. Write a function `showAnd(first: +Boolean, second: Boolean)` that uses a `String` template to show `first`, the +`&&` symbol, `second`, `==` symbol, and then the result of `first && second`. +Write a similar function for `showOr()`. + +The sample output for `showAnd(true, true)` should be: + +``` +true && true == true +``` + +Then write a function `showTruthTable` that should display a table produced by +all possible combinations of `true` and `false` for both `showAnd()` and +`showOr()`. Make sure you include all possible ordering. \ No newline at end of file diff --git a/Programming Basics/Booleans/Exercise 2/test/Tests.kt b/Programming Basics/Booleans/Exercise 2/test/Tests.kt new file mode 100644 index 00000000..2a551636 --- /dev/null +++ b/Programming Basics/Booleans/Exercise 2/test/Tests.kt @@ -0,0 +1,57 @@ +package booleans2 + +import org.junit.Assert +import org.junit.FixMethodOrder +import org.junit.Test +import org.junit.runners.MethodSorters +import util.runAndCheckSystemOutput +import util.runAndGetSystemOutput + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +class TestBooleans { + @Test + fun test1ShowFunctions() { + testShowAnd(true, true) + testShowAnd(true, false) + testShowAnd(false, true) + testShowAnd(false, false) + + testShowOr(true, true) + testShowOr(true, false) + testShowOr(false, true) + testShowOr(false, false) + } + + private fun testShowAnd(first: Boolean, second: Boolean) { + runAndCheckSystemOutput("Wrong output for 'showAnd($first, $second)", + "$first && $second == ${first && second}") { + showAnd(first, second) + } + } + + private fun testShowOr(first: Boolean, second: Boolean) { + runAndCheckSystemOutput("Wrong output for 'showOr($first, $second)", + "$first || $second == ${first || second}") { + showOr(first, second) + } + } + + @Test + fun test2Table() { + val table = runAndGetSystemOutput { showTruthTable() } + val expectedLines = """ + true && true == true + true && false == false + false && true == false + false && false == false + true || true == true + true || false == true + false || true == true + false || false == false + """.trimIndent().lines() + expectedLines.forEach { + Assert.assertTrue("""Not found the line "$it" in the table""", + it in table) + } + } +} \ No newline at end of file diff --git a/Programming Basics/Booleans/Exercise 3/src/Task.kt b/Programming Basics/Booleans/Exercise 3/src/Task.kt index 74c0d085..6cfae0e4 100644 --- a/Programming Basics/Booleans/Exercise 3/src/Task.kt +++ b/Programming Basics/Booleans/Exercise 3/src/Task.kt @@ -1,4 +1,4 @@ -package booleans2 +package booleans3 fun and(b1: Boolean, b2: Boolean): Boolean = if (b1) b2 else false diff --git a/Programming Basics/Booleans/Exercise 3/test/Tests.kt b/Programming Basics/Booleans/Exercise 3/test/Tests.kt index 5b7b9e0b..aa666d66 100644 --- a/Programming Basics/Booleans/Exercise 3/test/Tests.kt +++ b/Programming Basics/Booleans/Exercise 3/test/Tests.kt @@ -1,4 +1,4 @@ -package booleans2 +package booleans3 import org.junit.Assert import org.junit.FixMethodOrder diff --git a/Programming Basics/Booleans/Exercise 4/src/Foo.kt b/Programming Basics/Booleans/Exercise 4/src/Foo.kt index 8aebb2c5..3312b380 100644 --- a/Programming Basics/Booleans/Exercise 4/src/Foo.kt +++ b/Programming Basics/Booleans/Exercise 4/src/Foo.kt @@ -1,3 +1,3 @@ -package booleans3 +package booleans4 fun foo(): Boolean = true \ No newline at end of file diff --git a/Programming Basics/Booleans/Exercise 4/src/Task.kt b/Programming Basics/Booleans/Exercise 4/src/Task.kt index c8958824..7c2684f4 100644 --- a/Programming Basics/Booleans/Exercise 4/src/Task.kt +++ b/Programming Basics/Booleans/Exercise 4/src/Task.kt @@ -1,4 +1,4 @@ -package booleans3 +package booleans4 fun main() { println(foo()) diff --git a/Programming Basics/Booleans/Exercise 4/test/Tests.kt b/Programming Basics/Booleans/Exercise 4/test/Tests.kt index fb05f855..d7ef4050 100644 --- a/Programming Basics/Booleans/Exercise 4/test/Tests.kt +++ b/Programming Basics/Booleans/Exercise 4/test/Tests.kt @@ -1 +1 @@ -package booleans3 \ No newline at end of file +package booleans4 \ No newline at end of file diff --git a/Programming Basics/Booleans/lesson-info.yaml b/Programming Basics/Booleans/lesson-info.yaml index 49c0fa3c..19662b24 100644 --- a/Programming Basics/Booleans/lesson-info.yaml +++ b/Programming Basics/Booleans/lesson-info.yaml @@ -1,5 +1,6 @@ content: - Examples - Exercise 1 +- Exercise 2 - Exercise 3 - Exercise 4