From aeb07e3c8a9fff51a3d86b1f6f5853e46ab9414d Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 15 Apr 2025 22:31:15 -0500 Subject: [PATCH] Finished TestScores --- src/Main.java | 32 ++++++++++++++++++++++++++++++-- src/TestScores.java | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 9070c29..75acb0e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,7 +5,7 @@ import Ship.*; public class Main { public static void main(String[] args) { // For testing q1 - /* + // Testing the rough version of SavingsAccount SavingsAccount SA = new SavingsAccount(30, 2); System.out.println(SA); @@ -13,7 +13,9 @@ public class Main { System.out.println(SA); SA.withdraw(5); System.out.println(SA); - */ + + + // For testing q3 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()); + } } } \ No newline at end of file diff --git a/src/TestScores.java b/src/TestScores.java index c604dc0..3094e4f 100644 --- a/src/TestScores.java +++ b/src/TestScores.java @@ -1,3 +1,37 @@ +/** + * TestScores class will take an array of test Scores, and has a method to determine average + */ 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; + } }