Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Calculator, finding max_number, and sorting #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions TDD_for_beginners/Calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 31 additions & 0 deletions TDD_for_beginners/Calculator/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions TDD_for_beginners/Max_number_in_array/Max.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 22 additions & 0 deletions TDD_for_beginners/Max_number_in_array/MaxnumberTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 24 additions & 0 deletions TDD_for_beginners/Sorting/Sort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

22 changes: 22 additions & 0 deletions TDD_for_beginners/Sorting/SortingTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}