Page t You\'ve been hired by Microsofit Monarchs to write a Java console applica
ID: 3728393 • Letter: P
Question
Page t You've been hired by Microsofit Monarchs to write a Java console application that analyzes their stock data over the past eleven years. Use text file MicrosoftStockData.txt as input to the application. The first line of the file contains the column headers. The rest of the lines contain daily Microsoft stock data. Values are separated by commas into the following columns: Data type String double int double double double Purpose Trading date Ending share value on that day Number of shares traded during that day Starting share value on that day Highest share value on that day Lowest share value on that day Column Date Close Volume Open High Low Here are the first five lines of the file: date,close, volume, open,high,low 3/5/2018,93.64,23787950,92.34,94.27,92.26 3/2/2018,93.05,32815660, 91.58,93.15,90.86 3/1/2018,92.85,36979700,93.99,94.57,91.84 2/28/2018, 93.77,30011960,94.84,95.705,93.63 The application reads the data into arrays, prints the first several rows of the data, and then analyzes and charts the data. Create the following functions: main-this function defines one array for each column of data in the input file such that there are six parallel arrays. It then calls these functions in sequence: readTextFile, printData, analyzeData, and chartData. e readTextFile- this function reads each line of the file, parses it into six columns, and stores each token in its respective array. It skips past the first line which contains the column headers. e printData - this function prints two header lines. The first indicates the range of dates in the data. The second shows the column headers. It then prints the first twelve lines of the data. Here is sample output:Explanation / Answer
I'm answering the part which basically asks to read lines from file, store the column value into 6 arrays. Please note that I don't have access to JFree chart library therefore won't be able to plot the chart. Also the question asks for different functions but I've combined all the necessary tasks into main function so that reader can easily understand the code. The code is below
import java.io.*;
class StockAnalyser{
public static void main(String[] args){
//create instance of fileinput stream to open the file
FileInputStream fileInputStream = new FileInputStream(“//path to file”);
//read single line using BufferedReader
BufferedReader bufferedReader = new BufferedReader(fileInputStream);
//this will read the lines of the file, since we are not concerned with the header, we can // skip this first line
String line = bufferedReader.readLine();
//create six arrays for storing each column
String dateArray[]; //array for storing the dates
double closeArray[]; //array for storing the ending share value
int volumeArray[]; //array for number of shares traded
double openArray[]; //array for storing opening value of the stock
double highArray[]; //array for storing highest share value on that day
double lowArray[]; //array for storing lowest share value on that day
//loop over the lines of the file and use the counter for storing values in array
int i=0;
while(line != null) {
line = bufferedReader.readLine();
//we will split the string and use temporary array to store the value of the substrings
Array tempString[] = line.split(“,”); //since each line is separated by comma therefore using this regex to split the //string and storing the value in temporary string
dateArray[i] = tempString[0];
closeArray[i] = Double.parseDouble(tempString[1]); //string needs to be converted to double and tempString[1] //has the value for storing ending share value as defined in structure
volumeArray[i] = Int.parseInt(tempString[2]));
openArray[i] = Double.parseDouble(tempString[3]);
highArray[i] = Double.parseDouble(tempString[4]);
lowArray[i] = Double.parseDouble(tempString[5]);
//increment the counter
i++;
}
//loop is closed and now we have all the column values in 6 arrays
//close the fileinputstream and buffered reader now
bufferedReader.close();
fileInputStream.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.