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...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's 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
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
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....
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...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
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