Przeglądaj źródła

square array done

Jennifer Chao 6 lat temu
rodzic
commit
3a4604fc36

+ 29
- 1
src/main/java/rocks/zipcode/io/quiz3/arrays/SquareArrayAnalyzer.java Wyświetl plik

@@ -5,6 +5,34 @@ package rocks.zipcode.io.quiz3.arrays;
5 5
  */
6 6
 public class SquareArrayAnalyzer {
7 7
     public static Boolean compare(Integer[] input, Integer[] squaredValues) {
8
-        return null;
8
+        Boolean[] allFlags = new Boolean[input.length];
9
+
10
+        Integer[] inputSquared = squareEachValue(input);
11
+
12
+        for (Integer aInteger : inputSquared) {
13
+            for (int i = 0; i < squaredValues.length; i++) {
14
+                if (aInteger.equals(squaredValues[i])) {
15
+                    allFlags[i] = true;
16
+                }
17
+            }
18
+        }
19
+
20
+        for (Boolean flag : allFlags) {
21
+            if (!flag) {
22
+                return false;
23
+            }
24
+        }
25
+
26
+        return true;
27
+    }
28
+
29
+    public static Integer[] squareEachValue(Integer[] input) {
30
+        Integer[] squared = new Integer[input.length];
31
+
32
+        for (int i = 0; i < input.length; i++) {
33
+            squared[i] = input[i] * input[i];
34
+        }
35
+
36
+        return squared;
9 37
     }
10 38
 }

+ 19
- 0
src/test/java/rocks/zipcode/io/quiz3/arrays/squarearrayanalyzer/MyTest.java Wyświetl plik

@@ -0,0 +1,19 @@
1
+package rocks.zipcode.io.quiz3.arrays.squarearrayanalyzer;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+import rocks.zipcode.io.quiz3.arrays.SquareArrayAnalyzer;
6
+
7
+public class MyTest {
8
+
9
+    @Test
10
+    public void testSquareEachValue() {
11
+        Integer[] input = new Integer[] {1, 2, 3, 4};
12
+
13
+        Integer[] expected = new Integer[] {1, 4, 9, 16};
14
+        Integer[] actual = SquareArrayAnalyzer.squareEachValue(input);
15
+
16
+        Assert.assertArrayEquals(expected, actual);
17
+    }
18
+
19
+}