1
1
Fork 0

Added two more exercises to 'Creating classes'

This commit is contained in:
Svetlana Isakova 2019-08-11 20:39:35 +02:00
parent 1b8519f9f4
commit 9867257f37
25 changed files with 242 additions and 172 deletions

View File

@ -1,29 +1 @@
package creatingClasses1
class Robot {
fun goRight(steps: Int) {
println("going right $steps steps")
}
fun goLeft(steps: Int) {
println("going left $steps steps")
}
fun goDown(steps: Int) {
println("going down $steps steps")
}
fun goUp(steps: Int) {
println("going up $steps steps")
}
}
/*
fun main() {
val robot = Robot()
robot.goUp(11)
}
*/
/* Output:
going up 11 steps
*/
// type your solution here

View File

@ -2,11 +2,5 @@ type: edu
files:
- name: src/Task.kt
visible: true
placeholders:
- offset: 26
length: 296
placeholder_text: // create Robot class
- name: test/Tests.kt
visible: false
feedback_link: |
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Introduction+to+Objects+%2F+Creating+Classes+%2F+Exercise1

View File

@ -1,2 +0,0 @@
id: 399609
update_date: Wed, 03 Oct 2018 12:37:41 UTC

View File

@ -1,17 +0,0 @@
## Creating Classes (#1)
Create the `Robot` class with the following four member functions:
`goRight`, `goLeft`, `goDown` and `goUp`.
Each of these functions should take an integer `steps` as a parameter
and print one the following phrases to the console:
```
going right N steps
```
or
```
going left N steps
```
and so on accordingly, where N is the provided number of steps.

View File

@ -1,61 +1,11 @@
package creatingClasses1
import org.junit.FixMethodOrder
import org.junit.Assert
import org.junit.Test
import org.junit.runners.MethodSorters
import util.assertEqualsForOutput
import util.TIMEOUT
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.AssertionError
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestSimpleStringPalindrome {
@Test(timeout = TIMEOUT)
fun testRobot() {
createRobotInstance()
}
fun testDirection(direction: String, steps: Int) {
val (robotClass, robot) = createRobotInstance()
val goMethod =
try {
robotClass.getMethod("go$direction", Int::class.java)
} catch (e: NoSuchMethodException) {
throw AssertionError("Can't find the 'go$direction(steps: Int)' member function in 'Robot' class")
}
val byteArrayOutputStream = ByteArrayOutputStream()
System.setOut(PrintStream(byteArrayOutputStream))
goMethod.invoke(robot, steps)
assertEqualsForOutput("Incorrect output for 'go$direction' method",
"going ${direction.decapitalize()} $steps steps",
byteArrayOutputStream)
}
private fun createRobotInstance(): Pair<Class<out Any>, Any> {
val robotClass =
try {
ClassLoader.getSystemClassLoader().loadClass("creatingClasses1.Robot")
} catch (e: ClassNotFoundException) {
throw AssertionError("Can't find the 'Robot' class in 'creatingClasses1' package")
}
val robot = robotClass.constructors.first().newInstance()
return Pair(robotClass, robot)
}
@Test(timeout = TIMEOUT)
fun testRight() = testDirection("Right", 11)
@Test(timeout = TIMEOUT)
fun testLeft() = testDirection("Left", 37)
@Test(timeout = TIMEOUT)
fun testUp() = testDirection("Up", 4)
@Test(timeout = TIMEOUT)
fun testDown() = testDirection("Down", 8)
}
class TestSomeClass {
@Test fun testSolution() {
//TODO: implement your test here
Assert.assertTrue("Tests not implemented for the task", false)
}
}

View File

@ -1,11 +1,7 @@
package creatingClasses3
package creatingClasses4
fun main() {
val s = "Hello!"
println(s.toUpperCase())
println(s.toLowerCase())
class Foo {
fun bar() {
println(toString())
}
}
/* Output:
HELLO!
hello!
*/

View File

@ -1,8 +1,6 @@
type: ide
type: edu
files:
- name: src/Task.kt
visible: true
- name: test/Tests.kt
visible: false
feedback_link: |
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Introduction+to+Objects+%2F+Creating+Classes+%2F+Exercise2

View File

@ -1,13 +0,0 @@
## Mastering the IDE: Quick Documentation
The <span class="control">`Quick Documentation`</span> action produces
information about a symbol, such as a function or a class.
Press <span class="shortcut">&shortcut:QuickJavaDoc;</span> when the caret
is on a class or a function, and IntelliJ Idea will show you the available
documentation for this function or class.
Call <span class="control">`Quick Documentation`</span> action
for the `String` class and its `toUpperCase()` and `toLowerCase()` functions.
Press <span class="shortcut">&shortcut:EditorEscape;</span> to close the
quick documentation pop-up.

View File

@ -1 +1,37 @@
package creatingClasses3
package creatingClasses2
import org.junit.Assert
import org.junit.Test
import util.assertEqualsForOutput
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.AssertionError
class TestFoo {
@Test
fun testSolution() {
val fooClass =
try {
ClassLoader.getSystemClassLoader().loadClass("creatingClasses4.Foo")
} catch (e: ClassNotFoundException) {
throw AssertionError("Can't find the 'Foo' class in 'creatingClasses4' package")
}
val foo = fooClass.constructors.first().newInstance()
val barMethod =
try {
fooClass.getMethod("bar")
} catch (e: NoSuchMethodException) {
throw AssertionError("Can't find the 'bar()' member function in 'Foo' class")
}
val byteArrayOutputStream = ByteArrayOutputStream()
System.setOut(PrintStream(byteArrayOutputStream))
barMethod.invoke(foo)
val output = byteArrayOutputStream.toString()
Assert.assertTrue("The output should contain @ sign", '@' in output)
Assert.assertTrue("The output should contain 'Foo'", "Foo" in output)
}
}

View File

@ -1,12 +1,29 @@
package creatingClasses4
package creatingClasses3
fun cap(s: String) = s.capitalize()
class Robot {
fun goRight(steps: Int) {
println("going right $steps steps")
}
fun main() {
println(cap("hi!"))
println(cap("Hi!"))
fun goLeft(steps: Int) {
println("going left $steps steps")
}
fun goDown(steps: Int) {
println("going down $steps steps")
}
fun goUp(steps: Int) {
println("going up $steps steps")
}
}
/*
fun main() {
val robot = Robot()
robot.goUp(11)
}
*/
/* Output:
Hi!
Hi!
*/
going up 11 steps
*/

View File

@ -1,12 +1,12 @@
type: ide
type: edu
files:
- name: src/Task.kt
visible: true
placeholders:
- offset: 48
length: 13
placeholder_text: ' '
- offset: 26
length: 296
placeholder_text: // create Robot class
- name: test/Tests.kt
visible: false
feedback_link: |
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Introduction+to+Objects+%2F+Creating+Classes+%2F+Exercise3
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Introduction+to+Objects+%2F+Creating+Classes+%2F+Exercise1

View File

@ -1,2 +1,2 @@
id: 399613
update_date: Wed, 03 Oct 2018 12:37:44 UTC
id: 399609
update_date: Wed, 03 Oct 2018 12:37:41 UTC

View File

@ -1,9 +1,17 @@
## Mastering the IDE: Quick Documentation in the Completion List
## Creating Classes (#1)
The <span class="control">`Quick Documentation`</span> shortcut
<span class="shortcut">&shortcut:QuickJavaDoc;</span>
can be used not only in the editor but in the code completion popup list as well.
Create the `Robot` class with the following four member functions:
`goRight`, `goLeft`, `goDown` and `goUp`.
Each of these functions should take an integer `steps` as a parameter
and print one the following phrases to the console:
Using this feature, find the function for `String` that upper-cases only its
first letter, or returns the original string if it's already starts with an
upper case letter.
```
going right N steps
```
or
```
going left N steps
```
and so on accordingly, where N is the provided number of steps.

View File

@ -1,29 +1,61 @@
package creatingClasses4
package creatingClasses3
import org.junit.Assert
import org.junit.FixMethodOrder
import org.junit.Test
import org.junit.runners.MethodSorters
import util.assertEqualsForOutput
import util.TIMEOUT
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.AssertionError
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestCapitalize() {
private fun testString(s: String) {
Assert.assertEquals("""Wrong result for task("$s"):""", s.capitalize(), cap(s))
class TestSimpleStringPalindrome {
@Test(timeout = TIMEOUT)
fun testRobot() {
createRobotInstance()
}
fun testDirection(direction: String, steps: Int) {
val (robotClass, robot) = createRobotInstance()
val goMethod =
try {
robotClass.getMethod("go$direction", Int::class.java)
} catch (e: NoSuchMethodException) {
throw AssertionError("Can't find the 'go$direction(steps: Int)' member function in 'Robot' class")
}
val byteArrayOutputStream = ByteArrayOutputStream()
System.setOut(PrintStream(byteArrayOutputStream))
goMethod.invoke(robot, steps)
assertEqualsForOutput("Incorrect output for 'go$direction' method",
"going ${direction.decapitalize()} $steps steps",
byteArrayOutputStream)
}
private fun createRobotInstance(): Pair<Class<out Any>, Any> {
val robotClass =
try {
ClassLoader.getSystemClassLoader().loadClass("creatingClasses1.Robot")
} catch (e: ClassNotFoundException) {
throw AssertionError("Can't find the 'Robot' class in 'creatingClasses1' package")
}
val robot = robotClass.constructors.first().newInstance()
return Pair(robotClass, robot)
}
@Test(timeout = TIMEOUT)
fun test1() = testString("abc")
fun testRight() = testDirection("Right", 11)
@Test(timeout = TIMEOUT)
fun test2() = testString("Abc")
fun testLeft() = testDirection("Left", 37)
@Test(timeout = TIMEOUT)
fun test3() = testString("ABC")
fun testUp() = testDirection("Up", 4)
@Test(timeout = TIMEOUT)
fun test4() = testString("aBc")
@Test(timeout = TIMEOUT)
fun test5() = testString("abC")
}
fun testDown() = testDirection("Down", 8)
}

View File

@ -0,0 +1,11 @@
package creatingClasses4
fun main() {
val s = "Hello!"
println(s.toUpperCase())
println(s.toLowerCase())
}
/* Output:
HELLO!
hello!
*/

View File

@ -0,0 +1,8 @@
type: ide
files:
- name: src/Task.kt
visible: true
- name: test/Tests.kt
visible: false
feedback_link: |
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Introduction+to+Objects+%2F+Creating+Classes+%2F+Exercise2

View File

@ -0,0 +1,13 @@
## Mastering the IDE: Quick Documentation
The <span class="control">`Quick Documentation`</span> action produces
information about a symbol, such as a function or a class.
Press <span class="shortcut">&shortcut:QuickJavaDoc;</span> when the caret
is on a class or a function, and IntelliJ Idea will show you the available
documentation for this function or class.
Call <span class="control">`Quick Documentation`</span> action
for the `String` class and its `toUpperCase()` and `toLowerCase()` functions.
Press <span class="shortcut">&shortcut:EditorEscape;</span> to close the
quick documentation pop-up.

View File

@ -0,0 +1 @@
package creatingClasses4

View File

@ -0,0 +1,12 @@
package creatingClasses5
fun cap(s: String) = s.capitalize()
fun main() {
println(cap("hi!"))
println(cap("Hi!"))
}
/* Output:
Hi!
Hi!
*/

View File

@ -0,0 +1,12 @@
type: ide
files:
- name: src/Task.kt
visible: true
placeholders:
- offset: 48
length: 13
placeholder_text: ' '
- name: test/Tests.kt
visible: false
feedback_link: |
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Introduction+to+Objects+%2F+Creating+Classes+%2F+Exercise3

View File

@ -0,0 +1,2 @@
id: 399613
update_date: Wed, 03 Oct 2018 12:37:44 UTC

View File

@ -0,0 +1,9 @@
## Mastering the IDE: Quick Documentation in the Completion List
The <span class="control">`Quick Documentation`</span> shortcut
<span class="shortcut">&shortcut:QuickJavaDoc;</span>
can be used not only in the editor but in the code completion popup list as well.
Using this feature, find the function for `String` that upper-cases only its
first letter, or returns the original string if it's already starts with an
upper case letter.

View File

@ -0,0 +1,29 @@
package creatingClasses5
import org.junit.Assert
import org.junit.FixMethodOrder
import org.junit.Test
import org.junit.runners.MethodSorters
import util.TIMEOUT
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestCapitalize() {
private fun testString(s: String) {
Assert.assertEquals("""Wrong result for task("$s"):""", s.capitalize(), cap(s))
}
@Test(timeout = TIMEOUT)
fun test1() = testString("abc")
@Test(timeout = TIMEOUT)
fun test2() = testString("Abc")
@Test(timeout = TIMEOUT)
fun test3() = testString("ABC")
@Test(timeout = TIMEOUT)
fun test4() = testString("aBc")
@Test(timeout = TIMEOUT)
fun test5() = testString("abC")
}

View File

@ -3,3 +3,5 @@ content:
- Exercise 1
- Exercise 2
- Exercise 3
- Exercise 4
- Exercise 5