diff --git a/src/main/java/fi/utu/tech/ooj/exercise1/teht2/Calculator.java b/src/main/java/fi/utu/tech/ooj/exercise1/teht2/Calculator.java index 45a7c659ce90c96b365ca6cf600cfdfacbe5fac2..d80b1fefc34e3ef5f70f13b9e9b9e9d2705643cb 100644 --- a/src/main/java/fi/utu/tech/ooj/exercise1/teht2/Calculator.java +++ b/src/main/java/fi/utu/tech/ooj/exercise1/teht2/Calculator.java @@ -1,11 +1,50 @@ package fi.utu.tech.ooj.exercise1.teht2; +import java.util.ArrayList; +import java.util.List; + +class NegativeArrayValuesException extends Exception { + private List<Integer> invalidIndices; + private List<Integer> invalidValues; + + public NegativeArrayValuesException(List<Integer> indices, List<Integer> values) { + this.invalidIndices = indices; + this.invalidValues = values; + } + + public List<Integer> getInvalidIndices() { + return invalidIndices; + } + + public List<Integer> getInvalidValues() { + return invalidValues; + } +} + public class Calculator { - public static float avg(int[] nums) throws EmptyArrayException { + public static float avg(int[] nums) throws EmptyArrayException, NegativeArrayValuesException { int sum = 0; - if (nums == null || nums.length == 0) + if (nums == null || nums.length == 0) { throw new EmptyArrayException(); - for(int n: nums) sum += n; - return sum / nums.length; + } + + List<Integer> invalidIndices = new ArrayList<>(); + List<Integer> invalidValues = new ArrayList<>(); + + for (int i = 0; i < nums.length; i++) { + if (nums[i] < 0) { + invalidIndices.add(i + 1); + invalidValues.add(nums[i]); + } else { + sum += nums[i]; + } + } + + if (!invalidIndices.isEmpty()) { + throw new NegativeArrayValuesException(invalidIndices, invalidValues); + } + + // Palautetaan keskiarvo + return (float) sum / nums.length; } -} +} \ No newline at end of file diff --git a/src/main/java/fi/utu/tech/ooj/exercise1/teht2/Teht1.java b/src/main/java/fi/utu/tech/ooj/exercise1/teht2/Teht1.java index 2dc6408d3cb2773c749731bcc39642581763eb68..b466f49acb9a847ebe58a9202a7ebec0e4aaf9fe 100644 --- a/src/main/java/fi/utu/tech/ooj/exercise1/teht2/Teht1.java +++ b/src/main/java/fi/utu/tech/ooj/exercise1/teht2/Teht1.java @@ -2,19 +2,22 @@ package fi.utu.tech.ooj.exercise1.teht2; public class Teht1 { public static void SuoritaTeht1() { - //Tulostetaan info-rivi, jotta suoritusta on helpompi seurata System.out.println("Suoritetaan Tehtävä 1"); - int[] nums = new int[] { }; + int[] nums = new int[] {1, -2, -3, 4}; Float result = -1f; + try { result = Calculator.avg(nums); - } - catch(EmptyArrayException e) { - System.out.println("Oho, tapahtui hassuja"); + System.out.println(result); + } catch (EmptyArrayException e) { + System.out.println("Oho, tapahtui hassuja. Taulukko on tyhjä."); + } catch (NegativeArrayValuesException e) { + for (int i = 0; i < e.getInvalidIndices().size(); i++) { + System.out.println("Antamasi taulukon " + e.getInvalidIndices().get(i) + ". luku " + e.getInvalidValues().get(i) + " on virheellinen"); + } } - //Tulostetaan info-rivi, jotta suoritusta on helpompi seurata System.out.println("Tehtävä 1 suoritus päättyi"); } -} +} \ No newline at end of file