ProgramGuru

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

  1. Validate records

    Combine typed records, collection operations and deliberate validation into one school report.

    Validate records
  2. Sort and aggregate

    A data class groups each name and score, with an initialization requirement for valid marks.

    Sort and aggregate
  3. Format report

    Sorting puts Diya before Saira although the input order differs.

    Format report
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.

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 students

Follow 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

What changes if Saira’s score becomes 70?
Choose one answer. You can try again.

Answer and explanation

The ordered rows show Diya 80 and Saira 70; the total is 150 and the average is 75.0.

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: 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.

    Course overview →