0% found this document useful (0 votes)
3 views3 pages

Programming Assignment 4

The document outlines a programming assignment for CS 1102-01 that involves calculating stock prices in Java. It details four main tasks: calculating the average stock price, finding the maximum stock price, counting occurrences of a specific price, and computing the cumulative sum of stock prices. Each task is accompanied by sample code demonstrating the implementation of the respective functionality.

Uploaded by

onitiloolukayode
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)
3 views3 pages

Programming Assignment 4

The document outlines a programming assignment for CS 1102-01 that involves calculating stock prices in Java. It details four main tasks: calculating the average stock price, finding the maximum stock price, counting occurrences of a specific price, and computing the cumulative sum of stock prices. Each task is accompanied by sample code demonstrating the implementation of the respective functionality.

Uploaded by

onitiloolukayode
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

Programming Assignment Unit 4

CS 1102-01 Programming 1 - AY2026-T4

*Stock Price Calculations in Java*

For this week's Unit 4 Programming Assignment, I worked through four main
tasks with stock price data. Here’s how I approached each one:

*1. Calculating the average stock price*

To get the average, we just need to add up all the stock prices and then
divide by how many prices there are. It’s the same idea as finding the
average of test scores.

public static float calculateAveragePrice(float[] prices) {

float sum = 0;

for (float price : prices) {

sum += price;

return sum / [Link];

The loop goes through each price, adds it to `sum`, and at the end we divide
by the total number of elements.

*2. Finding the maximum stock price*

To find the highest price, we start by assuming the first price is the max.
Then we check each price in the array. If we find one that’s bigger, we
update our `maxPrice`.
public static float findMaximumPrice(float[] prices) {

float maxPrice = prices[0];

for (float price : prices) {

if (price > maxPrice) {

maxPrice = price;

return maxPrice;

This way, after one pass through the array, `maxPrice` holds the largest
value.

*3. Counting how often a specific price appears*

Sometimes we want to know how many times a certain price shows up. For
that, we just loop through the array and keep a counter. Every time we see
the `targetPrice`, we add 1 to `count`.

public static int countOccurrences(float[] prices, float targetPrice) {

int count = 0;

for (float price : prices) {

if (price == targetPrice) {

count++;

return count;

}
*4. Computing the cumulative sum of stock prices*

For the cumulative sum, we want a running total as we move through the
list. So the first element is just the first price, the second element is price1 +
price2, the third is price1 + price2 + price3, and so on. Using an `ArrayList`
works well here because we’re building the result as we go.

import [Link];

public static ArrayList<Float> computeCumulativeSum(ArrayList<Float>


prices) {

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

float sum = 0;

for (float price : prices) {

sum += price;

[Link](sum);

return cumulativeSum;

*Wrapping up*

That covers all four parts. Each method uses a simple loop to go through the
prices and do exactly what the problem asked. The code is straightforward
and should handle any basic array or ArrayList of stock prices.

You might also like