31 lines
780 B
Kotlin
31 lines
780 B
Kotlin
package drills.drill01.exercise4
|
|
|
|
import drills.drill01.exercise2.PrintService
|
|
import kotlinx.coroutines.GlobalScope
|
|
import kotlinx.coroutines.launch
|
|
import java.rmi.Remote
|
|
import java.rmi.RemoteException
|
|
import java.rmi.server.UnicastRemoteObject
|
|
|
|
interface Math : Remote {
|
|
@Throws(RemoteException::class)
|
|
fun getFactOf(n: Int): Long
|
|
|
|
@Throws(RemoteException::class)
|
|
fun getFactOf(n: Int, ps: PrintService)
|
|
}
|
|
|
|
class MathImpl : Math, UnicastRemoteObject() {
|
|
override fun getFactOf(n: Int): Long = fact(n)
|
|
|
|
override fun getFactOf(n: Int, ps: PrintService) {
|
|
GlobalScope.launch {
|
|
ps.print("The factorial of $n is ${fact(n)}")
|
|
}
|
|
}
|
|
|
|
private fun fact(n: Int): Long {
|
|
return if (n <= 1) 1 else n * fact(n - 1)
|
|
}
|
|
}
|