Unit 4 - Arrays and ArrayLists

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Unit 4: Arrays and ArrayLists

Computer Science Department: University of the People


CS 1102-01 - AY2024-T3 Programming 1
Ursa Sayeed
February 29, 2024
INTRODUCTION
This is a program that runs an analysis on a daily stock price of a given company for a
given period. This program Calculates average stock price, finds the maximum stick
price, determines the occurrence count of a specific stock price and also computes the
cumulative sum of the stock prices.

This program utilizes Arrays and ArrayLists to hold stock price data and also employs
loops in implementing some aspects of its Methods.

import java.util.ArrayList;

public class StockMarketAnalyzer {

// Method to calculate the average stock price


public static float calculateAveragePrice(float[] prices) {
float sum = 0;
// Loop through each price in the array
for (float price : prices) {
sum += price; // Add each price to the sum
}
// Calculate and return the average price
return sum / prices.length;
}

// Method to find the maximum stock price


public static float findMaximumPrice(float[] prices) {
float maxPrice = prices[0]; // Initialize maxPrice with the first element of the array
// Loop through each price in the array
for (float price : prices) {
// If the current price is greater than maxPrice, update maxPrice
if (price > maxPrice) {
maxPrice = price;
}
}
// Return the maximum price found
return maxPrice;
}

// Method to determine the occurrence count of a specific price


public static int countOccurrences(float[] prices, float targetPrice) {
int count = 0;
// Loop through each price in the array
for (float price : prices) {
// If the current price matches the targetPrice, increment the count
if (price == targetPrice) {
count++;
}
}
// Return the count of occurrences
return count;
}
// Method to compute the cumulative sum of stock prices
public static ArrayList<Float> computeCumulativeSum(ArrayList<Float> prices) {
ArrayList<Float> cumulativeSum = new ArrayList<>(); // Initialize a new ArrayList to store cumulative sum
float sum = 0;
// Loop through each price in the ArrayList
for (float price : prices) {
sum += price; // Add the current price to the sum
cumulativeSum.add(sum); // Add the cumulative sum to the ArrayList
}
// Return the ArrayList containing cumulative sums
return cumulativeSum;
}

public static void main(String[] args) {


// Example usage:
float[] stockPrices = {210.1f, 410.0f, 100.1f, 120.0f, 110.1f, 110.0f, 110.0f, 250.0f, 130.0f, 140.5f};
ArrayList<Float> stockPricesList = new ArrayList<>();
for (float price : stockPrices) {
stockPricesList.add(price); // Populate ArrayList with stock prices from the array
}

// Calculate average price


float averagePrice = calculateAveragePrice(stockPrices);
System.out.println("Average Price: " + averagePrice);

// Find maximum price


float maxPrice = findMaximumPrice(stockPrices);
System.out.println("Maximum Price: " + maxPrice);

// Count occurrences of a specific price


float targetPrice = 11.5f;
int occurrenceCount = countOccurrences(stockPrices, targetPrice);
System.out.println("Occurrences of " + targetPrice + ": " + occurrenceCount);

// Compute cumulative sum


ArrayList<Float> cumulativeSum = computeCumulativeSum(stockPricesList);
System.out.println("Cumulative Sum: " + cumulativeSum);
}
}

OUTPUT:
Code Description:

1. calculateAveragePrice(float[] prices):
- This method calculates the average stock price by summing up all the prices in the
array and then dividing the sum by the number of prices.
- It iterates through each element of the array using a loop.
- Within the loop, it accumulates the sum of all prices.
- After iterating through all prices, it divides the sum by the total number of prices to
get the average.
- Finally, it returns the calculated average.

2. findMaximumPrice(float[] prices):
- This method finds the maximum stock price among all the prices in the array.
- It initializes a variable `maxPrice` with the value of the first element of the array.
- Then, it iterates through each element of the array using a loop.
- During each iteration, it compares the current price with the `maxPrice`. If the current
price is greater than `maxPrice`, it updates `maxPrice` to the current price.
- After iterating through all prices, it returns the maximum price found.

3. countOccurrences(float[] prices, float targetPrice):


- This method counts the number of occurrences of a specific price (targetPrice) within
the array.
- It initializes a variable `count` to keep track of the number of occurrences.
- Then, it iterates through each element of the array using a loop.
- During each iteration, it compares the current price with the `targetPrice`. If they
match, it increments the `count`.
- After iterating through all prices, it returns the count of occurrences.

4. computeCumulativeSum(ArrayList<Float> prices):
- This method computes the cumulative sum of stock prices and returns it as a new
ArrayList.
- It initializes an empty ArrayList `cumulativeSum` to store the cumulative sums.
- It also initializes a variable `sum` to keep track of the cumulative sum as it progresses
through the prices.
- Then, it iterates through each element of the ArrayList using a loop.
- During each iteration, it adds the current price to the `sum` and then adds the `sum` to
the `cumulativeSum` ArrayList.
- After iterating through all prices, it returns the ArrayList containing the cumulative
sums.

You might also like