Kotlin Class: Student class with promotion eligibility check
Kotlin Class: Exercise-4 with Solution
Write a Kotlin program that creates a class 'Student' with properties for name, age, and grade. Include a function to check if the student is eligible for promotion.
Sample Solution:
Kotlin Code:
class Student(val name: String, val age: Int, val grade: Int) {
fun isEligibleForPromotion(): Boolean {
return grade >= 8
}
}
fun main() {
val student1 = Student("Ema Luella", 16, 9)
val student2 = Student("Iclza Iqoqtux", 15, 7)
println("${student1.name} is eligible for promotion: ${student1.isEligibleForPromotion()}")
println("${student2.name} is eligible for promotion: ${student2.isEligibleForPromotion()}")
}
Sample Output:
Ema Luella is eligible for promotion: true Iclza Iqoqtux is eligible for promotion: false
Explanation:
In the above exercise -
- First we define a class "Student" with three properties: name, age, and grade. The class also includes a function isEligibleForPromotion() that checks if the student's grade is greater than or equal to 8. If the grade is 8 or higher, the function returns true; otherwise, it returns false.
- In the main() function, we create two instances of the Student class, student1 and student2, with different names, ages, and grades. Each student object is then checked for promotion eligibility by calling the isEligibleForPromotion() function.
Kotlin Editor:
Previous: Create and display Car information.
Next: Book class with display function.
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-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics