In: Computer Science
Java program
Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations
private
double timeStep; // time between each data point
int numberOfPoints; // number of data samples in array
double [] acceleration = new double [1000]; // acceleration data
double [] velocity = new double [1000]; // calculated velocity data
double [] displacement = new double [1000]; // calculated disp data
saveData will output a new file containing all of the data in
the class. It will have a single String parameter that is the name
of the file to be created. That file should have the timeStep on
the first line, followed by the acceleration, velocity, and
displacement data with all three values for the same index in the
array on one line. There should be a comma and a single space
separating the acceleration value from the velocity value, and the
same between the velocity value and the displacement value. The
number of data samples to be output is less than 1000 and is
contained in numberOfPoints.
The method should create and open the file, put the timeStep into
the file on the first line followed by the information from the
arrays. At the end of the method close the file. In the method, be
sure to catch an IOException error if there are any problems with
the name of the file to be opened.
Program Code
[JAVA]
// Signal class
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
class Signal {
// Field Declarations
private double timeStep;
private int numberOfPoints;
double[] acceleration = new double[1000];
double[] velocity = new double[1000];
double[] displacement = new double[1000];
// Constructor which initializes number of points to 0
public Signal() {
numberOfPoints = 0;
}
// saveData method
public void saveData(String fileName) {
// Opening file & writing using PrintWriter
try {
PrintWriter writer = new PrintWriter(new File(fileName));
// First writing timeStep on first line
writer.write(timeStep + "\n");
// Now writing acceleration, velocity and displacement data of same index in array
for (int i=0; i<numberOfPoints; i++) {
writer.write(acceleration[i] + ", " + velocity[i] + ", " + displacement[i] + "\n");
}
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// Some additional method to see that saveData is working good
public void setNumberOfPoints(int numberOfPoints) {
this.numberOfPoints = numberOfPoints;
}
public void setTimeStep(double timeStep) {
this.timeStep = timeStep;
}
public void setData(double acc, double velocity, double displacement) {
this.acceleration[numberOfPoints] = acc;
this.velocity[numberOfPoints] = velocity;
this.displacement[numberOfPoints] = displacement;
numberOfPoints++; // For next entry
}
}
// A class to test method saveData in Signal class
public class TestSignal {
public static void main(String[] args) {
// adding some data and then storing in file
Signal signal = new Signal();
signal.setTimeStep(3);
signal.setData(10.1, 5.7, 3.4);
signal.setData(5.6, 3.25, 5.8);
signal.setData(8.9, 2.0, 1.3);
signal.saveData("output.txt");
}
}
output.txt
-------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!