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
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
Get step-by-step solutions from verified subject matter experts
