Challenge done during Section 7: Lambda Expressions, Collections and Generics.
- Make the following code compile:
fun main() { val joe = Person("Joe", "Jones", 45) val (fName, lName, age) = joe println("fName = $fName, lName = $lName, age = $age") } class Person(val firstName: String, val lastName: String, val age: Int)
- Using the following data, create an
ImmutableMap. EachPersonentry should have its last name as key.val joe = Person("Joe", "Jones", 45) val jane = Person("Jane", "Smith", 12) val mary = Person("Mary", "Wilson", 70) val john = Person("John", "Adams", 32) val jean = Person("Jean", "Smithson", 66)
- Without using an explicit loop, print out how many people have last names beginning with the letter "S".
- Without using an explicit loop and using the created
Map, create and print a list ofPairs. The first item in the pair should be the first name and the last item in the pair should be the last name. - Using the
alsofunction and the peopleMap, print out eachPerson's name and age, like in the following examples:Joe is 45 years old. Jane is 12 years old. - Without using an explicit loop, create a
list3which contains only elements present in bothlist1andlist2.val list1 = listOf(1, 4, 9, 15, 33) val list2 = listOf(4, 55, -83, 15, 22, 101)
- Do whatever you need to do to make
paper = regularPapercompile.val regularPaper = Box<Regular>() val paper = Box<Paper>() paper = regularPaper class Box<T> open class Paper class Regular : Paper() class Premium : Paper()