Finished TestScores

This commit is contained in:
2025-04-15 22:31:15 -05:00
parent e14806c444
commit aeb07e3c8a
2 changed files with 64 additions and 2 deletions

View File

@@ -5,7 +5,7 @@ import Ship.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// For testing q1 // For testing q1
/*
// Testing the rough version of SavingsAccount // Testing the rough version of SavingsAccount
SavingsAccount SA = new SavingsAccount(30, 2); SavingsAccount SA = new SavingsAccount(30, 2);
System.out.println(SA); System.out.println(SA);
@@ -13,7 +13,9 @@ public class Main {
System.out.println(SA); System.out.println(SA);
SA.withdraw(5); SA.withdraw(5);
System.out.println(SA); System.out.println(SA);
*/
// For testing q3 // For testing q3
Ship[] ships = new Ship[3]; Ship[] ships = new Ship[3];
@@ -26,5 +28,31 @@ public class Main {
} }
// For testing q5
int[] scores1 = {87, 21, 99, 100, 100, 65, 72, 0, 90, 81, 78};
int[] scores2 = {87, -21, 99, 100, 100, 65, 72, 0, 90, 81, 78};
int[] scores3 = {87, 21, 99, 101, 100, 65, 72, 0, 90, 81, 78};
TestScores TS1 = new TestScores(scores1);
TestScores TS2 = new TestScores(scores2);
TestScores TS3 = new TestScores(scores3);
try {
System.out.println("Average of Test Scores 1: " + TS1.getAverage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
System.out.println("Average of Test Scores 2: " + TS2.getAverage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
System.out.println("Average of Test Scores 3: " + TS3.getAverage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
} }
} }

View File

@@ -1,3 +1,37 @@
/**
* TestScores class will take an array of test Scores, and has a method to determine average
*/
public class TestScores { public class TestScores {
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 IllegalArgumentException 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 IllegalArgumentException("Invalid score " + x + ". Scores must be between 0 and 100");
}
else {
sum += x;
}
}
// Find average and return it
average = sum / scores.length;
return average;
}
} }