Question: import java.util.ArrayList; public class StockAnalysis { // Method to calculate the average stock price public static double calculateAveragePrice(double[] prices) { double sum = 0; for

import java.util.ArrayList; public class StockAnalysis { // Method to calculate the average stock price public static double calculateAveragePrice(double[] prices) { double sum = 0; for (double price : prices) { sum += price; } return sum / prices.length; } // Method to find the maximum stock price public static double findMaximumPrice(double[] prices) { double maxPrice = prices[0]; for (double price : prices) { if (price > maxPrice) { maxPrice = price; } } return maxPrice; } // Method to count the occurrences of a specific price in the array public static int countOccurrences(double[] prices, double targetPrice) { int count = 0; for (double price : prices) { if (price == targetPrice) { count++; } } return count; } // Method to compute the cumulative sum of stock prices public static ArrayList computeCumulativeSum(ArrayList prices) { ArrayList cumulativeSum = new ArrayList<>(); double sum = 0; for (double price : prices) { sum += price; cumulativeSum.add(sum); } return cumulativeSum; } public static void main(String[] args) { // Example stock prices for 10 days double[] stockPricesArray = {100.5, 102.3, 101.2, 105.0, 110.5, 108.8, 107.3, 109.5, 111.2, 112.8}; // ArrayList of stock prices for 10 days ArrayList stockPricesList = new ArrayList<>(); for (double price : stockPricesArray) { stockPricesList.add(price); } // Calculate average price double averagePrice = calculateAveragePrice(stockPricesArray); System.out.println("Average Stock Price: " + averagePrice); // Find maximum price double maximumPrice = findMaximumPrice(stockPricesArray); System.out.println("Maximum Stock Price: " + maximumPrice); // Count occurrences of a specific price double targetPrice = 110.5; int occurrences = countOccurrences(stockPricesArray, targetPrice); System.out.println("Occurrences of price " + targetPrice + ": " + occurrences); // Compute cumulative sum of stock prices ArrayList cumulativeSum = computeCumulativeSum(stockPricesList); System.out.println("Cumulative Sum of Stock Prices: " + cumulativeSum); } }

Explanation

Method to Calculate Average Stock Price (calculateAveragePrice):

  • This method loops through the array of stock prices, sums them up, and then divides by the length of the array to return the average price.

Method to Find Maximum Stock Price (findMaximumPrice):

  • This method iterates through the array, compares each price with the current maximum, and updates the maximum price if a higher price is found.

Method to Count Occurrences of a Specific Price (countOccurrences):

  • This method takes a target price and counts how many times this price appears in the array by iterating through the stock prices.

Method to Compute Cumulative Sum of Stock Prices (computeCumulativeSum):

  • This method takes an ArrayList of stock prices, iterates through them while keeping a running sum, and stores the cumulative sum at each step in a new ArrayList.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!