Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You’ve been hired by Microsoft Monarchs to write a Java console application that

ID: 3733853 • Letter: Y

Question

You’ve been hired by Microsoft 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:

Column

Data type

Purpose

Date

String

Trading date

Close

double

Ending share value on that day.

Volume

int

Number of shares traded during that day.

Open

double

Starting share value on that day.

High

double

Highest share value on that day.

Low

double

Lowest share value on that day.

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.

          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.

          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:

          analyzeData – this function determines the following:

                   (1) The date and value of the highest daily stock closing value.

                   (2) The date and value of the lowest daily stock closing value.

                   (3)The date and highest difference between the daily high and low stock values.

                   (4) The closing value of the stock on the latest day of each year. For 2018, this is March 5. For each of the other ten years, this is December 31. Store the years and closing values in two parallel arrays. These arrays may then be used to print and chart this data.

          It then prints the yearly closing values (4) formatted in two columns:

                   ü The year.

                   ü The closing value.

          It then prints the highest (1), lowest (2), and differences (3) formatted in three columns:

                   ü A label.

                   ü A date.

                   ü A closing value or difference.

Format all real numbers to two decimal places. For the following functions, use the JFreeChart library. This was discussed in the Session 12 Notes. See the Chart drawing sample application on Blackboard.

          chartData – this function charts the closing value of the stock on the latest day of each year. The chart is a line chart with the year along the x-axis and the stock value along the y-axis. It calls function createPanel, places the panel in a frame, and shows the frame.

          createPanel – this function call functions createDataset and createChart. It uses class DefaultCategoryDataset instead of class PieDataset.

          createDataset – this function uses class DefaultCategoryDataset instead of class PieDataset. It also uses the arrays created when determining the closing value of the stock on the latest day of each year.

          createChart – this function uses class createLineChart instead of class createPieChart. See web pages www.jfree.org/jfreechart/api/gjdoc/org/jfree/chart/ChartFactory.html#createLineChart:String:String:String:CategoryDataset:PlotOrientation:boolean:boolean:boolean and www.tutorialspoint.com/jfreechart/jfreechart_line_chart.htm for more information.

Here is what the chart should look like:

Column

Data type

Purpose

Date

String

Trading date

Close

double

Ending share value on that day.

Volume

int

Number of shares traded during that day.

Open

double

Starting share value on that day.

High

double

Highest share value on that day.

Low

double

Lowest share value on that day.

Explanation / Answer

Java program:

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

class StockDataAnalyzer {

public String DateAray[];

public double Close[];

public int Volume[];

public double Open[];

public double High[];

public double Low[];

public long totalFileData=0;

public int size=4;

public static void main(String[] args) {

StockDataAnalyzer stockData = new StockDataAnalyzer();

stockData.DateAray = new String[stockData.size];

stockData.Close = new double[stockData.size];

stockData.Volume = new int[stockData.size];

stockData.Open = new double[stockData.size];

stockData.High = new double[stockData.size];

stockData.Low = new double[stockData.size];

stockData.readTextFile(stockData);

stockData.printData(stockData);

stockData.analyzeData(stockData);

}

public void readTextFile(StockDataAnalyzer objStockData)

{

int i=0;

BufferedReader reader;

try

{

reader = new BufferedReader(new FileReader("MicrosoftStockData.txt"));

String line = reader.readLine();

while (line != null)

{  

line = reader.readLine();

String[] data = line.split(",");

if(data.length>0 && i<objStockData.size)

{

objStockData.DateAray[i]=data[0];

objStockData.Close[i]= Double.parseDouble(data[1]);

objStockData.Volume[i]= Integer.parseInt(data[2]);

objStockData.Open[i]=Double.parseDouble(data[3]);

objStockData.High[i]=Double.parseDouble(data[4]);

objStockData.Low[i]=Double.parseDouble(data[5]);

}

i++;

objStockData.totalFileData++;

}

reader.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

public void printData(StockDataAnalyzer objStockData)

{

int i=0;

System.out.println(objStockData.totalFileData+" data lines read from 'MicrosoftStockData.txt'");

System.out.println("First 12 days of data ranging from"+objStockData.DateArray[0]+" - "+objStockData.DateArray[3]);

System.out.println("Date"+" "+"Close"+ " "+"Volume"+" "+"Open"+" "+"High"+" "+"Low");

System.out.println();  

for(i=0;i<4;i++)

{

System.out.print(objStockData.DateAray[i]+" "+objStockData.Close[i]+objStockData.Volume[i]+" "+objStockData.Open[i]+" "+objStockData.High[i]+" "+objStockData.Low[i]);

System.out.println();

}

}

public void analyzeData(StockDataAnalyzer objStockData)

{

double HighestData = objStockData.Close[0];

double lowestData=objStockData.Close[0];

int indexH=0;

int indexL=0;

for(int i=1;i < objStockData.Close.length;i++)

{

if(objStockData.Close[i] > HighestData)

{

HighestData = objStockData.Close[i];

indexH=i;

}

if(objStockData.Close[i] < lowestData)

{

lowestData = objStockData.Close[i];

indexL=i;

}

}

System.out.println("Highest closing stock data date: "+objStockData.DateArray[indexH] +" and volume: "+objStockData.Volume[indexH]);

System.out.println("Lowest closing stock data date: "+objStockData.DateArray[indexL] +" and volume: "+objStockData.Volume[indexL]);

double HighestDiffrence = objStockData.High[0]-objStockData.Low[0];

indexH = 0 ;

for(int i=1;i < objStockData.Close.length;i++)

{

if((objStockData.High[i]-objStockData.Low[i])>HighestDiffrence)

{

HighestDiffrence = objStockData.High[i]-objStockData.Low[i];

indexH=i;

}

}

System.out.println("Highest diffrence date: "+objStockData.DateArray[indexH] +" and value: "+ HighestDiffrence);

}

}