Programming 1 Assignment Unit 4
Programming 1 Assignment Unit 4
import java.util.ArrayList;
float[] stockPrices = {10.5f, 12.3f, 11.2f, 13.1f, 11.8f, 12.9f, 14.7f, 12.4f, 15.6f, 14.2f};
stockPricesArrayList.add(price);
float sum = 0;
sum += price;
}
return sum / prices.length;
maxPrice = price;
return maxPrice;
int count = 0;
if (price == targetPrice) {
count++;
return count;
sum += price;
cumulativeSumArrayList.add(sum);
return cumulativeSumArrayList;
Output image
In this code:
- The `calculateAveragePrice` method calculates the average price by summing all the prices and dividing
by the number of prices.
-The `findMaximumPrice` method finds the maximum price by iterating through the array and keeping
track of the maximum value found so far.
-The `countOccurrences` method counts the occurrences of a specific price by iterating through the
array and incrementing a counter each time it finds the target price.
-The `computeCumulativeSum` method calculates the cumulative sum by iterating through the ArrayList
and adding each price to a running total, then adding the total to a new ArrayList.
These methods are tested in the `main` method by creating an array and an ArrayList of stock prices,
then calling each method to perform the required operations and print the results.