View Single Post
  #5 (permalink)  
Old 04-18-2008, 03:56 PM
BHCluster BHCluster is offline
Member
 
Join Date: Mar 2008
Posts: 32
BHCluster is on a distinguished road
I have added this so it reads the files one by one:

Get Set methods
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
Read file class
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

Test Class
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
Ok my next question is how to use algorithms from above to combine with these classes so they do waht they are suppose to do. Your help is greatly appreciated

Last edited by BHCluster : 04-18-2008 at 05:55 PM.
Reply With Quote