0% found this document useful (0 votes)
7 views4 pages

Programming Unit 4 Assign

This document contains a Java program for analyzing stock prices, including methods to calculate the average price, find the maximum price, count occurrences of a specific price, and compute the cumulative sum of stock prices. The program uses an array of stock prices and demonstrates its functionality in the main method. It outputs the average price, maximum price, occurrence count of a target price, and the cumulative sum of the stock prices.

Uploaded by

munyendoadam9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Programming Unit 4 Assign

This document contains a Java program for analyzing stock prices, including methods to calculate the average price, find the maximum price, count occurrences of a specific price, and compute the cumulative sum of stock prices. The program uses an array of stock prices and demonstrates its functionality in the main method. It outputs the average price, maximum price, occurrence count of a target price, and the cumulative sum of the stock prices.

Uploaded by

munyendoadam9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Bachelor of Computer Science, University of the People

CS 1102 Programming 1

Dr. Noman Shihadeh

February 27, 2024


import [Link];

public class StockAnalys {

public static float calculateAveragePrice(float[] stockPrice) {


float sum = 0;

for (float price : stockPrices){


sum += price;
}

return sum / [Link];


}

public static float findMaximumPrice(float[] stockPrices){


float maxPrice = stockPrice[0];

for (float price : stockPrices){


if (price > maxPrice){
maxPrice = price;
}
}
return maxPrice;
}

public static int countOccurrences(float[] stockPrices, float


targetPrice) {
int count = 0;

for (float price : stockPrices) {


if (price == targetPrice){
count++;
}

}
return count;
}
public static ArrayList<Float> computCumulativeSum(ArrayList<Float>
stockPrice) {
ArrayList<Float> cumulativeSum = new ArrayList<>();
float sum = 0;

for (float price : stockPrices){


sum += price;
[Link](sum);
}
return cumulativeSum;
}

public static void main(String[]args) {


float[] stockPriceArray = {11.5f, 14.8f, 13.4f, 16.8f, 15.5f,
18.8f, 16.6f, 17.4f, 14.8f, 18.5f};

ArrayList<Float> stockPriceArrayList = new ArrayList<>();


for (float price : stockPriceArray) {
[Link](price);
}

//
float averagePrice = calculateAveragePrice(stockPriceArray);
[Link]("Average Stock Price:" + averagePrice);

//
float maximumPrice = findMaximumPrice(stockPriceArray);
[Link]("Maximum Stock Price: " + maximumPrice);

//
float targetPrice = 14.8f;
int occurenceCount = countOccurrences(stockPriceArray,
targetPrice);
[Link]("Occurence of " + targetPrice ": "
occurenceCount);

// compute cumulative sum of stock price


ArrayList<Float> cumulativeSum =
computeCumulativeSum(stockPriceArrayList);
[Link]("Cumulative Sum of Stock Price:"
+cumulativeSum);
}
}

You might also like