initial
This commit is contained in:
5
src/InvalidTestScore.java
Normal file
5
src/InvalidTestScore.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class InvalidTestScore extends IllegalArgumentException {
|
||||||
|
public InvalidTestScore(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
56
src/Main.java
Normal file
56
src/Main.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Main for Q2
|
||||||
|
// Create the array of 5 testScore objects to be serialized later
|
||||||
|
TestScores[] testScoresArray = new TestScores[5];
|
||||||
|
testScoresArray[0] = new TestScores(new int[]{15, 24, 44, 30});
|
||||||
|
testScoresArray[1] = new TestScores(new int[]{69, 69, 55, 88, 92});
|
||||||
|
testScoresArray[2] = new TestScores(new int[]{82, 75, 89, 90, 0});
|
||||||
|
testScoresArray[3] = new TestScores(new int[]{72, 87, 92, 92, 94});
|
||||||
|
testScoresArray[4] = new TestScores(new int[]{100,100,100,100,100,100,100});
|
||||||
|
|
||||||
|
for(TestScores testScore : testScoresArray) {
|
||||||
|
System.out.println(testScore.getAverage());
|
||||||
|
}
|
||||||
|
System.out.println("\n");
|
||||||
|
String filename = "testScores.txt";
|
||||||
|
// Serialize array to file
|
||||||
|
try{
|
||||||
|
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(fileOut);
|
||||||
|
out.writeObject(testScoresArray);
|
||||||
|
System.out.println("Test Scores written to " + filename);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deserialize objects from file
|
||||||
|
TestScores[] deserializedScores = null;
|
||||||
|
|
||||||
|
try{
|
||||||
|
FileInputStream fileIn = new FileInputStream(filename);
|
||||||
|
ObjectInputStream in = new ObjectInputStream(fileIn);
|
||||||
|
deserializedScores = (TestScores[]) in.readObject();
|
||||||
|
System.out.println("Test Scores deserialized from " + filename);
|
||||||
|
}
|
||||||
|
//TODO: FIX THESE CATCH CLAUSES
|
||||||
|
catch(IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(TestScores testScore : deserializedScores) {
|
||||||
|
System.out.println(testScore.getAverage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
42
src/TestScores.java
Normal file
42
src/TestScores.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import java.io.Serial;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TestScores class will take an array of test Scores, and has a method to determine average
|
||||||
|
*/
|
||||||
|
public class TestScores implements Serializable {
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
int[] scores; // The array of test scores
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor taking array of test scores
|
||||||
|
* @param scores The array of scores
|
||||||
|
*/
|
||||||
|
public TestScores(int[] scores) {
|
||||||
|
this.scores = scores;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to return average of test scores. Throws InvalidTestScore if a score is
|
||||||
|
* negative or over 100
|
||||||
|
* @return Average of the scores array
|
||||||
|
*/
|
||||||
|
public double getAverage() {
|
||||||
|
double sum = 0; // Sum of all the scores
|
||||||
|
double average; // Average to be returned
|
||||||
|
for(int x : scores) {
|
||||||
|
// If score invalid, throw exception
|
||||||
|
if(x > 100 || x < 0) {
|
||||||
|
throw new InvalidTestScore("Invalid score " + x + ". Scores must be between 0 and 100");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sum += x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Find average and return it
|
||||||
|
average = sum / scores.length;
|
||||||
|
return average;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user