Build a validated student report
Build a validated student report
Combine typed records, collection operations and deliberate validation into one school report. Requirements are concrete: reject impossible scores, show students in an explicit order, calculate a fractional average and handle an empty register before dividing.
What you’ll learn
- Validate records, sort them and calculate an average.
- Handle an empty register before division.
Build a validated student report in the example
Validate records
Combine typed records, collection operations and deliberate validation into one school report.
Validate recordsSort and aggregate
A data class groups each name and score, with an initialization requirement for valid marks.
Sort and aggregateFormat report
Sorting puts Diya before Saira although the input order differs.
Format report
Understand build a validated student report
A data class groups each name and score, with an initialization requirement for valid marks. The report function sorts records and returns formatted text. Separating that result from printing makes the calculation reusable and easier to test independently.
Follow build a validated student report
Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.
Kotlin/JVM · complete program
data class Student(val name: String, val score: Int) {
init { require(score in 0..100) { "Invalid score" } }
}
fun report(students: List<Student>): String {
if (students.isEmpty()) return "No students"
val lines = students.sortedBy { it.name }.map { "${it.name}: ${it.score}" }
val total = students.sumOf { it.score }
val average = total.toDouble() / students.size
return (lines + "Average: $average").joinToString("\n")
}
fun main() {
println(report(listOf(Student("Saira", 90), Student("Diya", 80))))
println(report(emptyList()))
}Expected output
Diya: 80
Saira: 90
Average: 85.0
No studentsFollow the execution
Sorting puts Diya before Saira although the input order differs. Their total is 170 and the Double average is 85.0. The empty case returns No students before division. Invalid construction is rejected rather than inventing a score.
Try this in the Kotlin compiler by pasting the complete program.
Move the model into an application
Keep calculation logic independent before adding Android, server or multiplatform interfaces. Android activities and Compose state have framework lifecycle rules; Ktor and other server frameworks introduce their own request and deployment contracts. Use official application guides as the next step rather than assuming this console example covers them.
Practice: Build a validated student report
Your task: What changes if Saira’s score becomes 70?
Kotlin/JVM · complete program
data class Student(val name: String, val score: Int) {
init { require(score in 0..100) { "Invalid score" } }
}
fun report(students: List<Student>): String {
if (students.isEmpty()) return "No students"
val lines = students.sortedBy { it.name }.map { "${it.name}: ${it.score}" }
val total = students.sumOf { it.score }
val average = total.toDouble() / students.size
return (lines + "Average: $average").joinToString("\n")
}
fun main() {
println(report(listOf(Student("Saira", 90), Student("Diya", 80))))
println(report(emptyList()))
}Worked answer
The ordered rows show Diya 80 and Saira 70; the total is 150 and the average is 75.0. The empty-list message remains unchanged.
Build a validated student report: the important boundary
Names are not guaranteed unique IDs, and an absent mark needs its own domain representation. Real persistence, authentication and UI lifecycle are additional requirements. Console output does not create an Android application.
Check your understanding
Build a validated student report: what to keep in mind
The ordered rows show Diya 80 and Saira 70; the total is 150 and the average is 75.0. The empty-list message remains unchanged. Names are not guaranteed unique IDs, and an absent mark needs its own domain representation.
Further reading: official Kotlin documentation for this topic.
Reaching the recap and completing the quizzes are separate milestones.