I working through a few exercises from an academic programing book. The task is to implement 2 vectors and calculate the Euclidean distance between thereof. No this is not Home Work, rather self studying.
I'm seeking some feedback on the correctness of my distance implementation.
public class EuclideanDist
{
public static void main(String[] args)
{
EuclideanDist euc = new EuclideanDist();
Random rnd = new Random();
int N = Integer.parseInt(args[0]);
double[] a = new double[N];
double[] b = new double[N];
double[] x = new double[N];
euc.print(euc.init(a, rnd));
euc.print(euc.init(b, rnd));
print(euc.distance(a, b, x));
}
private double[] init(double[] src, Random rnd)
{
for(int i = 0; i < src.length; i++)
{
src[i] = rnd.nextDouble();
}
return src;
}
private double[] distance(double[] a, double[] b, double[] x)
{
double diff;
int N = a.length;
for(int i = 0; i < N; i++)
{
diff = a[i] - b[i];
x[i] = Math.sqrt(diff * diff);
}
return x;
}
private static void print(double[] x)
{
int N = x.length;
for(int j = 0; j < N; j++)
System.out.print(" " + x[j] + " ");
System.out.println();
}
}