Skip to content

Commit

Permalink
Merge pull request kennyledet#415 from aayushKumarJarvis/master
Browse files Browse the repository at this point in the history
Adding Java Implementation of Chebychev Distance
  • Loading branch information
patrickyevsukov committed Aug 24, 2014
2 parents 3e0fb81 + 0ffadd3 commit 0e29b8f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Chebyshev_distance/Java/aayushKumarJarvis/ChebychevDistance.java
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 Chebyshev_distance/Java/aayushKumarJarvis/ChebychevTest.java
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);
}
}

0 comments on commit 0e29b8f

Please sign in to comment.