-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestScores.java
More file actions
51 lines (48 loc) · 1.33 KB
/
TestScores.java
File metadata and controls
51 lines (48 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//Author: Matthew Bubb
//Date: 21st of September, 2017
/**
* The TestScores class is a class that provides methods to ensure that an array of test
* scores will only have scores greater than zero and less than one hundred. The TestScores
* class will throw an InvalidTestScore exception if it detects an invalid entry in the array.
*/
public class TestScores
{
double[] scores;
double invalid;
int element;
/**
* Constructor:
* Accepts a double array of test scores as its argument and detects any score
* less than zero or greater than one hundred. If this is detected it will
* throw an InvalidTestScore exception.
* @param scoreArray an array of scores
* @throws InvalidTestScore exception to handle the invalid score
*/
public TestScores(double[] scoreArray) throws InvalidTestScore
{
scores = scoreArray;
for (int i = 0; i< scores.length; i++)
{
if (scores[i] < 0 || scores[i] > 100)
{
invalid = scores[i];
element = i;
throw new InvalidTestScore(element, invalid);
}
}
}
/**
* getAverage:
* A method that determines the average of test scores
* @return average of test scores
*/
public double getAverage()
{
double average = 0;
double sum = 0;
for(int i = 0; i< scores.length; i++)
sum = sum + scores[i];
average = sum / scores.length;
return average;
}
}