๐ŸŽ“ A1Learner
๐Ÿ”

Kotlin Gradebook Project

A gradebook is a small program that stores student scores and reports a grade for each student. The program combines data classes, lists, functions, and loops.

Students in a list

Student is a data class with a name and a score. The list holds three students and the prints show the first student and the count.

data class Student(val name: String, val score: Int)

fun main() {
    val students = listOf(Student("Ana", 90), Student("Bo", 75), Student("Cy", 82))
    println(students[0])
    println(students.size)
}

Turning a score into a grade

The grade function tests the score from the top. The three calls land in three different branches.

fun grade(score: Int): String {
    return when {
        score >= 90 -> "A"
        score >= 80 -> "B"
        score >= 70 -> "C"
        else -> "F"
    }
}

fun main() {
    println(grade(90))
    println(grade(75))
    println(grade(50))
}

One report line per student

The loop visits each student in order. Each line shows the name, the score, and the grade.

data class Student(val name: String, val score: Int)

fun grade(score: Int): String {
    return when {
        score >= 90 -> "A"
        score >= 80 -> "B"
        score >= 70 -> "C"
        else -> "F"
    }
}

fun main() {
    val students = listOf(Student("Ana", 90), Student("Bo", 75), Student("Cy", 82))
    for (student in students) {
        println("${student.name}: ${student.score} (${grade(student.score)})")
    }
}

Average and top score

One loop adds every score into total and keeps the largest score in top. The prints show the average first and the top score second.

data class Student(val name: String, val score: Int)

fun main() {
    val students = listOf(Student("Ana", 90), Student("Bo", 75), Student("Cy", 82))
    var total = 0
    var top = 0
    for (student in students) {
        total += student.score
        if (student.score > top) {
            top = student.score
        }
    }
    println("Average is ${total / students.size}")
    println("Top score is $top")
}

Example

data class Student(val name: String, val score: Int)

fun grade(score: Int): String {
    return when {
        score >= 90 -> "A"
        score >= 80 -> "B"
        score >= 70 -> "C"
        else -> "F"
    }
}

fun main() {
    val students = listOf(Student("Ana", 90), Student("Bo", 75), Student("Cy", 82), Student("Dee", 64))
    for (student in students) {
        println("${student.name}: ${student.score} (${grade(student.score)})")
    }
    var total = 0
    var top = 0
    for (student in students) {
        total += student.score
        if (student.score > top) {
            top = student.score
        }
    }
    println("Average is ${total / students.size}")
    println("Top score is $top")
}

Key points

  • A data class holds the name and score of one student
  • A list holds all the students in order
  • A function turns one score into one grade with when
  • A loop prints one report line for each student
  • A class average adds all scores and divides by the number of students
  • A top score search keeps the largest score seen so far

In this example, the first loop prints one report line for each of the four students. A second loop adds the scores into a total of 311 and keeps 90 as the top score. The average print shows 77 because Int division of 311 by 4 drops the fraction.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Kotlin Playground

Exercise

Which grade does the grade function give for a score of 82?