Skip to content

Commit

Permalink
implemented helper calculators
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszpogoda committed Jan 30, 2017
1 parent f864adb commit 5ef25fd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/helper/DistanceCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package helper;

public interface DistanceCalculator {
void add(double a, double b);
double calculate();
}
6 changes: 6 additions & 0 deletions src/helper/MeanCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package helper;

public interface MeanCalculator {
void add(double a);
double calculate();
}
23 changes: 23 additions & 0 deletions src/helper/impl/EuklidesDistanceCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helper.impl;

import helper.DistanceCalculator;

public class EuklidesDistanceCalculator implements DistanceCalculator {

double sumBeforeSquare = 0;

@Override
public void add(double a, double b) {
sumBeforeSquare += Math.pow(a-b, 2);
}

@Override
public double calculate() {
double euklidesDistance = Math.sqrt(sumBeforeSquare);

sumBeforeSquare = 0;

return euklidesDistance;
}

}
26 changes: 26 additions & 0 deletions src/helper/impl/MeanCalaculatorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package helper.impl;

import helper.MeanCalculator;

public class MeanCalaculatorImpl implements MeanCalculator {

double currentSum = 0;
int numberOfItems = 0;

@Override
public void add(double a) {
currentSum += a;
numberOfItems++;
}

@Override
public double calculate() {
double calculatedMean = currentSum/numberOfItems;

currentSum = 0;
numberOfItems = 0;

return calculatedMean;
}

}

0 comments on commit 5ef25fd

Please sign in to comment.