From e315a3c42413a65cba55511f4af9c5d3fae14c1b Mon Sep 17 00:00:00 2001 From: allemangD Date: Thu, 11 Oct 2018 23:05:31 -0400 Subject: [PATCH] complete grade calcuator --- .idea/artifacts/resources.xml | 8 ++++++ README.md | 12 +++++++++ res/sample.grades | 50 +++++++++++++++++++++++++++++++++++ src/three_feature.kt | 49 +++++++++++++++++++++++++++++----- 4 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 .idea/artifacts/resources.xml create mode 100644 README.md create mode 100644 res/sample.grades diff --git a/.idea/artifacts/resources.xml b/.idea/artifacts/resources.xml new file mode 100644 index 0000000..b9ec937 --- /dev/null +++ b/.idea/artifacts/resources.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b5033b6 --- /dev/null +++ b/README.md @@ -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 + diff --git a/res/sample.grades b/res/sample.grades new file mode 100644 index 0000000..a70e9e4 --- /dev/null +++ b/res/sample.grades @@ -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 diff --git a/src/three_feature.kt b/src/three_feature.kt index 02f9713..d1b292d 100644 --- a/src/three_feature.kt +++ b/src/three_feature.kt @@ -1,11 +1,48 @@ -import java.util.* +import java.io.File + fun main(args: Array) { - 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>() - 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()` + + // 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 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") + } } \ No newline at end of file