forked from kennyledet/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kennyledet#415 from aayushKumarJarvis/master
Adding Java Implementation of Chebychev Distance
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Chebyshev_distance/Java/aayushKumarJarvis/ChebychevDistance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
public class ChebychevDistance { | ||
|
||
public double getMaximumValue(double elements[]) { | ||
|
||
double maxVal = elements[0]; | ||
|
||
for(int loopVariable = 1;loopVariable<elements.length;loopVariable++) { | ||
if(elements[loopVariable] >= maxVal) | ||
maxVal = elements[loopVariable]; | ||
} | ||
|
||
return maxVal; | ||
} | ||
|
||
public double getDistance(double a, double b) { | ||
|
||
return Math.sqrt(Math.abs(Math.pow(a,2) - Math.pow(b,2))); | ||
} | ||
|
||
public double calculateChebychevDistance(double listOfNumbers1[], double listOfNumbers2[]) { | ||
|
||
if(listOfNumbers1.length != listOfNumbers2.length) | ||
return 0; | ||
|
||
double distanceList[] = new double[listOfNumbers1.length]; | ||
for(int loopVariable = 0; loopVariable<listOfNumbers1.length;loopVariable++) { | ||
|
||
distanceList[loopVariable] = getDistance(listOfNumbers1[loopVariable],listOfNumbers2[loopVariable]); | ||
} | ||
|
||
return getMaximumValue(distanceList); | ||
} | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
Chebyshev_distance/Java/aayushKumarJarvis/ChebychevTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
public class TestChebychevDistance { | ||
|
||
@Test | ||
public void chebychevTest() { | ||
|
||
double list1[] = {0,3,4,5}; | ||
double list2[] = {7,6,3,-1}; | ||
|
||
ChebychevDistance objectForChebychev = new ChebychevDistance(); | ||
|
||
double result = objectForChebychev.calculateChebychevDistance(list1,list2); | ||
double answer = 7; | ||
double epsilon = 0.001; | ||
|
||
assertEquals(result,answer,epsilon); | ||
} | ||
} | ||
|