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

Programming 1 Assignment Unit 4

Uploaded by

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

Programming 1 Assignment Unit 4

Uploaded by

Malou Majok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Here is the Java code that implements this program.

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

float[] stockPrices = {10.5f, 12.3f, 11.2f, 13.1f, 11.8f, 12.9f, 14.7f, 12.4f, 15.6f, 14.2f};

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

for (float price : stockPrices) {

stockPricesArrayList.add(price);

System.out.println("Average price: " + calculateAveragePrice(stockPrices));

System.out.println("Maximum price: " + findMaximumPrice(stockPrices));

System.out.println("Occurrences of 12.9: " + countOccurrences(stockPrices, 12.9f));

ArrayList<Float> cumulativeSumArrayList = computeCumulativeSum(stockPricesArrayList);

System.out.println("Cumulative sum: " + cumulativeSumArrayList);

public static float calculateAveragePrice(float[] prices) {

float sum = 0;

for (float price : prices) {

sum += price;

}
return sum / prices.length;

public static float findMaximumPrice(float[] prices) {

float maxPrice = prices[0];

for (float price : prices) {

if (price > maxPrice) {

maxPrice = price;

return maxPrice;

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

int count = 0;

for (float price : prices) {

if (price == targetPrice) {

count++;

return count;

public static ArrayList<Float> computeCumulativeSum(ArrayList<Float> prices) {

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


float sum = 0;

for (float price : prices) {

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy