diff --git a/TDD_for_beginners/Calculator/Calculator.java b/TDD_for_beginners/Calculator/Calculator.java new file mode 100644 index 00000000..ba1e9cd3 --- /dev/null +++ b/TDD_for_beginners/Calculator/Calculator.java @@ -0,0 +1,12 @@ +public class Calculator { + + // Method to add two numbers + public int add(int first, int second) { + return first + second; + } + + // Method to multiply two numbers + public int multiply(int first, int second) { + return first * second; + } +} diff --git a/TDD_for_beginners/Calculator/CalculatorTest.java b/TDD_for_beginners/Calculator/CalculatorTest.java new file mode 100644 index 00000000..e8da728d --- /dev/null +++ b/TDD_for_beginners/Calculator/CalculatorTest.java @@ -0,0 +1,31 @@ +import org.junit.Test; +import static org.junit.Assert.*; + +public class CalculatorTest { + + @Test + public void testProduct() { + // Create an instance of the Calculator class + Calculator product = new Calculator(); + + // Call the multiply method on the Calculator instance + // and store the result in the 'prod' variable + int prod = product.multiply(2,4); + + // Check if the result is equal to 8 using the assertEquals method + assertEquals(8,prod); + } + + @Test + public void testAdd() { + // Create an instance of the Calculator class + Calculator calculator = new Calculator(); + + // Call the add method on the Calculator instance + // and store the result in the 'result' variable + int result = calculator.add(2, 3); + + // Check if the result is equal to 5 using the assertEquals method + assertEquals(5, result); + } +} diff --git a/TDD_for_beginners/Max_number_in_array/Max.java b/TDD_for_beginners/Max_number_in_array/Max.java new file mode 100644 index 00000000..ef4fc526 --- /dev/null +++ b/TDD_for_beginners/Max_number_in_array/Max.java @@ -0,0 +1,21 @@ +import java.util.Arrays; + +public class Max { + + // Array of integers + int[] arr = new int[]{1, 2, 3, 4}; + + // Method to find the maximum number in an array + public int max_num(int[] arr) { + int max = 0; + + // Iterate through the array + for (int j : arr) { + if (max < j) { + max = j; + } + } + + return max; + } +} diff --git a/TDD_for_beginners/Max_number_in_array/MaxnumberTest.java b/TDD_for_beginners/Max_number_in_array/MaxnumberTest.java new file mode 100644 index 00000000..fbb58d02 --- /dev/null +++ b/TDD_for_beginners/Max_number_in_array/MaxnumberTest.java @@ -0,0 +1,22 @@ +import org.junit.Test; +import static org.junit.Assert.*; + +public class MaxnumberTest { + + @Test + // Test case to verify the functionality of max_num method + public void textMax() { + // Create an instance of the Max class + Max max = new Max(); + + // Create an array of integers + int[] arr = new int[]{1, 2, 3, 4}; + + // Call the max_num method on the Max instance + // and store the result in the 'max_number' variable + int max_number = max.max_num(arr); + + // Check if the result is equal to 4 using the assertEquals method + assertEquals(4, max_number); + } +} diff --git a/TDD_for_beginners/Sorting/Sort.java b/TDD_for_beginners/Sorting/Sort.java new file mode 100644 index 00000000..24509afb --- /dev/null +++ b/TDD_for_beginners/Sorting/Sort.java @@ -0,0 +1,24 @@ +import java.util.Arrays; + + +public class Sort { +//There are multiple ways to sort an array +//Lets move with Bubble sort + int[] arr = new int[]{1,2,3,4}; + public void bubble(int[] arr){ + for(int i=0; i < arr.length;i++) { + for (int j = 1; j < arr.length; j++) { + if (arr[j] < arr[j - 1]) { + swap(arr,j, j - 1); + } + } + } + } + + private void swap(int[] arr,int first, int second) { + int temp = arr[first]; + arr[first] = arr[second]; + arr[second] = temp; + } + } + diff --git a/TDD_for_beginners/Sorting/SortingTest.java b/TDD_for_beginners/Sorting/SortingTest.java new file mode 100644 index 00000000..7a218690 --- /dev/null +++ b/TDD_for_beginners/Sorting/SortingTest.java @@ -0,0 +1,22 @@ +import org.junit.Test; +import java.util.Arrays; +import static org.junit.Assert.*; + +public class SortingTest { + + @Test + // Test case to verify the functionality of the bubble sort algorithm + public void testing() { + // Create an instance of the Sort class + Sort sort = new Sort(); + + // Create an array of integers + int[] arr = new int[]{1, 3, 2, 0}; + + // Call the bubble method on the Sort instance to sort the array + sort.bubble(arr); + + // Print the sorted array to the console + System.out.println(Arrays.toString(arr)); + } +}