Modify java code for Hw5 (stock,each csv file has headings: Date,Open,High,Low,Close, Adj Close,Volume) to count and print the number of days there is gain (increase from open to close price) of 2% or more of stock price. Also count and print the number of days the High is 30% bigger than Low. Also, find and print the average volume for the trading days where there is gain. Make sure each print occupies a single line, and all lines are lined up such that the label for each information requested is left aligned, and the decimal points are all aligned.
import java.util.*; import java.io.*; public class StockData { public static double average(ArrayList prices) { double total = 0; double average; for (int i = 0; i < prices.size();="" i++)="" {="" total="total" +="" prices.get(i);="" }="" if="" (prices.size()=""> 0) { average = total / prices.size(); return average; } else { return 0; } } public static double min(ArrayListprices) { double min = prices.get(0); for (int i = 1; i < prices.size();="" i++)="" {="" if="" (min=""> prices.get(i)) { min = prices.get(i); } } return min; } public static double max(ArrayListprices) { double max = prices.get(0); for (int i = 1; i < prices.size();="" i++)="" {="" if="" (max="">< prices.get(i))="" {="" max="prices.get(i);" }="" }="" return="" max;="" }="" public="" static="" double="">prices) { double total = 0; for(int i = 0; i < prices.size();="" i++)="" {="" double="" subtract="prices.get(i)" -="" average(prices);="" double="" numerator="Math.pow(subtract," 2);="" total="total" +="" numerator;="" }="" double="" divison="total" (prices.size()="" -="" 1);="" double="" stdev="Math.sqrt(divison);" return="" stdev;="" }="" public="" static="" void="" main(string="" []="" args)="" throws="" filenotfoundexception{="" scanner="" in="new" scanner(system.in);="" system.out.println("enter="" stock="" symbol:="" ");="" string="" stock="in.next();" stock="stock.toUpperCase();" try="" {="" file="" inputfile="new" file(stock="" +="" ".csv");="" scanner="" stockfile="new" scanner(inputfile);="" stockfile.usedelimiter(",");="" stockfile.nextline();="" skip="" the="" header="" line=""> prices = new ArrayList(); while (stockFile.hasNextLine()) { String line = stockFile.nextLine(); String [] data = line.split(","); String price = data[4]; //closing price double p = Double.parseDouble(price); prices.add(p); } stockFile.close(); System.out.printf("Average price: %8.2f \n", average(prices)); System.out.printf("Minimum price: %8.2f \n", min(prices)); System.out.printf("Maximum price: %8.2f \n", max(prices)); System.out.printf("Standard Deviation: %6.2f \n", standardDev(prices)); } catch(FileNotFoundException e) { System.out.println("File not found."); } catch(NumberFormatException e) { System.out.println("Number format error"); } } }
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here