From ebe798d8b7b51a8d8f5e587c7f0b885c7430d5c5 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:15:25 +0100 Subject: [PATCH 01/14] Add square calculation method --- src/main/java/Calculations.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/Calculations.java diff --git a/src/main/java/Calculations.java b/src/main/java/Calculations.java new file mode 100644 index 0000000..74654ff --- /dev/null +++ b/src/main/java/Calculations.java @@ -0,0 +1,5 @@ +public class Calculations { + public int calculateSquare(int num) { + return (num * num); + } +} From 63947fdc1da9023d1f309b551b4e8fea2809abb5 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:15:57 +0100 Subject: [PATCH 02/14] Create test template for Calculations Class --- src/test/java/CalculationsTest.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/test/java/CalculationsTest.java diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java new file mode 100644 index 0000000..bc1e503 --- /dev/null +++ b/src/test/java/CalculationsTest.java @@ -0,0 +1,5 @@ +import junit.framework.TestCase; + +public class CalculationsTest extends TestCase { + +} \ No newline at end of file From dc0e1ba40405f6e1d3c4233ecbed64b3cd9c1e39 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:43:42 +0100 Subject: [PATCH 03/14] Test: create test for positive numbers --- src/test/java/CalculationsTest.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index bc1e503..9b6a1df 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -1,5 +1,14 @@ -import junit.framework.TestCase; +import org.junit.Test; +import static org.junit.Assert.assertEquals; -public class CalculationsTest extends TestCase { - -} \ No newline at end of file +public class CalculationsTest { + + @Test + public void testCalculateSquare() { + Calculations calc = new Calculations(); + + // Test with a positive number + assertEquals(25, calc.calculateSquare(5)); + + } +} From 0e75fd599d45e02207011e7a66bf20f67e8675ad Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:44:24 +0100 Subject: [PATCH 04/14] Test: Add test for negative numbers --- src/test/java/CalculationsTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index 9b6a1df..accfdae 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -10,5 +10,8 @@ public class CalculationsTest { // Test with a positive number assertEquals(25, calc.calculateSquare(5)); + // Test with a negative number + assertEquals(16, calc.calculateSquare(-4)); + } } From 306dd5dad1dd5428fd8fe7e7e0ddd8b7983abd12 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:46:51 +0100 Subject: [PATCH 05/14] Test: Test for null input --- src/test/java/CalculationsTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index accfdae..29d2496 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -13,5 +13,8 @@ public class CalculationsTest { // Test with a negative number assertEquals(16, calc.calculateSquare(-4)); + // Test with zero + assertEquals(0, calc.calculateSquare(0)); + } } From b2117666abb8952d88c69b0feb81b0749a06015b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:47:46 +0100 Subject: [PATCH 06/14] Test: Check function for larger numbers --- src/test/java/CalculationsTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index 29d2496..52c5e17 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -16,5 +16,8 @@ public class CalculationsTest { // Test with zero assertEquals(0, calc.calculateSquare(0)); + // Test with a large number + assertEquals(1000000, calc.calculateSquare(1000)); + } } From 454bd8c0989d0e6b99163bba2d1fb8a1b87cb2eb Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:57:37 +0100 Subject: [PATCH 07/14] Create factorial calculation method --- src/main/java/Calculations.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/Calculations.java b/src/main/java/Calculations.java index 74654ff..09d1298 100644 --- a/src/main/java/Calculations.java +++ b/src/main/java/Calculations.java @@ -2,4 +2,18 @@ public class Calculations { public int calculateSquare(int num) { return (num * num); } + + public int calculateFactorial(int num) { + if (num < 0) { + throw new IllegalArgumentException("Factorial is not defined for negative numbers"); + } + if (num == 0 || num == 1) { + return 1; // Factorial of 0 and 1 is 1 + } + int factorial = 1; + for (int i = 2; i <= num; i++) { + factorial *= i; + } + return factorial; + } } From cec39970dfbb827a656e233f1906ed118def17f8 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:59:44 +0100 Subject: [PATCH 08/14] Test: Write positive numbers test for factorial calculation --- src/test/java/CalculationsTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index 52c5e17..6586f94 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -18,6 +18,19 @@ public class CalculationsTest { // Test with a large number assertEquals(1000000, calc.calculateSquare(1000)); + } + + @Test + public void testCalculateFactorial() { + Calculations fact = new Calculations(); + + // Test with positive numbers + assertEquals(1, fact.calculateFactorial(0)); + assertEquals(1, fact.calculateFactorial(1)); + assertEquals(2, fact.calculateFactorial(2)); + assertEquals(6, fact.calculateFactorial(3)); + assertEquals(24, fact.calculateFactorial(4)); + assertEquals(120, fact.calculateFactorial(5)); } } From c1acbdf3737d6d4caa7c44b95eef48dff748adc1 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 23:01:42 +0100 Subject: [PATCH 09/14] Test: Test function for large numbers --- src/test/java/CalculationsTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index 6586f94..634a972 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -32,5 +32,8 @@ public class CalculationsTest { assertEquals(24, fact.calculateFactorial(4)); assertEquals(120, fact.calculateFactorial(5)); + // Test with large number + assertEquals(3628800, fact.calculateFactorial(10)); } } + From 7b9dc7ea4035e46d8bbdbeab8531266ae0932a41 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 23:03:32 +0100 Subject: [PATCH 10/14] Test: Check method for wrong and negative entries --- src/test/java/CalculationsTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index 634a972..1073c19 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -35,5 +35,11 @@ public class CalculationsTest { // Test with large number assertEquals(3628800, fact.calculateFactorial(10)); } + + @Test(expected = IllegalArgumentException.class) + public void testNegativeNumber() { + Calculations fact = new Calculations(); + fact.calculateFactorial(-5); // Should throw IllegalArgumentException + } } From d8584854c38e60f4c7d286aa3e2ff2b4da6d882e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 23:14:18 +0100 Subject: [PATCH 11/14] Add method to check if number is prime --- src/main/java/Calculations.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/Calculations.java b/src/main/java/Calculations.java index 09d1298..a97d163 100644 --- a/src/main/java/Calculations.java +++ b/src/main/java/Calculations.java @@ -16,4 +16,22 @@ public class Calculations { } return factorial; } + + public static boolean isPrime(int num) { + if (num <= 1) { + return false; // Numbers less than or equal to 1 are not prime + } + if (num == 2 || num == 3) { + return true; // 2 and 3 are prime numbers + } + if (num % 2 == 0) { + return false; // Even numbers greater than 2 are not prime + } + for (int i = 3; i <= Math.sqrt(num); i += 2) { + if (num % i == 0) { + return false; // If num is divisible by any number from 3 to sqrt(num), it's not prime + } + } + return true; // If num is not divisible by any number from 2 to sqrt(num), it's prime + } } From 340116f30638043dcc02b424ae07ddfa2b428511 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 23:14:45 +0100 Subject: [PATCH 12/14] Test: Test assert true for prime numbers --- src/test/java/CalculationsTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index 1073c19..3aec84b 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -1,5 +1,7 @@ import org.junit.Test; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class CalculationsTest { @@ -41,5 +43,12 @@ public class CalculationsTest { Calculations fact = new Calculations(); fact.calculateFactorial(-5); // Should throw IllegalArgumentException } + + @Test + public void testIsPrime() { + assertTrue(Calculations.isPrime(2)); // Prime number + assertTrue(Calculations.isPrime(3)); // Prime number + assertTrue(Calculations.isPrime(5)); // Prime number + } } From d642131102960f51575cfa76b5d219dae52cce08 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 23:16:48 +0100 Subject: [PATCH 13/14] Test: check prime for non-prime numbers --- src/test/java/CalculationsTest.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index 3aec84b..f5b91aa 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -1,7 +1,7 @@ import org.junit.Test; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; public class CalculationsTest { @@ -46,9 +46,16 @@ public class CalculationsTest { @Test public void testIsPrime() { - assertTrue(Calculations.isPrime(2)); // Prime number - assertTrue(Calculations.isPrime(3)); // Prime number - assertTrue(Calculations.isPrime(5)); // Prime number + // Prime + assertTrue(Calculations.isPrime(2)); + assertTrue(Calculations.isPrime(3)); + assertTrue(Calculations.isPrime(5)); + + // Non-Prime + assertFalse(Calculations.isPrime(22)); + assertFalse(Calculations.isPrime(25)); + assertFalse(Calculations.isPrime(27)); + assertFalse(Calculations.isPrime(30)); } } From da859d795f693fa42425ab5a9ce22d18211314d2 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 23:23:13 +0100 Subject: [PATCH 14/14] Test: Add more numbers to test in loop --- src/test/java/CalculationsTest.java | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/java/CalculationsTest.java b/src/test/java/CalculationsTest.java index f5b91aa..785d377 100644 --- a/src/test/java/CalculationsTest.java +++ b/src/test/java/CalculationsTest.java @@ -1,7 +1,6 @@ import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; + +import static org.junit.Assert.*; public class CalculationsTest { @@ -47,15 +46,16 @@ public class CalculationsTest { @Test public void testIsPrime() { // Prime - assertTrue(Calculations.isPrime(2)); - assertTrue(Calculations.isPrime(3)); - assertTrue(Calculations.isPrime(5)); - - // Non-Prime - assertFalse(Calculations.isPrime(22)); - assertFalse(Calculations.isPrime(25)); - assertFalse(Calculations.isPrime(27)); - assertFalse(Calculations.isPrime(30)); + int[] primeNumbers = {2, 3, 5, 7, 11, 13}; + int[] nonPrimeNumbers = {22, 33, 25, 4, 8, 14}; + + for(int i = 0; i < primeNumbers.length; i++) { + assertTrue(Calculations.isPrime(primeNumbers[i])); + } + + for(int i = 0; i < nonPrimeNumbers.length; i++) { + assertFalse(Calculations.isPrime(nonPrimeNumbers[i])); + } } }