Repository: TheAlgorithms/Java
Pull request #3203 (2022-08-02 06:46:53)

Added three new distance formula and renamed a function
Three new distance formula:
-Manhattan Distance
-Hamming Distance
-Minkowski Distance
*Test cases are provided

Renaming a function:
-distance to euclideanDistance to make more specific

### **Describe your change:**

- [x] Add an algorithm?
- [x] Fix a bug or typo in an existing algorithm?
- [x] Documentation change?

#### References

[4 Distance Measures for Machine Learning](https://machinelearningmastery.com/distance-measures-for-machine-learning/)

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/DistanceFormula.java b/src/main/java/com/thealgorithms/maths/DistanceFormula.java
index cc6ebc9e..245aad9d 100644
--- a/src/main/java/com/thealgorithms/maths/DistanceFormula.java
+++ b/src/main/java/com/thealgorithms/maths/DistanceFormula.java
@@ -1,11 +1,46 @@
 package com.thealgorithms.maths;
 
 public class DistanceFormula {
-	public static double distance(double x1, double y1, double x2, double y2)
-	{
-		double dX = Math.pow(x2-x1, 2);
-		double dY = Math.pow(y2-x1, 2);
-		double d = Math.sqrt(dX+dY);
+	public static double euclideanDistance(double x1, double y1, double x2, double y2) {
+		double dX = Math.pow(x2 - x1, 2);
+		double dY = Math.pow(y2 - x1, 2);
+		double d = Math.sqrt(dX + dY);
+		return d;
+	}
+
+	public static double manhattanDistance(double x1, double y1, double x2, double y2) {
+		double d = Math.abs(x1 - x2) + Math.abs(y1 - y2);
+		return d;
+	}
+
+	public static int hammingDistance(int[] b1, int[] b2) {
+		int d = 0;
+
+		if (b1.length != b2.length) {
+			return -1; // error, both array must be have the same length
+		}
+
+		for (int i = 0; i < b1.length; i++) {
+			d += Math.abs(b1[i] - b2[i]);
+		}
+
+		return d;
+	}
+
+	public static double minkowskiDistance(double[] p1, double[] p2, int p) {
+		double d = 0;
+		double distance = 0.0;
+
+		if (p1.length != p2.length) {
+			return -1; // error, both array must be have the same length
+		}
+
+		for (int i = 0; i < p1.length; i++) {
+			distance += Math.abs(Math.pow(p1[i] - p2[i], p));
+		}
+
+		distance = Math.pow(distance, (double) 1 / p);
+		d = distance;
 		return d;
 	}
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java b/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java
index e54f1263..811eb4bf 100644
--- a/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java
+++ b/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java
@@ -1,28 +1,85 @@
 package com.thealgorithms.maths;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
-public class DistanceFormulaTest
-{
-  @Test
-  void test1()
-  {
-    Assertions.assertEquals(DistanceFormula.distance(1,1,2,2), 1.4142135623730951);
-  }
-  @Test
-  void test2()
-  {
-    Assertions.assertEquals(DistanceFormula.distance(1,3,8,0), 7.0710678118654755);
-  }
-  @Test
-  void test3()
-  {
-    Assertions.assertEquals(DistanceFormula.distance(2.4,9.1,55.1,100), 110.91911467371168);
-  }
-  @Test
-  void test4()
-  {
-    Assertions.assertEquals(DistanceFormula.distance(1000,13,20000,84), 19022.067605809836);
-  }
+public class DistanceFormulaTest {
+	@Test
+	void euclideanTest1() {
+		Assertions.assertEquals(DistanceFormula.euclideanDistance(1, 1, 2, 2), 1.4142135623730951);
+	}
+
+	@Test
+	void euclideanTest2() {
+		Assertions.assertEquals(DistanceFormula.euclideanDistance(1, 3, 8, 0), 7.0710678118654755);
+	}
+
+	@Test
+	void euclideanTest3() {
+		Assertions.assertEquals(DistanceFormula.euclideanDistance(2.4, 9.1, 55.1, 100), 110.91911467371168);
+	}
+
+	@Test
+	void euclideanTest4() {
+		Assertions.assertEquals(DistanceFormula.euclideanDistance(1000, 13, 20000, 84), 19022.067605809836);
+	}
+
+	@Test
+	public void manhattantest1() {
+		assertEquals(DistanceFormula.manhattanDistance(1, 2, 3, 4), 4);
+	}
+
+	@Test
+	public void manhattantest2() {
+		assertEquals(DistanceFormula.manhattanDistance(6.5, 8.4, 20.1, 13.6), 18.8);
+	}
+
+	@Test
+	public void manhattanTest3() {
+		assertEquals(DistanceFormula.manhattanDistance(10.112, 50, 8, 25.67), 26.442);
+	}
+
+	@Test
+	public void hammingTest1() {
+		int[] array1 = { 1, 1, 1, 1 };
+		int[] array2 = { 0, 0, 0, 0 };
+		assertEquals(DistanceFormula.hammingDistance(array1, array2), 4);
+	}
+
+	@Test
+	public void hammingTest2() {
+		int[] array1 = { 1, 1, 1, 1 };
+		int[] array2 = { 1, 1, 1, 1 };
+		assertEquals(DistanceFormula.hammingDistance(array1, array2), 0);
+	}
+
+	@Test
+	public void hammingTest3() {
+		int[] array1 = { 1, 0, 0, 1, 1, 0, 1, 1, 0 };
+		int[] array2 = { 0, 1, 0, 0, 1, 1, 1, 0, 0 };
+		assertEquals(DistanceFormula.hammingDistance(array1, array2), 5);
+	}
+
+	@Test
+	public void minkowskiTest1() {
+		double[] array1 = { 1, 3, 8, 5 };
+		double[] array2 = { 4, 2, 6, 9 };
+		assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 1), 10);
+	}
+
+	@Test
+	public void minkowskiTest2() {
+		double[] array1 = { 1, 3, 8, 5 };
+		double[] array2 = { 4, 2, 6, 9 };
+		assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 2), 5.477225575051661);
+	}
+
+	@Test
+	public void minkowskiTest3() {
+		double[] array1 = { 1, 3, 8, 5 };
+		double[] array2 = { 4, 2, 6, 9 };
+		assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 3), 4.641588833612778);
+	}
 }