consider the followinfg statement
You are working on a data analysis project where you need to process an array of opening
stock prices containing 10 days of data as float datatype.
Question:
Consider a scenario where you have been given an array of integers representing the daily
stock prices of a company for a given period. You are also provided with an ArrayList of
stock prices for the same period You are required to implement a program that performs the
following tasks:
What do you think would be the correct explanation of the desired outcomes:
Heres my code
import java.util.ArrayList;
public class StockAnalysis {
// Method to calculate the average stock price
public static double calculateAveragePrice(float[] prices) {
double sum = 0;
for (float price : prices) {
sum += price;
}
return sum / prices.length;
}
// Method to find the maximum stock price
public static float findMaximumPrice(float[] prices) {
float maxPrice = prices[0];
for (float price : prices) {
if (price > maxPrice) {
maxPrice = price;
}
}
return maxPrice;
}
// Method to determine the occurrence count of a specific price
public static int countOccurrences(float[] prices, float targetPrice) {
int count = 0;
for (float price : prices) {
if (price == targetPrice) {
count++;
}
}
return count;
}
// Method to compute the cumulative sum of stock prices
public static ArrayList<Float> computeCumulativeSum(ArrayList<Float> prices) {
ArrayList<Float> cumulativeSum = new ArrayList<>();
float sum = 0;
for (float price : prices) {
sum += price;
cumulativeSum.add(sum);
}
return cumulativeSum;
}
// Sample usage
public static void main(String[] args) {
// Sample array of stock prices
float[] stockPrices = {1890.5f, 1500.2f, 1000.9f, 1000.0f, 1000.0f, 1200.0f, 1100.8f, 2100.9f, 1400.2f, 2500.5f};
// Sample ArrayList of stock prices
ArrayList<Float> stockPricesList = new ArrayList<>();
//loop through the array and add stockprice prices in the array to the stockPrices variable
for (float price : stockPrices) {
stockPricesList.add(price);
}
// Calculate and print the average stock price
System.out.println("nnAverage Stock Price: " + calculateAveragePrice(stockPrices));
// Find and print the maximum stock price
System.out.println("Maximum Stock Price: " + findMaximumPrice(stockPrices));
// Determine and print the occurrence count of a specific price, in this case 1000.0
float targetPrice = 1000.0f;
System.out.println("Occurrences of " + targetPrice + ": " + countOccurrences(stockPrices, targetPrice));
// Compute and print the cumulative sum of stock prices
System.out.println("Cumulative Sum of Stock Prices: " + computeCumulativeSum(stockPricesList));
}
}
Thomson Thom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.