Question

In: Computer Science

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 will have a single String parameter that is the name of the file with the data. That file has the timestep on the first line, followed by the data that should be put into the acceleration array, 1 data sample per line. The number of data samples is not known or in the file, but is less than 1000.

The constructor should open the file, put the value into timestep, and fill acceleration with the data in the file. At the end of the constructor, numberOfPoints should have the number of data points entered into acceleration. Close the file at the end of the constructor. Do not change either velocity or displacement.

In the constructor, be sure to catch an IOException error if the name of the file to be opened does not exist

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Signal {
private double timeStep;
private int numberOfPoints;
private double [] acceleration = new double [1000];
private double [] velocity = new double [1000];
private double [] displacement = new double [1000];
  
public Signal(String filename)
{
Scanner fileReader;
try
{
fileReader = new Scanner(new File(filename));
timeStep = Double.parseDouble(fileReader.nextLine().trim());
numberOfPoints = 0;
while(fileReader.hasNextLine())
{
acceleration[numberOfPoints++] = Double.parseDouble(fileReader.nextLine().trim());
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(filename + " could not be found! Exiting..");
System.exit(0);
}
}
  
public void displayData()
{
if(numberOfPoints == 0)
{
System.out.println("No acceleration data has been recorded!\n");
return;
}
System.out.println("Timestep recorded: " + String.format("%.3f\n", timeStep));
System.out.print("There are total " + numberOfPoints + " acceleration data read from file.\n"
+ "Acceleration data: ");
for(int i = 0; i < numberOfPoints; i++)
System.out.print(String.format("%.3f", acceleration[i]) + " ");
System.out.println();
}
  
public static void main(String[] args) {
new Signal("signal.txt").displayData();
}
}

******************************************************* SCREENSHOT *********************************************************

INPUT FILE (signal.txt) - This file needs to be created before running the code and this file should be created within the same project directory where the above Signal.java file will be residing.

CONSOLE OUTPUT :


Related Solutions

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...
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
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG),...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG), the course number (for example, 101), the credits (for example, 3), and the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data. Create a subclass named LabCourse that adds $50 to the course fee....
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
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...
Develop a java program with a class named friend with data members like name, phno and...
Develop a java program with a class named friend with data members like name, phno and hobby. Use Parameterized constructor to create two friend objects and invoke checkhobby method which takes only one parameter, to find whether they have same hobby or different hobbies
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...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT