I want to create a UML class diagram to represent a Kotlin class. I have come across a few options and came up with two alternatives. However, I'm unsure which of the two is the most appropriate and accurate. Can someone help me determine which UML diagram is correct or better for this Kotlin class?
Here is the Kotlin class I want to represent:
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)"
}
Option 1: With getters and setters
All the class' attributes are private and the public ones get public getters/setters:
Option 2: using some additional UML notations
I used these two answers on Stackoverflow for the <<get/set>>
annotations (see here) and {readOnly}
attribute (see here).