Valentin Spiroski
11 months ago
2 changed files with 98 additions and 0 deletions
@ -0,0 +1,37 @@ |
|||
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; |
|||
} |
|||
|
|||
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 |
|||
} |
|||
} |
@ -0,0 +1,61 @@ |
|||
import org.junit.Test; |
|||
|
|||
import static org.junit.Assert.*; |
|||
|
|||
public class CalculationsTest { |
|||
|
|||
@Test |
|||
public void testCalculateSquare() { |
|||
Calculations calc = new Calculations(); |
|||
|
|||
// Test with a positive number |
|||
assertEquals(25, calc.calculateSquare(5)); |
|||
|
|||
// Test with a negative number |
|||
assertEquals(16, calc.calculateSquare(-4)); |
|||
|
|||
// Test with zero |
|||
assertEquals(0, calc.calculateSquare(0)); |
|||
|
|||
// 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)); |
|||
|
|||
// 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 |
|||
} |
|||
|
|||
@Test |
|||
public void testIsPrime() { |
|||
// Prime |
|||
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])); |
|||
} |
|||
} |
|||
} |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue