Knowee
Questions
Features
Study Tools

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:Calculate the average stock price:Write a method, calculateAveragePrice, that takes the array of stock prices as input and returns the average price of the stocks.Find the maximum stock price:Write a method, findMaximumPrice, that takes the array of stock prices as input and returns the maximum price among all the stocks. Determine the occurrence count of a specific price:Write a method, countOccurrences, that takes the array of stock prices and a target price as input and returns the number of times the target price occurs in the array. Compute the cumulative sum of stock prices:Write a method, computeCumulativeSum, that takes the ArrayList of stock prices as input and returns a new ArrayList containing the cumulative sum of prices at each position.Note:Assume both the array and ArrayList of stock prices is not null and contains at least one element.You are allowed to use loops (for, while) for array and ArrayList.Write the code for the above scenario, including the required methods and their implementations. Remember to use appropriate variable names and follow coding best practices.

Question

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:Calculate the average stock price:Write a method, calculateAveragePrice, that takes the array of stock prices as input and returns the average price of the stocks.Find the maximum stock price:Write a method, findMaximumPrice, that takes the array of stock prices as input and returns the maximum price among all the stocks. Determine the occurrence count of a specific price:Write a method, countOccurrences, that takes the array of stock prices and a target price as input and returns the number of times the target price occurs in the array. Compute the cumulative sum of stock prices:Write a method, computeCumulativeSum, that takes the ArrayList of stock prices as input and returns a new ArrayList containing the cumulative sum of prices at each position.Note:Assume both the array and ArrayList of stock prices is not null and contains at least one element.You are allowed to use loops (for, while) for array and ArrayList.Write the code for the above scenario, including the required methods and their implementations. Remember to use appropriate variable names and follow coding best practices.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the code implementation for the given scenario:

import java.util.ArrayList;

public class StockAnalysis {
    
    public static float calculateAveragePrice(float[] prices) {
        float sum = 0;
        for (float price : prices) {
            sum += price;
        }
        return sum / prices.length;
    }
    
    public static float findMaximumPrice(float[] prices) {
        float maxPrice = prices[0];
        for (float price : prices) {
            if (price > maxPrice) {
                maxPrice = price;
            }
        }
        return maxPrice;
    }
    
    public static int countOccurrences(float[] prices, float targetPrice) {
        int count = 0;
        for (float price : prices) {
            if (price == targetPrice) {
                count++;
            }
        }
        return count;
    }
    
    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;
    }
    
    public static void main(String[] args) {
        float[] stockPrices = {10.5f, 12.3f, 11.8f, 13.2f, 10.9f, 12.5f, 11.7f, 13.8f, 12.1f, 11.5f};
        ArrayList<Float> stockPricesList = new ArrayList<>();
        for (float price : stockPrices) {
            stockPricesList.add(price);
        }
        
        float averagePrice = calculateAveragePrice(stockPrices);
        System.out.println("Average Stock Price: " + averagePrice);
        
        float maximumPrice = findMaximumPrice(stockPrices);
        System.out.println("Maximum Stock Price: " + maximumPrice);
        
        float targetPrice = 12.5f;
        int occurrenceCount = countOccurrences(stockPrices, targetPrice);
        System.out.println("Occurrence Count of " + targetPrice + ": " + occurrenceCount);
        
        ArrayList<Float> cumulativeSum = computeCumulativeSum(stockPricesList);
        System.out.println("Cumulative Sum of Stock Prices: " + cumulativeSum);
    }
}

Please note that this code is written in Java.

This problem has been solved

Similar Questions

Finding the Maximum Stock PriceYou are an analyst tracking the stock prices of a company over the past week (7 days). Write a program that reads the stock prices into an array and finds the maximum stock price during this period.Constraints:NAExample:Sample Input:1001021059998110107Sample Output:110

Initialize an array/list with five integers. Write a program to find the sumand average of these numbers

Problem statementRaj is working on a data analysis project and needs help creating a program that processes a 2D array. This program includes functions to calculate the average value of each row in a 2D array. The program should consist of several functions:calculateRowAverage(cols, row): Calculates the average of a single row in a 2D array.calculateAverages(rows, cols, arr, averages): Computes and stores the averages for each row in the 2D array.displayAverages(rows, averages): Displays the row number and its average value.Input format :The first line of input consists of two integers, rows and cols, representing the dimensions of the 2D array.The second line of input consists of the elements arr[i] of the 2D array, where each element is a double.Output format :The output displays the average value of each row as a double datatype value, rounded off two decimal points in the specified format:Average of each row:Row 1: [row1_avg]Row 2: [row2_avg]Row 3: [row3_avg]Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ rows and cols ≤ 51.0 ≤ arr[i] ≤ 5.0Max_Rows and Cols = 100Sample test cases :Input 1 :3 31.7 2.9 3.54.2 5.7 6.87.2 8.1 9.6Output 1 :Average of each row:Row 1: 2.70Row 2: 5.57Row 3: 8.30Input 2 :2 26.8 1.25.6 8.9Output 2 :Average of each row:Row 1: 4.00Row 2: 7.25Input 3 :5 52.2 3.8 4.3 5.8 9.26.7 7.1 8.8 9.2 10.711.7 12.8 13.7 14.3 15.316.2 17.4 18.2 19.2 20.121.3 22.7 23.4 24.3 25.7Output 3 :Average of each row:Row 1: 5.06Row 2: 8.50Row 3: 13.56Row 4: 18.22Row 5: 23.48

Single File Programming QuestionProblem statementRaj is working on a data analysis project and needs help creating a program that processes a 2D array. This program includes functions to calculate the average value of each row in a 2D array. The program should consist of several functions:calculateRowAverage(cols, row): Calculates the average of a single row in a 2D array.calculateAverages(rows, cols, arr, averages): Computes and stores the averages for each row in the 2D array.displayAverages(rows, averages): Displays the row number and its average value.Input format :The first line of input consists of two integers, rows and cols, representing the dimensions of the 2D array.The second line of input consists of the elements arr[i] of the 2D array, where each element is a double.Output format :The output displays the average value of each row as a double datatype value, rounded off two decimal points in the specified format:Average of each row:Row 1: [row1_avg]Row 2: [row2_avg]Row 3: [row3_avg]Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ rows and cols ≤ 51.0 ≤ arr[i] ≤ 5.0Max_Rows and Cols = 100Sample test cases :Input 1 :3 31.7 2.9 3.54.2 5.7 6.87.2 8.1 9.6Output 1 :Average of each row:Row 1: 2.70Row 2: 5.57Row 3: 8.30Input 2 :2 26.8 1.25.6 8.9Output 2 :Average of each row:Row 1: 4.00Row 2: 7.25Input 3 :5 52.2 3.8 4.3 5.8 9.26.7 7.1 8.8 9.2 10.711.7 12.8 13.7 14.3 15.316.2 17.4 18.2 19.2 20.121.3 22.7 23.4 24.3 25.7Output 3 :Average of each row:Row 1: 5.06Row 2: 8.50Row 3: 13.56Row 4: 18.22Row 5: 23.48

You are given an integer array prices where prices[i] is the price of a given stock on the ith day.On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.Find and return the maximum profit you can achieve.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.