This course explores the fundamental concepts and syntax associated with Control Flow and showcases these with some real-life projects.
Learning Objectives
- Learn about the three categories of control statements: sequential, selection, and repetition
- Manipulate control statements using the continue and break statements
- Learn about pseudo-random numbers and the Random class
Intended Audience
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
Prerequisites
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In this section, we have primarily focused on control statements, which help us to have our code make decisions and choose to execute code or not, so called conditional execution. This is an enormously powerful tool that we can use to make our programs do very powerful things. We will use these throughout the rest of the course. In this lecture, we're going to take a slight detour and discuss random numbers or more accurately pseudo-random number generation. Computers don't think the same way we do. So, in and of themselves, they are unable to generate truly random numbers, but they use some tricks behind the scenes to generate numbers that are sufficiently random for any purpose we might need. A deep mathematical discussion about pseudo-random number generation is outside the scope of this lecture, but we can rest assure that we can safely use the handy tools at our disposal to generate random numbers to add interesting features to our programs and make the programs seem more alive in some sense.
To work with pseudo-random numbers in Java, you can use the random class. Let's create a file in our section three projects IntelliJ project named Random Fun. All right. So, 'src', right click it. 'New', 'Java Class', and we will call this RandomFun. Let me put our public static void main. There we go. The random class doesn't live in the default java.lang package that's automatically imported. Like the scanner class that we've used before, the random class lives in the java.util package. So, we must explicitly import this file, so that we can use it. Let's do that and write some code to test the random class out. So, import java.util.Random. right there. And in here we will create a random number generator. Random, we'll just call it random with r equals new random. And then we'll say int myRandomNumber, myRandomNumber = random.nextInt().
So, because the nextInt returns an integer, it returns any integer in the range -2,147,483,248 up to 2,147,483,647. I knew you needed all those details, but you can look it up of course. So, it's just over a billion on either end and it generates signed output. So, let's print this out and see what we can get. So, we'll say number is and I'll put a \t here, myRandomNumber. There we go. However, we might want to limit the range, so we can generate numbers between zero and some number by passing an argument to next end, and the number you pass itself is actually excluded but it will go zero up to one less than the number that you pass. So, let's try that. After we printed out the number up here, we're going to reuse this myRandomNumber here. myRandomNumber = random.nextInt, and I'm going to put a 1000 there. So, it will go 0 to 999 because 1000 is excluded, and we'll print this out.
You could copy and paste if you like. So, we'll just say but I'm going to, I'm going to just put this through 999 \t and myRandomNumber. If you want to generate a number from say one through 1000, you have to use a shifting technique. So, you add to the generated number. So, instead of zero through 999, I want to shift it over one so it generates one through 1000. Well, you can't really have the number generator do that, but you can use the shifting technique using addition to make it include one through a 1000. So, I'll show you how to do that here. So, this is using shifting. myRandomNumber = random.nextInt, exactly the same as before but the difference here is we're going to go up by one, so that shifts the entire range. You might get a zero returned by this random number generation, but then you'll add one to it and it will become one.
So, that's the lowest number it can have, and the highest number that this will generate excludes 1000. So, it'll be 999. But that could go if it generates that to a 1000 by adding the one. So, that goes one through 1000. Pretty cool. One through 1000? and then /t myRandomNumber. There we go. And of course, let's run it a couple of times and see what happens. So, right click random fun and we'll run it, and we do get the first number is quite humongous right here, which is what we expect probably that big negative number or it could be small, it could be anything in that range, zero through 999, that generated 932. 1 through 1000 generated 668. Let's run it again, and we get different numbers this time. That's a much smaller number for both of these in that case and maybe do it one more time just to see what we get.
And that's a really, really big negative number, very large magnitude in the negative direction. So, quite a variety here. So, that would be sufficiently random for most purposes. So, now that you know how to work with the random class, and you have learned how to work with repetition control statements like the for loop, I'd like to issue you a challenge. For this challenge, I'd like you to create a file named DiceSimulation and in the file, I'd like you to simulate 10 rolls of a single standard 6-sided die, printing out the results in a for loop. If you're not familiar with a die plural, dice, it's a small cube with a certain number of dots on each of its six faces. One face has one dot on it, another has two dots, then 3, 4, so on up to six. So, you can roll the die and get anywhere from one dot to six dots. Many games, including gambling games at casinos, for instance use dice.
So, pause the video and simulate the 10 die rolls. Come back when you're done or if you need some help. How did that work out for you? Were you able to complete this challenge? Let's do it together. So, we'll close our RandomFun here, and I'll create another one right here, Java Class, DiceSimulation. Right there. And let's see, we know we're going to need random. All right. And we know that we need main, of course. Okay, there we go. We need a random number generator. Good. And diceVal to keep track of each role. And we know that we need 10 rolls. So, the best thing to do instead of actually, physically printing out 10 of them manually is to use a loop. So, we'll go from i = 0 to i < 10. That will give us 10. You could start at one and go up to including 10, that would do the same thing, but programmers tend to like starting at zero, unless they have another reason not to.
So, we will do this nextInt, and I will do nextInt to six, and that will generate the numbers zero through five, but I don't want zero through five. I want one through six. So, what do we do? Shifting technique. And now of course, system.out.println(diceVal). Cool. Now let's run it. Right click dice simulation, run the dice simulation, and we get bunch of these numbers here and there we go, 10 rolls of the dice and you'll notice they all occur between one and six inclusive. If we ran it again, we will get different numbers, but they all again are between one and six inclusive. Very cool. So, that would be something you could add to a game and even if you had graphics involved, this fundamentals would still be the same. You would still need a random number generator. Maybe you want a specific type of enemy to appear but that gets decided while your game is being played rather than ahead of time.
Random number generators can do that for you too. Awesome. Excellent work, everyone. You've combined your skills with repetition control statements and random number generation to make a cooler program. The next lecture will be your first project for this section, a learning program of the month club application. I'll see you there.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.