Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-18-2008, 06:38 PM
Member
 
Join Date: Mar 2008
Posts: 32
BHCluster is on a distinguished road
Algorithms and data from a File Problem
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
Code:
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
Code:
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
Code:
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
Code:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding Tabular data from .doc file praveen83 Advanced Java 8 04-03-2008 10:00 AM
Accessing Data from a .txt file Oasis13 New To Java 5 02-01-2008 02:16 AM
Saving data in an XML file Thez New To Java 1 12-08-2007 11:24 PM
Reading Data from a file ramachandran New To Java 2 10-24-2007 09:22 AM
problem with the way to export data to CSV file gonne Java Servlet 0 07-01-2007 04:43 PM


All times are GMT +3. The time now is 10:10 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org