Question

In: Computer Science

Java program Create a public method named saveData for a class named Signal that will hold...

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.

Solutions

Expert Solution

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!!!


Related Solutions

Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor 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 The constructor...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
JAVA Implement a public class method named comparison on a public class Compare that accepts two...
JAVA Implement a public class method named comparison on a public class Compare that accepts two Object arguments. It should return 0 if both references are equal. 1 if both objects are equal. and -1 otherwise. (SUPER IMPORTANT) Either reference can be null, so you'll need to handle those cases carefully! Here is what I have so far: public class Compare { public static int comparison(Object a, Object b) {   if (a == null || b == null) {    return...
In java: -Create a class named Animal
In java: -Create a class named Animal
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
for java!! Make a public class Value that provides a static method named test. test takes...
for java!! Make a public class Value that provides a static method named test. test takes a single int argument and returns an anonymous object that implements the following Absolute interface: public interface Absolute { int same(); int opposite(); } -------------------------------------------------------------------------- The returned object should implement same so that it returns the passed int as it is, and opposite so that it returns the passed int as the int * -1. For example Absolute first = Value.test(-90) Absolute second =...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT