Programming Assignment Unit 4
Programming Assignment Unit 4
9 May, 2024
2
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
First of all I am going to import a function / method which is a built-in function / method
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
Code:
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
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
Code:
sum += price;
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
Code:
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
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:
int count = 0;
if (price == targetPrice) {
count++;
return count;
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
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:
int sum = 0;
sum += price;
cumulativeSum.add(sum);
return cumulativeSum;
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:
priceList.add(price);
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
https://www.geeksforgeeks.org/java-util-arraylist-add-method-java/