From 0ee7655b88cc07d1447ce0d8b2964a8c8d99856d Mon Sep 17 00:00:00 2001 From: ac189223 <52603022+ac189223@users.noreply.github.com> Date: Sat, 19 Oct 2019 14:02:03 +0200 Subject: [PATCH 1/2] Create AndrzejC_Lund_Ex1.md --- docs/AndrzejC_Lund_Ex1.md | 90 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 docs/AndrzejC_Lund_Ex1.md diff --git a/docs/AndrzejC_Lund_Ex1.md b/docs/AndrzejC_Lund_Ex1.md new file mode 100644 index 0000000..a72ef3d --- /dev/null +++ b/docs/AndrzejC_Lund_Ex1.md @@ -0,0 +1,90 @@ +## Exercises I: Filter + +#### Summary +* The most difficult part for me was to add libraries to **the existing gradle project**. +For a long time Junit5 and Hamcrest were not visible. +Apparently neither getting them directly via IntelliJ, nor downloading and linking jar files are enough. +Gradle has to be built correctly :) +* I know that code inputs do not look very nice, but this is the best I managed. +Code works, this is important :) + +#### RectangleArrayProcessor - Implementation of the following methods: +* **removeRectanglesLargerThan** + * takes a Rectangle array and an int n + * returns an array containing only the rectangles with a smaller area than n + * _public Rectangle[] removeRectanglesLargerThan(Rectangle[] array, int maxArea) { + ArrayList resultArray = new ArrayList<>(); + for (Rectangle rectangle: array) if (rectangle.area() <= maxArea) resultArray.add(rectangle); + return resultArray.toArray(new Rectangle[resultArray.size()]); + }_ +* **removeRectanglesLargerThan** + * takes a Rectangle array and a Rectangle r + * returns an array containing only the rectangles with a smaller area than r's area + * _public Rectangle[] removeRectanglesLargerThan(Rectangle[] array, Rectangle r) { + ArrayList resultArray = new ArrayList<>(); + for (Rectangle rectangle: array) if (rectangle.compareTo(r) < 0) resultArray.add(rectangle); + return resultArray.toArray(new Rectangle[resultArray.size()]); + }_ +* **containsSquare** + * takes a Rectangle array + * returns true if the array contains a square or false otherwise + * _public boolean containsSquare(Rectangle[] array) { + for (Rectangle rectangle: array) if (rectangle.getHeight() == rectangle.getWidth()) return true; + return false; + }_ +* **filterRectanglesWithEqualArea** + * takes a Rectangle array and a Rectangle r + * returns an array containing only the rectangles with an area equal to r's area + * _public Rectangle[] filterRectanglesWithEqualArea(Rectangle[] array, Rectangle r) { + ArrayList resultArray = new ArrayList<>(); + for (Rectangle rectangle: array) if (rectangle.compareTo(r) == 0) resultArray.add(rectangle); + return resultArray.toArray(new Rectangle[resultArray.size()]); + }_ + +#### RectangleArrayProcessorTest - Implementation of the following test methods: +* **removeRectanglesLargerThan** (both, with input int and Rectangle) + * _@Test + void testProcessorRemovesRectanglesLargerThan() { + // Arrange + Rectangle[] inputArray = {new Rectangle(2, 2), new Rectangle(2, 3), new Rectangle(5, 4), new Rectangle(1, 5)}; + int inputMaxArea = 5; + Rectangle[] expectedArrayForMaxArea = {new Rectangle(2, 2), new Rectangle(1, 5)}; + Rectangle r = new Rectangle(4, 4); + Rectangle[] expectedArrayForR = {new Rectangle(2, 2), new Rectangle(2, 3), new Rectangle(1, 5)}; + // Act + Rectangle[] actualArrayForMaxArea = rectangleArrayProcessor.removeRectanglesLargerThan(inputArray, inputMaxArea); + Rectangle[] actualArrayForR = rectangleArrayProcessor.removeRectanglesLargerThan(inputArray, r); + // Assert + assertArrayEquals(expectedArrayForMaxArea, actualArrayForMaxArea); + assertArrayEquals(expectedArrayForR, actualArrayForR); + assertNotEquals(actualArrayForR, actualArrayForMaxArea); + }_ +* **containsSquare** + * _@Test + void testContainsSquare() { + // Arrange + Rectangle[] inputArrayWithSquare = {new Rectangle(2, 2), new Rectangle(2, 3), new Rectangle(5, 4), new Rectangle(1, 5)}; + Rectangle[] inputArrayWithoutSquare = {new Rectangle(2, 3), new Rectangle(5, 4), new Rectangle(1, 5)}; + // Act + boolean checkOfArrayWithSquare = rectangleArrayProcessor.containsSquare(inputArrayWithSquare); + boolean checkOfArrayWithoutSquare = rectangleArrayProcessor.containsSquare(inputArrayWithoutSquare); + // Assert + assertTrue(checkOfArrayWithSquare); + assertFalse(checkOfArrayWithoutSquare); + }_ +* **filterRectanglesWithEqualArea** + * _@Test + void testFilterRectanglesWithEqualArea() { + // Arrange + Rectangle[] inputArrayWith = {new Rectangle(2, 2), new Rectangle(2, 3), new Rectangle(5, 4), new Rectangle(3, 2), + new Rectangle(1, 6), new Rectangle(5, 3), new Rectangle(3, 5)}; + Rectangle[] inputArrayWithout = {new Rectangle(2, 2), new Rectangle(5, 4), new Rectangle(5, 3), new Rectangle(3, 5)}; + Rectangle r = new Rectangle(6, 1); + Rectangle[] expectedArrayWithForR = {new Rectangle(2, 3), new Rectangle(3, 2), new Rectangle(1, 6),}; + // Act + Rectangle[] actualArrayWithForR = rectangleArrayProcessor.filterRectanglesWithEqualArea(inputArrayWith, r); + int actualSizeArrayWithoutForR = rectangleArrayProcessor.filterRectanglesWithEqualArea(inputArrayWithout, r).length; + // Assert + assertArrayEquals(expectedArrayWithForR, actualArrayWithForR); + assertEquals(0, actualSizeArrayWithoutForR); + }_ From e2acb361211dd6422eba633de07cf662c348aaf6 Mon Sep 17 00:00:00 2001 From: ac189223 <52603022+ac189223@users.noreply.github.com> Date: Sat, 19 Oct 2019 15:58:49 +0200 Subject: [PATCH 2/2] Update AndrzejC_Lund_Ex1.md Code block highlighting added. --- docs/AndrzejC_Lund_Ex1.md | 189 +++++++++++++++++++++++++------------- 1 file changed, 127 insertions(+), 62 deletions(-) diff --git a/docs/AndrzejC_Lund_Ex1.md b/docs/AndrzejC_Lund_Ex1.md index a72ef3d..1d334e3 100644 --- a/docs/AndrzejC_Lund_Ex1.md +++ b/docs/AndrzejC_Lund_Ex1.md @@ -12,79 +12,144 @@ Code works, this is important :) * **removeRectanglesLargerThan** * takes a Rectangle array and an int n * returns an array containing only the rectangles with a smaller area than n - * _public Rectangle[] removeRectanglesLargerThan(Rectangle[] array, int maxArea) { - ArrayList resultArray = new ArrayList<>(); - for (Rectangle rectangle: array) if (rectangle.area() <= maxArea) resultArray.add(rectangle); - return resultArray.toArray(new Rectangle[resultArray.size()]); - }_ + * ```java + public Rectangle[] removeRectanglesLargerThan(Rectangle[] array, int maxArea) { + ArrayList resultArray = new ArrayList<>(); + for (Rectangle rectangle: array) + if (rectangle.area() <= maxArea) + resultArray.add(rectangle); + return resultArray.toArray(new Rectangle[resultArray.size()]); + } + ``` * **removeRectanglesLargerThan** * takes a Rectangle array and a Rectangle r * returns an array containing only the rectangles with a smaller area than r's area - * _public Rectangle[] removeRectanglesLargerThan(Rectangle[] array, Rectangle r) { - ArrayList resultArray = new ArrayList<>(); - for (Rectangle rectangle: array) if (rectangle.compareTo(r) < 0) resultArray.add(rectangle); - return resultArray.toArray(new Rectangle[resultArray.size()]); - }_ + * ```java + public Rectangle[] removeRectanglesLargerThan(Rectangle[] array, Rectangle r) { + ArrayList resultArray = new ArrayList<>(); + for (Rectangle rectangle: array) + if (rectangle.compareTo(r) < 0) + resultArray.add(rectangle); + return resultArray.toArray(new Rectangle[resultArray.size()]); + } + ``` * **containsSquare** * takes a Rectangle array * returns true if the array contains a square or false otherwise - * _public boolean containsSquare(Rectangle[] array) { - for (Rectangle rectangle: array) if (rectangle.getHeight() == rectangle.getWidth()) return true; - return false; - }_ + * ```java + public boolean containsSquare(Rectangle[] array) { + for (Rectangle rectangle: array) + if (rectangle.getHeight() == rectangle.getWidth()) + return true; + return false; + } + ``` * **filterRectanglesWithEqualArea** * takes a Rectangle array and a Rectangle r * returns an array containing only the rectangles with an area equal to r's area - * _public Rectangle[] filterRectanglesWithEqualArea(Rectangle[] array, Rectangle r) { - ArrayList resultArray = new ArrayList<>(); - for (Rectangle rectangle: array) if (rectangle.compareTo(r) == 0) resultArray.add(rectangle); - return resultArray.toArray(new Rectangle[resultArray.size()]); - }_ + * ```java + public Rectangle[] filterRectanglesWithEqualArea(Rectangle[] array, Rectangle r) { + ArrayList resultArray = new ArrayList<>(); + for (Rectangle rectangle: array) + if (rectangle.compareTo(r) == 0) + resultArray.add(rectangle); + return resultArray.toArray(new Rectangle[resultArray.size()]); + } + ``` #### RectangleArrayProcessorTest - Implementation of the following test methods: * **removeRectanglesLargerThan** (both, with input int and Rectangle) - * _@Test - void testProcessorRemovesRectanglesLargerThan() { - // Arrange - Rectangle[] inputArray = {new Rectangle(2, 2), new Rectangle(2, 3), new Rectangle(5, 4), new Rectangle(1, 5)}; - int inputMaxArea = 5; - Rectangle[] expectedArrayForMaxArea = {new Rectangle(2, 2), new Rectangle(1, 5)}; - Rectangle r = new Rectangle(4, 4); - Rectangle[] expectedArrayForR = {new Rectangle(2, 2), new Rectangle(2, 3), new Rectangle(1, 5)}; - // Act - Rectangle[] actualArrayForMaxArea = rectangleArrayProcessor.removeRectanglesLargerThan(inputArray, inputMaxArea); - Rectangle[] actualArrayForR = rectangleArrayProcessor.removeRectanglesLargerThan(inputArray, r); - // Assert - assertArrayEquals(expectedArrayForMaxArea, actualArrayForMaxArea); - assertArrayEquals(expectedArrayForR, actualArrayForR); - assertNotEquals(actualArrayForR, actualArrayForMaxArea); - }_ + ```java + @Test + void testProcessorRemovesRectanglesLargerThan() { + // Arrange + Rectangle[] inputArray = { + new Rectangle(2, 2), + new Rectangle(2, 3), + new Rectangle(5, 4), + new Rectangle(1, 5) + }; + int inputMaxArea = 5; + Rectangle[] expectedArrayForMaxArea = { + new Rectangle(2, 2), + new Rectangle(1, 5) + }; + Rectangle r = new Rectangle(4, 4); + Rectangle[] expectedArrayForR = { + new Rectangle(2, 2), + new Rectangle(2, 3), + new Rectangle(1, 5) + }; + + // Act + Rectangle[] actualArrayForMaxArea = rectangleArrayProcessor.removeRectanglesLargerThan(inputArray, inputMaxArea); + Rectangle[] actualArrayForR = rectangleArrayProcessor.removeRectanglesLargerThan(inputArray, r); + + // Assert + assertArrayEquals(expectedArrayForMaxArea, actualArrayForMaxArea); + assertArrayEquals(expectedArrayForR, actualArrayForR); + assertNotEquals(actualArrayForR, actualArrayForMaxArea); + } + ``` * **containsSquare** - * _@Test - void testContainsSquare() { - // Arrange - Rectangle[] inputArrayWithSquare = {new Rectangle(2, 2), new Rectangle(2, 3), new Rectangle(5, 4), new Rectangle(1, 5)}; - Rectangle[] inputArrayWithoutSquare = {new Rectangle(2, 3), new Rectangle(5, 4), new Rectangle(1, 5)}; - // Act - boolean checkOfArrayWithSquare = rectangleArrayProcessor.containsSquare(inputArrayWithSquare); - boolean checkOfArrayWithoutSquare = rectangleArrayProcessor.containsSquare(inputArrayWithoutSquare); - // Assert - assertTrue(checkOfArrayWithSquare); - assertFalse(checkOfArrayWithoutSquare); - }_ + ```java + @Test + void testContainsSquare() { + // Arrange + Rectangle[] inputArrayWithSquare = { + new Rectangle(2, 2), + new Rectangle(2, 3), + new Rectangle(5, 4), + new Rectangle(1, 5) + }; + Rectangle[] inputArrayWithoutSquare = { + new Rectangle(2, 3), + new Rectangle(5, 4), + new Rectangle(1, 5) + }; + + // Act + boolean checkOfArrayWithSquare = rectangleArrayProcessor.containsSquare(inputArrayWithSquare); + boolean checkOfArrayWithoutSquare = rectangleArrayProcessor.containsSquare(inputArrayWithoutSquare); + + // Assert + assertTrue(checkOfArrayWithSquare); + assertFalse(checkOfArrayWithoutSquare); + } + ``` * **filterRectanglesWithEqualArea** - * _@Test - void testFilterRectanglesWithEqualArea() { - // Arrange - Rectangle[] inputArrayWith = {new Rectangle(2, 2), new Rectangle(2, 3), new Rectangle(5, 4), new Rectangle(3, 2), - new Rectangle(1, 6), new Rectangle(5, 3), new Rectangle(3, 5)}; - Rectangle[] inputArrayWithout = {new Rectangle(2, 2), new Rectangle(5, 4), new Rectangle(5, 3), new Rectangle(3, 5)}; - Rectangle r = new Rectangle(6, 1); - Rectangle[] expectedArrayWithForR = {new Rectangle(2, 3), new Rectangle(3, 2), new Rectangle(1, 6),}; - // Act - Rectangle[] actualArrayWithForR = rectangleArrayProcessor.filterRectanglesWithEqualArea(inputArrayWith, r); - int actualSizeArrayWithoutForR = rectangleArrayProcessor.filterRectanglesWithEqualArea(inputArrayWithout, r).length; - // Assert - assertArrayEquals(expectedArrayWithForR, actualArrayWithForR); - assertEquals(0, actualSizeArrayWithoutForR); - }_ + ```java + @Test + void testFilterRectanglesWithEqualArea() { + // Arrange + Rectangle[] inputArrayWith = { + new Rectangle(2, 2), + new Rectangle(2, 3), + new Rectangle(5, 4), + new Rectangle(3, 2), + new Rectangle(1, 6), + new Rectangle(5, 3), + new Rectangle(3, 5) + }; + Rectangle[] inputArrayWithout = { + new Rectangle(2, 2), + new Rectangle(5, 4), + new Rectangle(5, 3), + new Rectangle(3, 5) + }; + Rectangle r = new Rectangle(6, 1); + Rectangle[] expectedArrayWithForR = { + new Rectangle(2, 3), + new Rectangle(3, 2), + new Rectangle(1, 6), + }; + + // Act + Rectangle[] actualArrayWithForR = rectangleArrayProcessor.filterRectanglesWithEqualArea(inputArrayWith, r); + int actualSizeArrayWithoutForR = rectangleArrayProcessor.filterRectanglesWithEqualArea(inputArrayWithout, r).length; + + // Assert + assertArrayEquals(expectedArrayWithForR, actualArrayWithForR); + assertEquals(0, actualSizeArrayWithoutForR); + } + ```