complete grade calcuator

This commit is contained in:
2018-10-11 23:05:31 -04:00
parent f1919677de
commit e315a3c424
4 changed files with 113 additions and 6 deletions

8
.idea/artifacts/resources.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact build-on-make="true" name="resources">
<output-path>$PROJECT_DIR$/out</output-path>
<root id="root">
<element id="dir-copy" path="$PROJECT_DIR$/res" />
</root>
</artifact>
</component>

12
README.md Normal file
View File

@@ -0,0 +1,12 @@
# Term Project Part I
## Three Programs
Create a program (or programs) which show how the language you picked (Kotlin) works with the following:
1. I/O
1. Data Structures
1. Control Structures
## Report Card Generator

50
res/sample.grades Normal file
View File

@@ -0,0 +1,50 @@
advanced calculus of one variable: 61
advanced calculus of one variable: 59
programming languages: 89
programming languages: 74
parallel and distributed computing: 90
parallel and distributed computing: 71
programming languages: 96
programming languages: 92
programming languages: 95
parallel and distributed computing: 97
parallel and distributed computing: 89
advanced calculus of one variable: 82
advanced calculus of one variable: 74
software architecture: 53
software architecture: 82
programming languages: 61
programming languages: 93
parallel and distributed computing: 91
parallel and distributed computing: 81
software architecture: 57
advanced calculus of one variable: 85
programming languages: 99
programming languages: 89
differential equations: 59
differential equations: 97
software architecture: 56
parallel and distributed computing: 89
parallel and distributed computing: 66
differential equations: 58
software architecture: 98
software architecture: 66
advanced calculus of one variable: 79
differential equations: 53
advanced calculus of one variable: 96
parallel and distributed computing: 100
differential equations: 57
differential equations: 70
differential equations: 59
parallel and distributed computing: 93
advanced calculus of one variable: 71
advanced calculus of one variable: 99
differential equations: 100
differential equations: 74
software architecture: 93
advanced calculus of one variable: 93
differential equations: 86
programming languages: 61
software architecture: 79
software architecture: 76
software architecture: 99

View File

@@ -1,11 +1,48 @@
import java.util.*
import java.io.File
fun main(args: Array<String>) {
val in1 = readLine() ?: ""
// File I/O is easily done with `File()` constructor
val file = File("res/sample.grades")
val reader = Scanner(System.`in`)
val in2 = reader.nextInt()
// Maps and Lists are, by default, immutable.
// Specify our data structure to be mutable at both levels
val courses = mutableMapOf<String, MutableList<Int>>()
println(in1)
print(in2)
// File supports line iteration with forEachLine, which accepts a code block.
// `it` is the iteration variable in the code block
file.forEachLine {
// It is possible to unpack iterables into multiple variables
// This is very brittle, though, if improperly formatted
val (rawCourse, rawGrade) = it.split(":")
val course = rawCourse.trim()
val grade = rawGrade.trim().toInt() // string conversion is easy with helper methods like `toInt()` and `toIntOrNull()`
// collection testing is easy with `in` and `!in` operators
if (course !in courses)
courses[course] = mutableListOf() // implicit type arguments are passed: `mutableListOf<Int>()`
// use `!!` to assert the hash lookup will not be null. We just added it to the map.
courses[course]!!.add(grade)
}
// Iteration over maps is easy when combined with iterable unpacking
for ((course, grades) in courses) {
val average = grades.average() // List<Int> has helper methods such as `sum()` and `average()`
// use a `when` block as an alternative to case. Supports rich pattern matching,
// but we just use it here to check which grade range we're in
val letter = when {
average > 90 -> "A"
average > 80 -> "B"
average > 70 -> "C"
average > 70 -> "D"
else -> "F"
}
// input and output from console are as easy as `println()` and `readline()`
println("$course:\n\t$letter ($average)\n")
}
}