package calculatorFunctions
public interface ExamCalculationInterface {
def execute(examScores)
}
AverageScore.groovy
package calculatorFunctions
import ExamCalculationInterface;
class AverageScore implements ExamCalculationInterface {
@Override
def execute(examScores) {
def total = 0
for (int i = 0; i < examScores.size(); i++)
{
total += examScores[i]
}
total / examScores.size()
}
}
MinimumScore.groovy
package calculatorFunctions
class MinimumScore implements ExamCalculationInterface {
@Override
def execute(examScores) {
def lowest = 100
for (int i = 0; i<examScores.size(); i++)
{
if (examScores[i]< lowest)
{lowest= examScores[i]}
}
lowest
}
}
ExamScores.groovy
package ui
class ExamScores {
def scores = [72,43,29,96,93,33,49,32,44,45,44,29,60,61,66,66,59] as Integer[]
def analyse(closure) {
closure(scores)
}
def analyseWithPassmark(passmark, closure)
{
//do something here with passmark
closure(scores)
}
}
Main.groovy
package ui
import calculatorFunctions.*;
class Main {
static main(args) {
def examScores = new ExamScores()
def firstScore = { scores ->
scores[0]
}
def first = examScores.analyse(firstScore)
println "The first score was $first"
def averageScore = { scores ->
def total = 0
scores.each {total += it}
total / scores.size()
}
def lowestScore = { scores ->
def lowest = 100
scores.each {
if (it < lowest)
{lowest= it}
}
lowest
}
def passed = { scores ->
def result = 0
scores.each {
if (it > 50) {result++}
}
result
}
def average = examScores.analyse(averageScore)
def lowest = examScores.analyse(lowestScore)
def passedNumber = examScores.analyse(passed)
println "The average score was $average"
println "The lowerst score was $lowest"
println "$passedNumber passed the exam"
def numberOfCandidates = examScores.analyse {scores -> scores.size()}
println "There were $numberOfCandidates candidates."
def testPassMark = examScores.analyseWithPassmark (50) {scores -> scores.size()}
(20..30).step(2) {println it}
}
}
import calculatorFunctions.*;
class Main {
static main(args) {
def examScores = new ExamScores()
def firstScore = { scores ->
scores[0]
}
def first = examScores.analyse(firstScore)
println "The first score was $first"
def averageScore = { scores ->
def total = 0
scores.each {total += it}
total / scores.size()
}
def lowestScore = { scores ->
def lowest = 100
scores.each {
if (it < lowest)
{lowest= it}
}
lowest
}
def passed = { scores ->
def result = 0
scores.each {
if (it > 50) {result++}
}
result
}
def average = examScores.analyse(averageScore)
def lowest = examScores.analyse(lowestScore)
def passedNumber = examScores.analyse(passed)
println "The average score was $average"
println "The lowerst score was $lowest"
println "$passedNumber passed the exam"
def numberOfCandidates = examScores.analyse {scores -> scores.size()}
println "There were $numberOfCandidates candidates."
def testPassMark = examScores.analyseWithPassmark (50) {scores -> scores.size()}
(20..30).step(2) {println it}
}
}
The first score was 72
The average score was 54.1764705882
The lowerst score was 29
8 passed the exam
There were 17 candidates.
20
22
24
26
28
30
No comments:
Post a Comment