[MUSIC] Welcome to the second part of our lesson on Java arrays. After completing this part of the lesson you'll know how Java arrays can be used to solve various types of programming problems. After working through two very different problems, you'll recognize how Java arrays often make solution strategies simple and straightforward. You will also understand that elements of an array can be accessed and processed sequentially from the front of the array to the end, or they can be accessed and processed randomly at any index value in the array. [MUSIC] In this lecture, we will look at some simple examples of using arrays. To start off, let's consider the problem that we used to motivate the need for arrays. In this problem, we want to read in a set of scores and compute their average. And then we want to report which scores are below the average so that we can give extra help to those students. Without arrays we would not be able to solve this problem very easily. But now that we know about arrays this problem can be trivially solved. Our solution will follow this strategy. First, we will need to access the data from the user. We then need to create an array to hold all the scores. After creating the array, we will get each score and place it in the array at the next location. As we do this, we will also sum all the scores so that we can compute the average. After summing all the scores we divide the sum by the number of scores to get an average. And then finally, we can compare each score with the average. We'll consider each of these steps in order. For this problem, we'll assume that the user has entered all scores in a textbox and separated them by a blank character. There are framework who will provide the string of input as a parameter to our process method. Given this String, we can use method of the String class to create an array of Strings. One element per value entered. We can later, convert each String value into the corresponding integer value. Here we see the starting point of our code which is the process method inside the logic class. This method will receive a string parameter that contains all the scores. The string class has a method called Split that'll divide a string into an array of strings with a delimiting character between values is given as a parameter. In this case it is a blank. Next we create an integer array of the correct size. The size we need is the same size as the array that was produced by the split method. So we simply get the length of the vals array and use it to specify the size of the score array. Next we'll need to convert each string value into it's corresponding integer value. This is accomplished by using the parseInt method from the integer wrapper class. Once we convert the number, we store the value in the next element of the score array. As we do this, we also want to compute the total of all the scores, and we accomplish that with a simple cumulative sum loop, using the accumulator variable total. We can finally compute the average by dividing the total by the number of scores and then use a simple for loop to report which scores were less than the average. And here's a sample execution. Given the input string of scores displayed at the top of the slide, the program will produce the output displayed at the bottom of the slide. Here's another interesting problem where arrays make the solution very simple. We want to simulate the rolling of two dice, and report the percentage of times each value was rolled. For this problem, we will use an array of integers where each array element is a counter for how many times the corresponding index value was rolled. Each time we roll the dice, we will increment the correct counter. And here is the code of our process method that is the starting point of our code in the logic class. The first thing we do is to find a constant that is the number of rolls we want to perform. The keyword Final makes the variable NUMROLLS a constant that cannot be changed after it has been initialized. Next, we declare our array of counters. Now when we roll two dice, there are 11 possible outcomes. Two through 12. So we could declare an array with only 11 elements. But instead, we are going to create an array with 13 elements with indices zero through 12. This will allow us to use the result of a roll to directly index into the array. I.e., when we roll a 12, we will access the counter at index 12. This greatly simplifies our code at the cost of having two array elements goes at index zero and one that will not be used. The next thing we do is create a random object with which we can use to generate random numbers. And now we can look at the meat of our simulation. We have a for loop that will iterate the desired number of times. On each iteration of the loop, we simulate the roll of our two dice by generating random values in the range of one to six. We then sum the two dice, and use that value as an index into our array of counters. And we increment the specified counter. After simulating all the rolls, we iterate over the counters for two through 12, and report the percentage of times each was rolled. To compute the percentage, we divide the count by the total number of rolls and multiply that by 100. To ensure that we get floating point division rather than integer division, we perform the multiplication by 100.0 first. And here is the output of our program. [MUSIC] The second part of this lesson showed examples of using Java arrays to solve different problems using different solutions. After studying these examples, you should now know how arrays can be utilized to solve in variety of problems and recognize how Java arrays often make solution strategies simple and straightforward. You should also understand how arrays can be accessed and process sequentially. As we did when we processed exam scores one after another or how arrays can be accessed and processed randomly as we did when we incremented counters based upon the roll of two dice. [MUSIC]