In: Computer Science
fun main() {
val stringResult = getResult("Kotlin")
val intResult = getResult(100)
// TODO 2
println()
}
// TODO 1
fun <T> getResult(args: T): Int {
return 0
}
TODO 1:
Create a new generics function with the following conditions:
The function name is getResult.
Has one type of parameter.
If the attached argument is of type Int, then the value returned is
the value of the argument multiplied by 5.
If the attached argument is of type String, the value returned is a
character length.
If the attached argument is of type other than Int and String, then
the returned value is 0.
TODO 2:
Add functions to print the values of the stringResult and intResult
variables in the console.
Hi,
I have written two methods one for TODO1, another for TODO2.
TODO2 method name is : printIntResult
it will just print the argument passed in it.
TODO1 it is generic method
it can take any type of argument as you can see code below.
Please find the attached code and output also , hope it will help you.
class TODO { // TODO 2 |
when you will run this code :
output will be below image :