0% found this document useful (0 votes)
27 views

Programming Assignment Unit 4

The document details four tasks to analyze stock price data: calculate average price, find maximum price, count occurrences of a target price, and compute the cumulative sum of prices. It provides methods to implement each task and tests them on sample stock price data.

Uploaded by

albertmutoya57
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Programming Assignment Unit 4

The document details four tasks to analyze stock price data: calculate average price, find maximum price, count occurrences of a target price, and compute the cumulative sum of prices. It provides methods to implement each task and tests them on sample stock price data.

Uploaded by

albertmutoya57
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT-4 OPENING STOCK PRICES

import java.util.ArrayList;

public class StockAnalyzer {

// Task 1: Calculate the average stock price


public static double calculateAveragePrice(int[] stockPrices) {
if (stockPrices.length == 0) {
return 0.0; // To handle an empty array
}

int sum = 0;
for (int price : stockPrices) {
sum += price;
}

return (double) sum / stockPrices.length;


}

// Task 2: Find the maximum stock price


public static int findMaximumPrice(int[] stockPrices) {
if (stockPrices.length == 0) {
return 0; // To handle an empty array
}

int maxPrice = stockPrices[0];


for (int price : stockPrices) {
maxPrice = Math.max(maxPrice, price);
}

return maxPrice;
}

// Task 3: Determine the occurrence count of a specific price


public static int countOccurrences(int[] stockPrices, int targetPrice) {
int count = 0;
for (int price : stockPrices) {
if (price == targetPrice) {
count++;
}
}
return count;
}

// Task 4: Compute the cumulative sum of stock prices


public static ArrayList<Integer> computeCumulativeSum(ArrayList<Integer> stockPrices) {
ArrayList<Integer> cumulativeSum = new ArrayList<>();
int sum = 0;

for (int price : stockPrices) {


sum += price;
cumulativeSum.add(sum);
}

return cumulativeSum;
}

public static void main(String[] args) {


int[] dailyStockPrices = {100, 150, 120, 180, 20, 150, 220};
ArrayList<Integer> arrayListStockPrices = new ArrayList<>(dailyStockPrices.length);

for (int price : dailyStockPrices) {


arrayListStockPrices.add(price);
}

// Task 1: Calculate the average stock price


double averagePrice = calculateAveragePrice(dailyStockPrices);
System.out.println("Average Stock Price: " + averagePrice);

// Task 2: Find the maximum stock price


int maxPrice = findMaximumPrice(dailyStockPrices);
System.out.println("Maximum Stock Price: " + maxPrice);

// Task 3: Determine the occurrence count of a specific price


int targetPrice = 150;
int occurrenceCount = countOccurrences(dailyStockPrices, targetPrice);
System.out.println("Occurrences of " + targetPrice + ": " + occurrenceCount);

// Task 4: Compute the cumulative sum of stock prices


ArrayList<Integer> cumulativeSumList = computeCumulativeSum(arrayListStockPrices);
System.out.println("Cumulative Sum of Stock Prices: " + cumulativeSumList);
}
}

You might also like