// // simplified program to compute letter grade based on: // maximum points (values "hardcoded" in program) // and student's points (entered by user) // // version using function to avoid repetitive code // the next line is needed to avoid warnings with the newest Scala // "comment it out" (insert a //) if you compile with an earlier version import scala.io.StdIn._ val MaxExam1 = 100.0 val MaxExam2 = 200.0 val MaxHomework = 200.0 val exam1 = promptForNonNegativeDouble("exam1", MaxExam1) val exam2 = promptForNonNegativeDouble("exam2", MaxExam2) val homework = promptForNonNegativeDouble("homework", MaxHomework) val average = (exam1 + exam2 + homework) / (MaxExam1 + MaxExam2 + MaxHomework) println("average = " + average) if (average >= .90) { println("A- or better") } else if (average >= .80) { println("B- or better") } else if (average >= .70) { println("C- or better") } else if (average >= .60) { println("D- or better") } else { println("sorry, F") } // prompt for input, using name and perfectScore // exit if value is < 0 def promptForNonNegativeDouble(name:String, perfectScore:Double) : Double = { println("enter points for " + name + " (max " + perfectScore + ")") val in = readDouble if (in < 0) { println("value must be >= 0") sys.exit(0) } in }