Computer scienceProgramming languagesKotlinObject-oriented programmingObject-oriented programming features

Building your own delegate

Full name

Report a typo

You have a program that reads the full name of a person. Your task is to create a class named Person with three String properties: firstName, lastName, and fullName. The firstName and lastName properties should be simple string variables, but the fullName property should be a delegated property.

The delegate class should ensure that whenever the fullName property is set, it splits the input into the first and last names and assigns them to the firstName and lastName properties, respectively. If the fullName is accessed, it should return the firstName and lastName concatenated together with a space in between.

All packages you need to solve this challenge are already imported.

Sample Input 1:

Harry Potter

Sample Output 1:

First name: Harry
Last name: Potter
Full name: Harry Potter
Write a program in Kotlin
class Person {
var firstName: String = ""
var lastName: String = ""

// Declare here the delegated property: fullName

}

// Implement the property delegate
___

Create a free account to access the full topic