Project - Guess The Number

1
Course Overview
PREVIEW1m 40s
8
9

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 42m
Students
7
Description

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.

Transcript

For this project,  you're going to create a file called Guess The Number or Proj3_3_GuessTheNumber. The computer will randomly select an integer between 1 and 100 (inclusive), and the user will guess what that number is. You must also count the number of guesses as the user makes them. There are four possibilities with each iteration, that is, with each guess the user makes. If the user selects the correct number, the program prints out, "Congratulations! You guessed the number in, and then however many guesses! Thanks for playing!", and the program terminates. If the user selects the number that's too low, the program prints out, "Your guess was too low." And if it's too high, the program prints out, "Your guess was too high." If the user selects a number lower than 1 or higher than 100, that is out of range, the program will print out, "That was a wasted guess! You must pick a number between 1 and 100, inclusive!". The program continues asking for guesses and counting the number of guesses until the user has selected the correct number.

Let's see what we can do here. I will show you the code running without showing you the actual code. We'll right-click and go to 'Run'. And you will see that it will say, Enter your integer guess. So, we know that the computer has decided on a number, and it says the guess was too high. So, now we know it's between 1 and 50. Let's try 25; too high. So, let's try maybe 17; too high. So, we know it's between 1 and 17. Let's try about halfway, as in 8. So, between 8 and 17, let's try maybe 12; too high. So, between 8 and 12, 10 and that's got to be 11. So, you guess the number in 7 guesses! Thanks for playing! So, are you ready? I want you to pause the video and give it your best shot. If you complete it or you get lost and need a little bit of help, come back here and we'll work on it together. Were you able to get that to work? A big congratulations if you finished it or even got close. This was very challenging. Even if you didn't finish it, I'm still happy you gave it a shot. Let's do it together.

And again, even if your code isn't just like mine, that's totally okay. It doesn't mean you did it wrong, necessarily. So, let's create the file, and I'm going to go back to our naming convention with Proj3_3. The last time, we did divisible by three; that was okay. It doesn't really matter. I wanted to show you that you can name them whatever you want as long as it's a valid name, and it works fine even if it's a big project. So, 'src', 'New', 'Java Class', Proj3_3_GuessTheNumber. And, of course, we're going to ask user input,  so we might as well just import java.util.Scanner. And, of course, we need public static void main. So, we're going to create one integer to hold the computer's number and one to hold our number that we make for our guesses. While we're at it, let's add a guessCount integer to count the number of guesses made, and we'll initialize that to 0, since we haven't made any guesses yet. So, we've got ourGuess, computerNumber and guessCount = 0. Before we write the loop, we need our computer to make the guess  for the number computerNumber. We can use the Random class with the nextInt method that we used before earlier in this section.

Recall that we have to pass an argument to the nextInt method in order to generate a value between 0 and that integer, exclusively. So, we want to generate numbers between 1 and 100, inclusively. So, since nextInt returns 0 through some value, we need to shift it. So, let's shift the randomized value by 1. So, we're going to have computerNumber, and we have to have a Random number generator too.  Random, we could have included this as well, which we will, = new Random(). And initially, it doesn't know what it is, but we can click there;  'Alt+Enter', 'Import class'. They're both in java.util. computerNumber = random.nextInt(100),  and we want that to go up to a 100. So, what that's going to do is generate 0-99 and then shifted by 1. So, there's many ways to accomplish this little project. But the way I'm going to do it is this: we'll create a Boolean variable called guessedNumber and send it to false, because at the very beginning, the user has not guessed the number. So, let's put it here, boolean guessedNumber; meaning have you guessed the number yet. Set it to false. And our while loop is going to be while(!guessedNumber). Just put that there, so you know what ends what. And as long as it's not guessed, we keep going. Obviously, this would be an infinite loop, so we got to make modifications.

So, inside the loop, we have to prompt the user for their guess right before the guessCount increment,  and then use if-statement to check if their guess is out of range, to start. If it is out of range, print "That was a wasted guess! You must pick a number between 1 and 100, inclusive!" and the other options that we have saw before. So, let's write the code to prompt the user for the guess. So, we'll say, ("Enter your integer guess"), and then we're going to put guessCount. Well, actually here let's get the guess first. So, we'll say, ourGuess  = keyboard.nextInt(). And we never created a Scanner. We probably need that too, don't we? Scanner keyboard = new Scanner based on (System.in).  Now it knows what keyboard is. So, we got ourGuess is keyboard.nextInt(). So, if you see red, you know that something is wrong. And professional developers mess up all the time just like beginners. It's just we usually can catch the error earlier, and that's what can make you a better developer is when you learn what does it mean when there's a syntax error or it doesn't recognize something or what's missing. So, the more experience you get, the better you'll get. We've got the guessCount. So, they made a guess. So, we counted up; we increment it.

So, now we want to add the rest of the selection control statements for the user's guess. So, these are the four different possibilities here. So, if(ourGuess  == computerNumber); that's one possibility. if(ourGuess  > computerNumber). And we also have if(ourGuess < computerNumber). And I guess we also have to put in an additional feature here where if it is out of range, we don't want to accept it. So, that might be the first thing we do. So, we could do nesting or we could use and. Let's use nesting. If(ourGuess < 1 || ourGuess  > 100), then that is invalid. Let's do this. We'll be a little more optimistic here. If (ourGuess >= 1 && ourGuess >= 100, then it's a valid. So, we'll put our valid code in there. And then this else is going to be invalid guess. So, there's multiple ways to do this, but this is the way I'm going to choose it. So, if it is within range, we will then say, "Okay, well, is it the right number too high or too low?" So, and in that case I guess we don't even need this one because this would be too low automatically. So, for the invalid guess, we know that we want to print out, "That was a wasted guess". So, ("That was a wasted guess! You must pick a number between 1 and 100, inclusive!").

So, now we can take care of the valid options. We will print out. ("Congratulations! You guessed the number in " + , I'm going to go to the next line here, guessCount  + " and then don't forget the space, guesses! Thanks for playing!") or something along those lines.  And then System.out.println("Your guess was too high!"), and then finally, ("Your guess was too low!"). Let's run it, and then test this out. I guess there's another thing we have to do is if since we're basing this on the guessedNumber being true or false, we want to make sure that if they are equal up in here, we have to set guessedNumber = true; otherwise, we're going to have a bad time. So, right here is guessedNumber = true. The loop would continue forever. It would be an infinite loop. So, this is the only condition that sets guessedNumber = true. So, let's try this. So, right click and 'Run'. And let's see what we have here. So, enter your integer guess. Let's put a 105 just to see what it looks like out of range. It'll say, that was a wasted guess. And then what about -5. That was a wasted guess.

So, let's guess 50; too low. So, that means we have to try higher. So, let's say, 75; too high. So, we know it's between 50 and 75. Let's try 60; too low. So, between 60 and 75. Let's try 70; too high. So, it's between 60 and 70.  65; too low. So, we're narrowing in 67. Congratulations! You guessed the number in 8 guesses! Thanks for playing! Let's run it one more time and give it a shot again. Integer guess, now we're not going to waste any guesses. We'll go straight for the center here. Too high. Let's try 25; too high. So, we know it's between 1 and 25. Let's try 17; too high. So, we know it's between 1 and 17. And let's try 8; too low. Between 8 and 17, let's try 12; too high.

So, between 8 and 12, let's try 10; too low. Got to be 11. Guess it in 7 guesses right there. Thanks for playing! So, we have the else in the associated if statement; that's the only other possibility in our code, right here; this one. So, we wouldn't actually have to, like we had before, have the condition here. Now if you were not doing the nesting approach and you had to put and in range, then you would have to include this with an and, essentially each of these. So, just keep that in mind.

So, that's fantastic work, everyone! I'm really proud of you. I hope by now that you're getting a hang of the fundamentals, including control statements and interacting with the user. Remember that this is a marathon, not a sprint. If you must take more time or go through the entire lectures or sections more than once to understand the material, don't be discouraged. It's better to take your time and understand the material as thoroughly as possible rather than trying to just get through and be done with it. Now, sometimes it might make sense to go ahead and then come back when you've learned a little bit more and review something. In fact, a lot of students find that to be very enriching. They go back another round. Sometimes they're like, "Oh, wow, that makes so much more sense now. And why, why do we do that?" So, I will see you in the section wrap up in the next lecture. Thanks.