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

Programming Assignment Unit 4

The document outlines a programming assignment for creating a Java script that calculates various stock price metrics, including average price, maximum price, occurrences of a specific price, and cumulative sum of stock prices. It provides detailed code snippets for each functionality and explains how to implement and test them in the main method. Additionally, it includes references for further reading on Java ArrayLists and related concepts.

Uploaded by

mudasirshah129
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Assignment Unit 4

The document outlines a programming assignment for creating a Java script that calculates various stock price metrics, including average price, maximum price, occurrences of a specific price, and cumulative sum of stock prices. It provides detailed code snippets for each functionality and explains how to implement and test them in the main method. Additionally, it includes references for further reading on Java ArrayLists and related concepts.

Uploaded by

mudasirshah129
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1

Programming Assignment Unit 4: Stock Price Software

Department of Computer Science, University of People

CS 1102-01 Programming 1– AY2024-t4

9 May, 2024
2

Programming Assignment Unit 4: Stock Price

In this assignment I am going to write a simple script in java which will calculate the

average stock price, maximum stock price, occurrence count of a specific price and it will also

compute the cumulative sum of stock prices. It will be a simple script in java.
3

Importing Functions / Methods:

First of all I am going to import a function / method which is a built-in function / method

in java for ArrayLists.

Code:

import java.util.ArrayList;

Main Class:

Now I am going to create a public class which is called Main. In this class I am going to

write my whole script.

Code:

public class Main {

Calculate Average Price:

Now I am going to write a function / method (I am going to start calling it method) which

is going to calculate the average of the stock price. It will be a public static double method which

take int[] prices (which is an array). Then the second line in which I declared a variable name

sum which is a double and the value stored in this right now is 0.0. I will use this variable to

accumulate the total price of all stocks.


4

Next I am going to create a for loop which will iterates over each element in the array in

my case I am using the name prices for my array and price for each elements in the array. At the

end of the every iteration it will simply put the price in the sum variable. After that it is going I

am going to ask for my method to return me calculation of sum / prices.lenght; ( sum will be the

sum of the arrays element and prices.lenght; will be total length of the array) and I will get the

average of the stock with this method.

Code:

public static double calculateAveragePrice(int[] prices) {

double sum = 0.0;

for (int price : prices) {

sum += price;

return sum / prices.length;

Maximum Price:

Now I am going to write a method which is going to find maximum price of the stock

array. It will take an array as a parameter. First I am going to declare int maxPrice variable and

initialize it with price[0] (again remember price will be replaced with the element of the Array

and [0] is the first element of the Array, which we will give it while we run the program).

After that I am going to create a for loop which will loop through each element in the

Array and use Mat.max method (it is a built-in method in java) to find the max price in the Array
5

and store it in the maxPrice variable which we declared earlier. Lastly we will use return

keyword to return the value of maxPrice variable value.

Code:

public static int findMaximumPrice(int[] prices) {

int maxPrice = prices[0];

for (int price : prices) {

maxPrice = Math.max(maxPrice, price);

return maxPrice;

Count Occurrences:

It’s time to create function which is going to count occurrences of the stock. It will take

to parameter one will be Array and second will be target price. First I have initialized a counter

with the name of count. This count variable will be used to store the number of time the

targetPrice is found in the given Array.

Now I have written a for loop to iterate through all the element in the provided list and

check if the targetPrice is there or not if it’s in the array counter will go up if not counter will

stay the same and at the end of this loop I used return keyword to get the counter value.

Code:

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


6

int count = 0;

for (int price : prices) {

if (price == targetPrice) {

count++;

return count;

Compute Cumulative Sum:

In this method I am going to compute cumulative sum of the stock prices. It will take a

single parameter which this time will be an ArrayList. First and foremost I am going to declare a

new ArrayList cumulativeSum, this list will be used to store the cumulative sums for each price

in the given ArrayList.

After declaring and initializing Array I am going to delare and initialize a variable name

sum to 0. This variable will be used to accumulate the sum of the prices as we iterate through the

ArrayList. Now I am going to write a for loop to iterate through each and every elements in the

ArrayList store it in sum variable which I mentioned earlier and after that I am going to use .add

method which is built-in function of ArrayList to add cumulative sum values to the declared
7

ArrayList cumulativeSum. Lastly I am going to use return keyword to return the cumulative

ArrayList.

Code:

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

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

int sum = 0;

for (int price : prices) {

sum += price;

cumulativeSum.add(sum);

return cumulativeSum;

Main Method / Function:

Our script is done now we have to test if everything works well or not? So I am going to

write the main method which will demonstrate how to use the function which I created above

and if they work correctly or not. First I am going to initialize an array name price which will

have 100, 120, 90, 115, 85 these values. Now I am going to declare an array list by the name

pricelist. Next step I am going to run for loop on the array and add their values in the pricelist.
8

Next I am going to declare a variable of double type name averagePrice and assign it the

method called calculateAveragePrice and give it the parameter array which is price. It will going

to store the answer which will return by the averagePrice method and store it in the mentioned

variable. I am going to do similar thing with other methods but I will use the specific types

variables which are exactly the one which are return by the methods.(Note: you can look at the

code for all the declarations and their types. I am not going in that details to keep it short.).

Lastly I am going to use println method to print everything on the terminal and our script is

complete.

Code:

public static void main(String[] args) {

int[] prices = {100, 120, 90, 115, 85};

ArrayList<Integer> priceList = new ArrayList<>();

for (int price : prices) {

priceList.add(price);

double averagePrice = calculateAveragePrice(prices);

int maxPrice = findMaximumPrice(prices);

int targetPrice = 100;


9

int occurrences = countOccurrences(prices, targetPrice);

ArrayList<Integer> cumulativePrices = computeCumulativeSum(priceList);

System.out.println("Average Price: " + averagePrice);

System.out.println("Maximum Price: " + maxPrice);

System.out.println("Occurrences of " + targetPrice + ": " + occurrences);

System.out.println("Cumulative Prices: " + cumulativePrices);

Output:

I am going to show you screen shot of the output. Please take a look at figure 1.1.

Figure 1.1
10
11

References

Javanotes 9, Section 7.1 -- Array details. (n.d.). https://math.hws.edu/javanotes/c7/s1.html

Javanotes 9, Section 7.3 -- ArrayList. (n.d.). https://math.hws.edu/javanotes/c7/s3.html

GeeksforGeeks. (2020, June 21). Java.Util.ArrayList.add() method in Java. GeeksforGeeks.

https://www.geeksforgeeks.org/java-util-arraylist-add-method-java/

Java ArrayList. (n.d.-b). https://www.w3schools.com/java/java_arraylist.asp

You might also like