Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have 2 problems with my code. First it only prints off the first four entries I need it to print of 12. Also, how

I have 2 problems with my code. First it only prints off the first four entries I need it to print of 12. Also, how would I fix the calander dates?

Here is the code.

package homework4;

//======================================================================

//

// Title:

// Course: CSC 3020

// Homework: <4>

// Author:

// Date: <3-20-2018>

// Description:

//

// the span of eleven years. This program will output the highest stock date with the volume

// as well as the lowest and the difference in between them.>

//

//======================================================================

// Import classes

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.text.DecimalFormat;

//======================================================================

// class JavaTemplateMainClass

//======================================================================

class MicrosoftMonarchs

{

//declaring all the arrays

public String array[];

public double close[];

public int volume[];

public double open[];

public double high[];

public double low[];

//declaring both size and totalFileData

public long totalFileData=0;

public int size=13;

//main file

public static void main(String[] args)

{

MicrosoftMonarchs data = new MicrosoftMonarchs();

//setting all the arrays equal to the size of the given text

data.array= new String[data.size];

data.close= new double[data.size];

data.volume= new int[data.size];

data.open= new double[data.size];

data.high= new double[data.size];

data.low= new double[data.size];

//read in the text file

data.readTextFile(data);

//print the data from the text file

data.printData(data);

//analyze the data from the text file

data.analyzeData(data);

//creating the chart

data.chartData(data);

}

//implementation of the read in from text file

public void readTextFile(MicrosoftMonarchs objdata)

{

//create data member i and set it to zero

int i=0;

BufferedReader reader;

//a try and catch block

try

{

//reading in the text file

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

String first=reader.readLine();

//while look where it reads in first then checks if it is null

while((first=reader.readLine())!=null)

{

//reading in the first line with commas in between each piece of data

String[] datas=first.split(",");

//assigning the data to the appropriate array

if(datas.length>0&&i

{

objdata.array[i]=datas[0];

objdata.close[i]=Double.parseDouble(datas[1]);

objdata.volume[i]=Integer.parseInt(datas[2]);

objdata.open[i]=Double.parseDouble(datas[3]);

objdata.high[i]=Double.parseDouble(datas[4]);

objdata.low[i]=Double.parseDouble(datas[5]);

}

i++;

objdata.totalFileData++;

}

//stop reading in once there is nothing left from the text file

reader.close();

}

//generic catch block

catch(IOException e)

{

e.printStackTrace();

}

}

//implementation of the printData class

public void printData(MicrosoftMonarchs objdata)

{

//creating a decimal format and a number format

DecimalFormat deci = new DecimalFormat("0.00");

DecimalFormat num = new DecimalFormat("00,000,000");

//create data member i and set it to zero

int i=0;

//Printing out the data

System.out.println("Welcome to Microsoft Monarchs");

System.out.println("-----------------------------");

System.out.println();

System.out.println(objdata.totalFileData+" line(s) read from file 'MicrosoftStockData.txt'");

System.out.println();

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

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

//a for loop which prints out the data four times

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

{

System.out.print(" "+objdata.array[i]+" "+objdata.close[i]+" "+num.format(objdata.volume[i])+" "+objdata.open[i]+" "+deci.format(objdata.high[i])+" "+objdata.low[i]);

System.out.println();

}

}

//implementation of the analyzeData class

public void analyzeData(MicrosoftMonarchs objdata)

{

//creating a decimal format and a number format

DecimalFormat deci =new DecimalFormat("0.00");

DecimalFormat num=new DecimalFormat("00,000,000");

//declare two variables for both high and low

double highData=objdata.close[0];

double lowData=objdata.close[0];

//declare the indexes

int indexA=0;

int indexB=0;

//a for loop which go until i is greater than the condition

for(int i=1; i

{

//if

if(objdata.close[i]> highData)

{

highData=objdata.close[i];

indexB=i;

}

//if

if(objdata.close[i]< lowData)

{

lowData=objdata.close[i];

indexA=i;

}

}

//Prints out the highest and lowest data as well as the volume of stocks traded that day.

System.out.println("Highest closing stock data date: "+objdata.array[indexB]+" and volume: "+num.format(objdata.volume[indexB]));

System.out.println("Lowest closing stock data date: "+objdata.array[indexA]+" and volume: "+num.format(objdata.volume[indexA]));

//creating a new variable

double highDiff=objdata.high[0]-objdata.low[0];

//setting the index equal to zero

indexB=0;

//for loop which will go until condition is met

for(int i=1; i

{

//

if((objdata.high[i]-objdata.low[i])>highDiff)

{

highDiff=objdata.high[i]-objdata.low[i];

indexB=i;

}

}

//prints out the difference between the highest and lowest data

System.out.println("Highest diffrence data: "+objdata.array[indexB]+" and value: "+deci.format(highDiff));

}

public void chartData(MicrosoftMonarchs objdata)

{

}

}

Here is the text 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 2/27/2018,94.2,25842000,95.74,95.84,94.2 2/26/2018,95.42,30167890,94.4,95.45,94.25 2/23/2018,94.06,26294200,92.75,94.07,92.36 2/22/2018,91.73,23613350,92.05,92.73,91.36 2/21/2018,91.49,26664100,92.98,93.3595,91.49 2/20/2018,92.72,30325530,91.475,93.06,91.01 2/16/2018,92,30580470,92.45,93.5,91.8 2/15/2018,92.66,27523410,91.21,92.72,90.62 2/14/2018,90.81,34825510,88.51,90.99,88.41 2/13/2018,89.83,26320030,88.93,90,87.8 2/12/2018,89.13,35657250,88.735,89.78,87.9295

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions