I have implemented the following UML in Kotlin, and I am now considering whether I should explicitly implement the methods getName()
, getAddress()
, and setAddress()
in my code, as they are mentioned in the UML. I know that Kotlin provides implicit generation of getters and setters for class properties.
Since getName()
, getAddress()
, and setAddress()
methods don't have any additional logic, I considered using implicit getters and setters provided by Kotlin. However, since these methods are explicitly mentioned in the UML, I am unsure whether I should proceed with the implicit generation or explicitly implement them in my code.
Should i explicitly implement these methods or continue using the implicit getters and setters provided by Kotlin?
For more details, this is my Kotlin class based on the UML that I have implemented using the implicit getter and setter provided by Kotlin.
class Student(val name: String, var address: String) {
private var numCourses: Int = 0
private val courses: Array<String> = Array(MAX_COURSES) { "" }
private val grades: IntArray = IntArray(MAX_COURSES)
companion object {
const val MAX_COURSES = 30
}
fun printGrades() {
print(name)
for (i in 0 until numCourses) {
print(" ${courses[i]}:${grades[i]}, ")
}
println()
}
fun calculateAverageGrade() = grades.sum().toDouble() / grades.size
fun addCourseGrade(course: String, grade: Int) {
require(numCourses < MAX_COURSES) {
"A student cannot take more than $MAX_COURSES courses"
}
require(grade in 0..100) {
"Grade must be between 0 and 100"
}
courses[numCourses] = course
grades[numCourses] = grade
numCourses++
}
override fun toString() = "name($address)"
}