As the topic name said I have a Algorithms and data from a File Problem. To be more precise I can read and display the contents of the file on the screen shown bellow:
experiment.java
package get.set; // packaged for reuse
public class experiment
{
private double experiment1;
private double experiment2;
private double experiment3;
// no-argument constructor calls other constructor with default values
public experiment()
{
this ( 0.0, 0.0, 0.0 ); // call four-argument constructor
} // end no-argument AccountRecord constructor
// initialize a record
public experiment( double exp1, double exp2, double exp3 )
{
setExperiment1( exp1 );
setExperiment2( exp2 );
setExperiment3( exp3 );
} // end three-argument experiment constructor
// set experiment1
public void setExperiment1( double exp1 )
{
experiment1 = exp1;
} // end method setExperiment1
// get account number
public double getExperiment1()
{
return experiment1;
} // end method getExperiment1
// set experiment2
public void setExperiment2( double exp2 )
{
experiment2 = exp2;
} // end method setExperiment2
// get first name
public double getExperiment2()
{
return experiment2;
} // end method getExperiment2
// set set experiment3
public void setExperiment3( double exp3 )
{
experiment3 = exp3;
} // end method setExperiment3
// get experiment3
public double getExperiment3()
{
return experiment3;
} // end method getExperiment3
} // end class experiment
readFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import get.set.experiment;
public class readFile
{
private Scanner input;
// enable user to open file
public void openFile()
{
try
{
input = new Scanner( new File("Data_Set_1.txt") );
} // end try
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file." );
System.exit( 1 );
} // end catch
} // end method openFile
// read record from file
public void readRecords()
{
// object to be written to screen
experiment record = new experiment();
System.out.printf( "%10s%15s%15s\n", "Experiment 1",
" Experiment 2", " Experiment 3" );
try // read records from file using Scanner object
{
while ( input.hasNext() )
{
record.setExperiment1( input.nextDouble() ); // read experiment1
record.setExperiment2( input.nextDouble() ); // read experiment2
record.setExperiment3( input.nextDouble() ); // read experiment3
// display record contents
System.out.printf( "%10f%15f%15f\n",
record.getExperiment1(), record.getExperiment2(),record.getExperiment3());
} // end while
} // end try
catch ( NoSuchElementException elementException )
{
System.err.println( "File improperly formed." );
input.close();
System.exit( 1 );
} // end catch
catch ( IllegalStateException stateException )
{
System.err.println( "Error reading from file." );
System.exit( 1 );
} // end catch
} // end method readRecords
// close file and terminate application
public void closeFile()
{
if ( input != null )
input.close(); // close file
} // end method closeFile
} // end class readFile
runReadFileTest.java
public class runReadFileTest
{
public static void main( String args[] )
{
readFile application = new readFile();
application.openFile();
application.readRecords();
application.closeFile();
} // end main
} // end class runReadFileTest
Now my issue is that I can not implement the algorithms which I wrote for the simpler version of this program (which works as well).
numbers.java
import java.io.*;
import static java.lang.Math.*;
class numbers2
{
public static void Manhattan(double[][] a,double[][] b,int n,int n2)
{
int q=n2;
int c=n;
for(int m=0;m<q;m++)
{
for(int h=0;h<q;h++)
{
double d=0;
double d2=0;
for(int j=0;j<c;j++)
{
d2=a[m][j]-a[h][j];
if(d2<0)
d2=d2*-1;
d=d+d2;
}
b[m][h]=d;
}
}
}
public static double Euclidean(double[] array1, double[] array2)
{
//Store the numbers of the both arrays
double x1= array1[0];
double y1=array2[0];
double x2= array1[1];
double y2= array2[1];
double x3=array1[2];
double y3=array2[2];
//Compute the minus part of the formula
double minusResult1 = y1-x1;
double minusResult2 = y2-x2;
double minusResult3 = y3-x3;
//Square the answers
double square1 = pow(minusResult1, 2);
double square2 = pow(minusResult2, 2);
double square3 = pow(minusResult3, 2);
//Sum the squares
double sumOfSquares = square1+square2+square3;
//Get the square root of the sumOfSquares
double finalResult = sqrt(sumOfSquares);
return finalResult;
}
public static double Manhattan(double[] array1, double[] array2)
{
//Store the numbers of the both arrays
double x1= array1[0];
double y1=array2[0];
double x2= array1[1];
double y2= array2[1];
double x3=array1[2];
double y3=array2[2];
//Compute the minus part of the formula
double minusResult1 = y1-x1;
double minusResult2 = y2-x2;
double minusResult3 = y3-x3;
//Square the answers
double abs1 = abs(minusResult1 );
double abs2 = abs(minusResult2);
double abs3 = abs(minusResult3 );
//Sum the squares
double sumOfAbs = abs1+abs2+abs3;
//Get the square root of the sumOfAbs
double finalResult = sqrt(sumOfAbs);
return finalResult;
}
public static double Pearson(double[] array1, double[] array2)
{
//Store the numbers of the both arrays
double x1= array1[0];
double y1=array2[0];
double x2= array1[1];
double y2= array2[1];
double x3=array1[2];
double y3=array2[2];
//Compute the minus part of the formula
double mean1 = (x1 + x2 + x3)/3;
double mean2 = (y1 + y2 + y3)/3;
double t = (x1 - mean1)*(y1 - mean2) + (x2 - mean1)*(y2 - mean2) + (x3 - mean1)*(y3 - mean2);
//Square the answers
double square1 = sqrt((x1 - mean1)*(x1 - mean1) + (x2 - mean1)*(x2 - mean1) + (x3 - mean1)*(x3 - mean1));
double square2 = sqrt((y1 - mean2)*(y1 - mean2) + (y2 - mean2)*(y2 - mean2) + (y3 - mean2)*(y3 - mean2));
//Get the square root of the sumOfSquares
double finalResult = t/(square1*square2);
return finalResult;
}
public static void main(String[] args)
{
double[] gene={-9.30, 5.65, -5.02};
double[] gene1={-4.10, 5.68, 0.56};
double EuclideanDist = Euclidean(gene, gene1);
System.out.println("Euclidean distance = " + EuclideanDist);
double ManhattanDist = Manhattan(gene, gene1);
System.out.println("Manhattan = " + ManhattanDist);
double PearsonDist = Pearson(gene, gene1);
System.out.println("Pearson = " + PearsonDist);
}
}
If anybody know how to implement those three algorithms (Euclidean, Manhattan, Pearson) into the the program above please help. I greatly appreciate any help.