0

It is a program using array to hold running paths. fun printMaxMinPath() is to print out the longest and shortest paths. My questions are:

  1. 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).

  2. 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)                                                            
        }                                                                                                     
                                             

                                                             
                                      

                     

                         

1 Answer 1

1

You can use maxBy and minBy from the standard kotlin library to get object having maximum and minimum length, and then use their properties to print their values out.

data class Runningpath(val name: String, val length: Int, val spot: String)

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 paths = arrayOf(input1, input2, input3)

    printMaxMinPath(paths)
}

fun printMaxMinPath(paths: Array<RunningPath>) {
    val max = path.maxBy { it.length }
    val min = path.minBy { it.length }
    println("The shortest path is with ${min.length} m. It is the path ${min.name} near ${min.spot}")
    println("The longest path is with ${max.length} m. It is the path ${max.name} near ${max.spot}")
}

Edit: The minBy and maxBy may return null if the array provided was null. You have to safe assert them, you can do it before printing anything as follows.

fun printMaxMinPath(paths: Array<RunningPath>) {
    val max = path.maxBy { it.length }
    val min = path.minBy { it.length }
    min?.run { println("The shortest path is with $length m. It is the path $name near $spot") }
    max?.run { println("The longest path is with $length m. It is the path $name near $spot") }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Error message (Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Runningpath?) with the last two printlin, but can be fixed with ${min?.length}. may i ask, do you know why Runningpath is nullable type? it seems that i never write any ?. with Runningpath. Thanks for your time and kindness, it helps me a lot.
@user12595983 it'll return null when there is no element in the array given.
Good, thanks! just a reminder abt typo, val path (at array), and $name (last 2 codes)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.