Added two more exercises to 'Creating classes'
This commit is contained in:
parent
1b8519f9f4
commit
9867257f37
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
id: 399609
|
||||
update_date: Wed, 03 Oct 2018 12:37:41 UTC
|
|
@ -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.
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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!
|
||||
*/
|
|
@ -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
|
||||
|
|
|
@ -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.
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
*/
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package creatingClasses4
|
||||
|
||||
fun main() {
|
||||
val s = "Hello!"
|
||||
println(s.toUpperCase())
|
||||
println(s.toLowerCase())
|
||||
}
|
||||
/* Output:
|
||||
HELLO!
|
||||
hello!
|
||||
*/
|
|
@ -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
|
|
@ -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.
|
|
@ -0,0 +1 @@
|
|||
package creatingClasses4
|
|
@ -0,0 +1,12 @@
|
|||
package creatingClasses5
|
||||
|
||||
fun cap(s: String) = s.capitalize()
|
||||
|
||||
fun main() {
|
||||
println(cap("hi!"))
|
||||
println(cap("Hi!"))
|
||||
}
|
||||
/* Output:
|
||||
Hi!
|
||||
Hi!
|
||||
*/
|
|
@ -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
|
|
@ -0,0 +1,2 @@
|
|||
id: 399613
|
||||
update_date: Wed, 03 Oct 2018 12:37:44 UTC
|
|
@ -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.
|
|
@ -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")
|
||||
}
|
|
@ -3,3 +3,5 @@ content:
|
|||
- Exercise 1
|
||||
- Exercise 2
|
||||
- Exercise 3
|
||||
- Exercise 4
|
||||
- Exercise 5
|
||||
|
|
Loading…
Reference in New Issue