Kotlin Class: Employee class with details display
Kotlin Class: Exercise-8 with Solution
Write a Kotlin program that creates a class 'Employee' with properties for name, age, and designation. Include a function to display employee details.
Sample Solution:
Kotlin Code:
class Employee(
val name: String,
val age: Int,
val designation: String
) {
fun displayDetails() {
println("Name: $name")
println("Age: $age")
println("Designation: $designation")
}
}
fun main() {
val employee = Employee("Herais Lochlainn", 35, "Manager")
employee.displayDetails()
}
Sample Output:
Name: Herais Lochlainn Age: 35 Designation: Manager
Explanation:
In the above exercise -
- First we define a class "Employee" with three properties: name, age, and designation. The class includes a function "displayDetails()" that prints employee details, including name, age, and designation.
- In the "main()" function, we create an instance of the "Employee" class named employee with the name "Bryce Clark", age 30, and designation "Manager". We then call the displayDetails function on the employee object, which prints the employee details to the console.
Kotlin Editor:
Previous: Circle class with circumference calculation.
Next: Triangle class with perimeter calculation.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/kotlin-exercises/class/kotlin-class-exercise-8.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics