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...
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...
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...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT