Today's lesson, we're going to talk about enumeration. Now enumeration is basically a complete ordered list of all the items in a collection or a set. So for example, Monday, Tuesday Wednesday, Thursday, Friday, Saturday, Sunday, would be a complete list of days of the week. Red, orange, yellow, green, blue, indigo, and violet may or may not be a complete set depending upon what you're talking about. If they are the colors in the spectrum, the ROYGBIV colors, then yes it is. If it stands for all colors, well, no it's not. Let's take a look at how enumeration is used in a program. I've pulled line 11 out to illustrate how you would declare an enumeration called planets. And if you look at the top of the screen you can see I have public enum planets and then the nine planets. Yes, I'm still including Pluto as a planet. The nine planets in our solar system. Line 11 is followed by our main routine where we simply have a for loop that prints out all of the planets by accessing each one individually. So in line 15, I have four planets, P, and I'm associating the planet's enumeration, and specifically the values of that list, and I'm printing out a P. And this is a different kind of for loop, because I'm using P as a control for which planet I'd like to access. When I run the program, as you can see down at the very bottom, I have all of the planets in order because P rotates for each of the planets in the enumeration. The last three lines show you that you can access the value of planet earth, meaning the word earth, or the ordinal numbers of any of the planets. Pluto, Mars. And you can pick one. And just in case you were wondering where we are in the solar system, Earth is here. In the inner planets we're the biggest. But when you look at the outer planets, earth is here and we're the smallest. Let's take another example of enumeration. Suppose I wanted to play a game of rock paper scissors. All I need to do is have an enumeration that I'll call hand sign, because rock paper scissors is a hand game. So I have three possibilities, scissor, paper and rock, and it doesn't matter the order that I put them in. When I write a program I have line 12 that shows here is my enumeration. The program has a routine to clear the screen by creating an escape sequence, and I can use system.out.print and use this combination of characters. The \033 is the escape character. When the \033 is combined with the open bracket, the 033H allows us to move to the top left corner of the screen, and the 2J allows us to clear the screen. So by putting these characters together I can clear the screen of anything that's there, followed by a flush of the system output to get rid of any garbage that still is in the system output print. Then I declare some random numbers, which we've done before. I have some integers to calculate how many games are played, how many times the computer wins, how many times the player wins, and how many ties there are. And then I get into the logic of the game. And you know what? Let's move over to the actual running of the game and see how this works. What we're going to get is something that looks like this. But let's see it happen live. I'm using the online GDB compiler, and I'm gonna run this program to show you how I go about playing the game. Again, the entire code is here. A lot of it doesn't have to do specifically with setting up the enumeration, but it does have to do with how we access the items that are in that enumeration. Hopefully everyone knows the rules to rock paper scissors. I start with a while loop that issues the command that tells the user what to do. Enter an S for scissor, P for paper, R for rock or Q to quit the game when you're done playing. I convert the input to lowercase and then evaluate through a switch statement which letter they pressed. If they had an invalid letter, I give them a message and continue the loop again. Moving down, I have some code that will generate a random number which will be the computer's move, and a line prints out the computer and its move, the player and its move. Then it's time to figure out who won the game. If the two moves are equal, it's a tie. If the computer move is scissors and the player move is paper, well we know that scissors cut paper so the computer wins. I go through all of the possibilities of having the computer win, and then the possibilities of having the player win. Finally, at the bottom of the screen, I figure out the percentages of how many times the computer won, how many times the player won, and how many times there was a tie, and print that information out. But the enumeration part here has to do with this hand sign scissor, hand sign rock and hand sign paper. And that allows me to go back and check to see what were the values of those items. Let's play the game. When we run the game it tells me the instructions, that this is the rock paper scissor game. I'll choose an R for rock. And it tells me that was a tie 'cause the computer also chose a rock. Now choose a rock again. This time the computer chose scissors and I win. Another rock. The computer chose scissors. I won again. Another rock. Oh, the computer chose paper. He caught onto my game. Now the computer wins. So I'll choose paper. This time the computer chose scissors, and the scissors cut the paper. Now remember there is a random generator, and the random generator figures out what goes on. I'll choose scissors again and then I'll quit the program. And it produces, I played six games. Two times the computer won. Two times I won. And there were two ties. A good way to learn about enumeration is to take a look at things that we're familiar with. In this case, the order of the planets in our solar system. Take a look at line 12. That's the standard list of planets in order as they move away from the sun. Mercury, Venus, Earth, Mars, Jupiter, Saturn. We put those planet names inside of an enumeration called planets. And from there I can print out the list. Let's run the program. When I run the program I get the entire list of planets. At the bottom I get the value of earth is earth. The index of Pluto is eight, and the index of Mars is three. What's going on here is this for loop will print out the value, or in this case the name, of each planet in the enumeration. Then I print out the value of a specific planet, calling it by name. And I can print out the ordinal number, which is the index. Because you'll remember that it starts at zero. Now we all know that earth is the third planet from the sun. It's somewhat confusing to see Mars listed as planet number three. We all know that earth is the third rock from the sun, so what I need to do is not print out the ordinal number, but print out the position of the planet in the enum. I do that, but with two lines. The first line will be to create a integer called planet, and I will set it to the planet of the value of earth. And I will add one to the ordinal number. So earth, which has an index of two. Mercury is zero. Venus is one and earth is two. If we add one to the ordinal, or the index, we will have earth as the third planet. And then I can print out earth is planet number three. Let's run the program, and we'll see that earth does indeed come up as planet number three. Now you know that by creating a simple enumeration you can pull in any values that are a complete set, and use those values in your program.