Rewrite the Java code snippets below in Kotlin. With the correct answers you can win one of our online trainings of your choice. Kotlin is one of them.

The online training is made by one of our partners, Instruqt. Instruqt brings tech learning to the new generation of engineers. They believe that by cultivating a world of better learners, thinkers, and problem-solvers, organizations are well-positioned to thrive. 

Let's start the challenge!

Challenge 1: 

public class Person{
   private final String name;
  
private final int age;
  
public Person (String name, int age) {
       this.name = name;
      
this.age = age;
  
}
   public Person (String name) {
       this.name = name;
      
this.age = 0 ;
  
}
   public int getAge ( ) {
       return age;
  
}
   public String getName ( ) {
       return name;
  
}
}

Tip: Check out Kotlin's class syntax and take a look at default arguments. 

Challenge 2: 

File file = new File ("/any/dir/somwhere");
String firstFileInParentFolder =
       file.getParentFile ( )  !=
null &&
               f.getParentFile( ) . listFiles ( ) !=
null &&
               f.getParentFile( ) . listFiles ( ) [
0] != null ?
               f.getParentFile( ) .
listFiles ( ) [0] . getName ( ) : "unknown";

Tip: Take a look at Kotlin's null-safety features. 

Challenge 3: 

Map<Character, Integer> characterCount = List.of ("Count", "us", "all")
       .stream( )
       .flatMap ( p -> p.chars( ).mapToObj(c -> (
char) c ) )
       .collect ( Collectors.groupingBy ( i -> i ) )
       .entrySet( )
       .stream( )
       .collect ( Collectors.toMap ( Map.Entry::getKey, e -> e.getValue ( ) . size ( ) ) ) ;

Tip: Check out Kotlin's collection methods and higher order functions, function syntax and primitive types. 

Use the form on the right hand side 👉to fill in your answers. You can submit your answers until the 30th of november. The winner will be contacted by e-mail.

 

instruqt_avatar_500x500_logo_purple@2x