The Accumulate Algorithm
Suppose you have a list of all the baseball players on a team and you want to find the total number of hits that the team had. As humans, we can do this quite easily. Just add them up. But how do you teach a computer to add up all of the hits?
General Problem: Add up all of the hits for a baseball team.
Refined Problem: Write a method that finds the sum of all of the hits in an array of hits.
Final Problem: Write a method called findSum that has one parameter: an ArrayList of Integers. The method should return the sum of all of the elements in the array
Algorithm:
Step 1: Create a variable called sum and set it equal to zero
Step 2: Look at each of the elements in the list
Step 3: Add the element to the sum
Step 4: Continue until you reach the end of the list
Step 5: Return the sum
Pseudocode:

Java code 2: Using an ArrayList (for loop)

The Find-Highest Algorithm
What is the highest score on your favorite video game? As more people play a game, how does the computer figure out what is the highest score in the list?
General Problem: Find the highest score out of all the scores for a video game.
Refined Problem: Write a method that finds the highest score in a list of scores.
Final Problem: Write a method called findHigh that has one parameter: an ArrayList of Integers. The method should return the largest value in the array.
Solution 1: Let the highest be the first element in the list.
Algorithm:
Step 1: Create a variable called high and set it to the first element in the list
Step 2: Look at each of the scores in the list
Step 3: If the score is greater than the high score, then make it be the new high score
Step 4: Continue until you reach the end of the listStep 5: Return the high score
Pseudocode:

Java code 2: Using an ArrayList (for loop)

Solution 2: Let the highest be an extremely low value.
There is an alternative solution to the high/low problem that you should know. To execute this alternative, we modify the first step in the previous algorithm.
Step 1: Create a variable called high and set it to a really, really small number.
This algorithm works as long as the really, really small number is guaranteed to be smaller than at least one of the numbers in the list. In Java, we can use the public fields from the Integer class to solve this problem.
Likewise, to solve the find-lowest problem, you could set the low to be a really, really big number
Searching for the Smallest Item in a List
If we changed the previous example to find the lowest score in the list, then the comparator in the if-statement would be changed to less than, <, instead of greater than, >.
The Accumulate Advanced Algorithm
You have been hired by the high school baseball team to write a program that calculates statistics for the players on the team. You decide to have a class called BaseballPlayer. Every BaseballPlayer has a name, a numberOfHits, and a numberOfAtBats. The BaseballRunner has an array of BaseballPlayer objects called roster. Your goal is to find the team batting average.


General Problem: Given a roster of baseball players, find the team's batting average.
Refined Problem: You are given a list of baseball players. Every baseball player knows his number of hits and number of at-bats. The team average is found by first computing the sum of the at-bats and the sum of the hits and then dividing the total hits by the total at-bats. Write a method that computes the team batting average.
Final Problem: Write a method called findTeamAverage that has one parameter: a BaseballPlayer array. The method should return a double that is the team's batting average. Compute the team average by dividing the total hits by the total at-bats. Make sure a player exists before processing him. Also, perform a check against zero before computing the team average. If the team total at-bats is 0, return a team batting average of 0.0. Finally, adapt your algorithm to work with an ArrayList of BaseballPlayer objects.
Algorithm:
Step 1: Create a variable called totalHits and set it equal to 0
Step 2: Create a variable called totalAtBats and set it equal to 0
Step 3: Look at each player on the rosterStep 4: Get the number of hits for the player and add it to totalHits
Step 5: Get the number of atBats for the player and add it to totalAtBats
Step 6: Continue until you reach the end of the list
Step 7: Compute the teamBattingAverage by dividing the totalHits by the totalAtBats
Step 8: Return the teamBattingAverage
Pseudocode:

Java Code 1: Java code using an array (for loop)




