It is a program using array to hold running paths. fun printMaxMinPath() is to print out the longest and shortest paths. My questions are:
how can i get access to the length of Runningpath and find the max and min? if i write the code as below, line 19, 20 won't work as "length" is unsolved reference. but i don't know how should i write, (or what keywords to google).
how can i get access to the name and spot of longest and shortest Runningpath to print them inside
printMaxMinPath()? $min.name won't work
Any hints is very appreciated. Thank you for the kindness.
my codes with compile errors are as below:
class Runningpath(val name: String, val length: Int, val spot: String){
override fun toString(): String= "The path $name ($length m) is near $spot"
fun printMaxMinPath (p: Array<Runningpath> , min:Int, max:Int) {
println("The shortest path is with $min m. It is the path $name near $spot")
println("The longest path is with $max m. It is the path $name near $spot")
}
}
fun main() {
println("Warming up")
val input1 = Runningpath("in Forest", 2000, "some houses")
val input2 = Runningpath("at lake", 1500, "a school")
val input3 = Runningpath("in mountain", 2800, "under sky")
val path = arrayOf(input1, input2, input3)
line19 val max = path.length.max()
20 val min = path.length.min()
input1.printMaxMinPath(path, min, max)
}