From 22c38737b9dce869ffe6800000feaa078ae000d4 Mon Sep 17 00:00:00 2001
From: Dao <comnuoc@users.noreply.gitlab.utu.fi>
Date: Sun, 23 Jun 2024 00:14:50 +0300
Subject: [PATCH] feat(part2): Exercise 1.

---
 part_2/exercise_1/.gitignore         | 30 +++++++++++
 part_2/exercise_1/exercise_1.iml     | 11 ++++
 part_2/exercise_1/src/Exercise1.java | 75 ++++++++++++++++++++++++++++
 part_2/exercise_1/src/Main.java      |  6 +++
 4 files changed, 122 insertions(+)
 create mode 100644 part_2/exercise_1/.gitignore
 create mode 100644 part_2/exercise_1/exercise_1.iml
 create mode 100644 part_2/exercise_1/src/Exercise1.java
 create mode 100644 part_2/exercise_1/src/Main.java

diff --git a/part_2/exercise_1/.gitignore b/part_2/exercise_1/.gitignore
new file mode 100644
index 0000000..d578053
--- /dev/null
+++ b/part_2/exercise_1/.gitignore
@@ -0,0 +1,30 @@
+### IntelliJ IDEA ###
+/.idea/
+out/
+!**/src/main/**/out/
+!**/src/test/**/out/
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+bin/
+!**/src/main/**/bin/
+!**/src/test/**/bin/
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/part_2/exercise_1/exercise_1.iml b/part_2/exercise_1/exercise_1.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/part_2/exercise_1/exercise_1.iml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
\ No newline at end of file
diff --git a/part_2/exercise_1/src/Exercise1.java b/part_2/exercise_1/src/Exercise1.java
new file mode 100644
index 0000000..319ddd7
--- /dev/null
+++ b/part_2/exercise_1/src/Exercise1.java
@@ -0,0 +1,75 @@
+import java.util.ArrayList;
+
+public class Exercise1 {
+    class EmptyArray extends Exception {
+    }
+
+    class ArrayNegativeNumbersException extends Exception {
+        public ArrayNegativeNumbersException(String message) {
+            super(message);
+        }
+    }
+
+    public void main() {
+        printAvg(new int[]{1, -2, -3, 4});
+        printAvg(new int[]{1, 2, 3});
+        printAvg(new int[]{-1, 2, 3, -4});
+    }
+
+    public float avg(int[] nums) throws EmptyArray, ArrayNegativeNumbersException { // 1
+        int sum = 0;
+        ArrayList<String> errors = new ArrayList<String>();
+
+        if (nums == null || nums.length == 0)
+            throw new EmptyArray(); // 2
+
+        for (int i = 0; i < nums.length; i++) {
+            int n = nums[i];
+
+            if (n < 0) {
+                errors.add(getErrorMessage(i, n));
+            } else {
+                sum += n;
+            }
+        }
+
+        if (!errors.isEmpty()) {
+            throw new ArrayNegativeNumbersException(String.join("\n", errors));
+        }
+
+        return sum / nums.length;
+    }
+
+    private void printAvg(int[] nums) {
+        Float result;
+
+        try {
+            result = avg(nums);
+
+            System.out.println(result);
+        } // 3
+        catch (EmptyArray e) {
+        } // 3
+        catch (ArrayNegativeNumbersException e) {
+            System.out.println(e.getMessage());
+        }
+    }
+
+    private String getErrorMessage(int index, int number) {
+        int position = index + 1;
+
+        String positionSuffix = switch (position) {
+            case 1 -> "st";
+            case 2 -> "nd";
+            case 3 -> "rd";
+            default -> "th";
+        };
+
+        return String.format(
+                "The %d%s number %d in your array is invalid",
+                position,
+                positionSuffix,
+                number
+        );
+    }
+}
diff --git a/part_2/exercise_1/src/Main.java b/part_2/exercise_1/src/Main.java
new file mode 100644
index 0000000..7cd5395
--- /dev/null
+++ b/part_2/exercise_1/src/Main.java
@@ -0,0 +1,6 @@
+public class Main {
+    public static void main(String[] args) {
+        Exercise1 exercise = new Exercise1();
+        exercise.main();
+    }
+}
\ No newline at end of file
-- 
GitLab