1
Programming Assignment Unit 4
In the realm of financial data analysis, efficiently processing and analyzing stock prices is
essential for identifying trends and making informed investment decisions. This assignment
explores the use of both arrays and ArrayLists in Java to process and analyze ten days of opening
stock prices represented as floating-point numbers. Through the implementation of basic yet
powerful methods, this project demonstrates the practical applications of programming in Java in
handling real-world financial data. The key operations include calculating the average stock
price, finding the maximum price, counting specific occurrences, and computing the cumulative
sum.
To begin with, the calculateAveragePrice method takes an array of float values as input
and calculates the mean by summing all values and dividing by the length of the array. This
operation is vital in assessing the general price trend over the observed period. Similarly, the
findMaximumPrice method iterates through the array to identify the highest value, which
provides insight into peak performance. The countOccurrences method introduces a more
targeted analysis by checking how many times a specific price point occurs, allowing for
frequency analysis of key support or resistance levels. Lastly, the computeCumulativeSum
method utilizes an ArrayList to generate a running total of prices, giving analysts a sense of
momentum or growth over time.
These operations not only demonstrate fundamental programming skills such as the use
of loops, conditionals, and data structures but also highlight how Java can be leveraged for
applied financial analysis. As emphasized by Eck (2022), understanding the capabilities and
limitations of different data structures is critical when developing efficient and readable
2
programs. Arrays, for instance, are fixed in size and offer faster access, making them ideal for
consistent-sized datasets. In contrast, ArrayLists provide dynamic resizing and flexible
manipulation, which is particularly useful for operations like cumulative sum computation where
size may evolve during runtime.
In conclusion, by implementing methods to calculate average, maximum, occurrence
frequency, and cumulative sums, this project not only reinforces key programming concepts but
also bridges the gap between theoretical knowledge and practical data analysis in finance. The
dual use of arrays and ArrayLists illustrates the importance of choosing the right data structure
based on the nature of the task emphasizing both performance and adaptability.
import [Link];
public class StockAnalysis {
// Method to calculate average price
public static float calculateAveragePrice(float[] prices) {
float sum = 0;
for (float price : prices) {
sum += price;
return sum / [Link];
}
3
// Method to find maximum price
public static float findMaximumPrice(float[] prices) {
float max = prices[0];
for (float price : prices) {
if (price > max) {
max = price;
return max;
// Method to count occurrences of a specific price
public static int countOccurrences(float[] prices, float target) {
int count = 0;
for (float price : prices) {
if (price == target) {
count++;
}
4
return count;
// Method to compute cumulative sum from an ArrayList
public static ArrayList<Float> computeCumulativeSum(ArrayList<Float> prices) {
ArrayList<Float> result = new ArrayList<>();
float sum = 0;
for (float price : prices) {
sum += price;
[Link](sum);
return result;
// Sample usage
public static void main(String[] args) {
float[] stockArray = {102.5f, 104.2f, 100.3f, 101.0f, 105.6f, 103.9f, 98.4f, 100.0f, 104.8f,
102.2f};
5
ArrayList<Float> stockList = new ArrayList<>();
for (float price : stockArray) {
[Link](price);
[Link]("Average Price: " + calculateAveragePrice(stockArray));
[Link]("Maximum Price: " + findMaximumPrice(stockArray));
[Link]("Occurrences of 100.0: " + countOccurrences(stockArray, 100.0f));
[Link]("Cumulative Sum: " + computeCumulativeSum(stockList));
Reference
Eck, D. J. (2022). Introduction to programming using Java version 9, JavaFX edition. Licensed
under CC 4.0. [Link] (Accessed: 16 July 2025)