1 00:00:00,025 --> 00:00:04,451 [MUSIC] 2 00:00:04,451 --> 00:00:05,952 [SOUND] Welcome back! 3 00:00:05,952 --> 00:00:08,578 I have solicited some feedback and some example stories for 4 00:00:08,578 --> 00:00:12,300 us to ensure that our program is working the way that we wanted to. 5 00:00:12,300 --> 00:00:16,300 We will use this information to iterate and improve our program. 6 00:00:16,300 --> 00:00:18,020 I'm excited [LAUGH] to look through these. 7 00:00:18,020 --> 00:00:19,140 Let's look. Here's the first one here. 8 00:00:19,140 --> 00:00:23,238 It says, Craig is a big dork. 9 00:00:23,238 --> 00:00:24,556 Nice. 10 00:00:24,556 --> 00:00:26,785 While true, it's not very nice. 11 00:00:26,785 --> 00:00:28,734 Okay, let's look at this other one here. 12 00:00:28,734 --> 00:00:31,008 [LAUGH] Of course it starts the same way. 13 00:00:31,008 --> 00:00:38,084 Craig is a, what does that even mean? 14 00:00:38,084 --> 00:00:42,050 [LAUGH] Well, one thing is certain here. 15 00:00:42,050 --> 00:00:44,560 We are going to need to put some sort of age restriction on this. 16 00:00:45,750 --> 00:00:48,980 After just reviewing those first two responses, I am certain 17 00:00:48,980 --> 00:00:52,730 that we better limit this from getting in the hands of someone under the age of 13. 18 00:00:52,730 --> 00:00:56,570 So, let's write some code that stops the program from running, 19 00:00:56,570 --> 00:00:59,240 if the user doesn't meet our age requirements. 20 00:00:59,240 --> 00:01:02,610 In order to do this we're gonna need to pick up a few new skills. 21 00:01:03,760 --> 00:01:07,800 To put this age restriction in place, we are going to need to work with numbers. 22 00:01:07,800 --> 00:01:12,450 Now, Java has a data type that deals with whole numbers, and it's called integer. 23 00:01:12,450 --> 00:01:16,140 We'll store the user's age in a variable of the integer data type. 24 00:01:17,490 --> 00:01:21,653 Then we're going to use what is known as a conditional statement. 25 00:01:21,653 --> 00:01:25,420 Conditionals help us to make choices based on information we know. 26 00:01:25,420 --> 00:01:26,990 We make decisions like these all the time. 27 00:01:28,020 --> 00:01:31,610 A clear example is to take a look at the conditions that are made by users of 28 00:01:31,610 --> 00:01:33,690 an online dating program. 29 00:01:33,690 --> 00:01:38,000 I will go out with that guy, if he doesn't smoke, doesn't have kids, and 30 00:01:38,000 --> 00:01:39,420 likes to go to concerts. 31 00:01:39,420 --> 00:01:44,080 Or, I will date anyone who is older than 28 and less than 50. 32 00:01:44,080 --> 00:01:47,260 Only after these conditions are met is contact made. 33 00:01:47,260 --> 00:01:53,448 So the conditional we are specifically looking for is, is the user older than 13? 34 00:01:53,448 --> 00:01:59,250 The answer that we get back is either yes or no, true or false. 35 00:01:59,250 --> 00:02:02,410 In Java this is a data type referred to as a boolean. 36 00:02:03,940 --> 00:02:07,650 And last but not least, we'll learn how to immediately exit the program so 37 00:02:07,650 --> 00:02:10,650 that it doesn't continue running after we've decided they aren't 38 00:02:10,650 --> 00:02:12,290 old enough to use the program. 39 00:02:12,290 --> 00:02:13,240 Are you ready? 40 00:02:13,240 --> 00:02:16,294 Let's go put a PG13 warning on this program. 41 00:02:16,294 --> 00:02:21,400 All right, so first things first, we gonna make a new variable called age. 42 00:02:21,400 --> 00:02:23,830 So remember, we're gonna make an integer data type. 43 00:02:23,830 --> 00:02:28,590 So the way we do it is we start by stating the data type name, which is int. 44 00:02:28,590 --> 00:02:31,505 And then we type the name of the variable, which is age. 45 00:02:31,505 --> 00:02:33,052 And let's go ahead and set ourselves up to fail. 46 00:02:33,052 --> 00:02:35,629 And let's set it = 12. 47 00:02:35,629 --> 00:02:39,260 And then we'll press the semi-colon of course to end the statement, great. 48 00:02:40,525 --> 00:02:43,050 Okay, so now let's go ahead and add our conditional. 49 00:02:43,050 --> 00:02:46,850 Now remember, we're gonna check and see if the person is younger than 13. 50 00:02:46,850 --> 00:02:51,330 In other words, if the age is less than 13, exit. 51 00:02:51,330 --> 00:02:52,890 Okay, let's write that. 52 00:02:52,890 --> 00:02:56,190 So, first we'll write the keyword if. 53 00:02:56,190 --> 00:02:58,010 And then we'll add an open parentheses. 54 00:02:58,010 --> 00:02:59,321 And here's where we write our expression. 55 00:02:59,321 --> 00:03:04,170 And that's the condition that must be true for the code following it to run. 56 00:03:04,170 --> 00:03:06,158 So, which we said earlier that is age. 57 00:03:06,158 --> 00:03:10,676 And then we're gonna do < 13. 58 00:03:10,676 --> 00:03:14,862 And we're gonna close the parentheses, and we're gonna give a space and 59 00:03:14,862 --> 00:03:15,750 a curly brace. 60 00:03:15,750 --> 00:03:18,963 And a curly brace opens up a block of code. 61 00:03:18,963 --> 00:03:21,410 And so let's just go ahead really quick and add a comment here. 62 00:03:21,410 --> 00:03:24,189 And this is where we're gonna insert our exit code. 63 00:03:24,189 --> 00:03:26,180 And then let's close that curly brace. 64 00:03:27,740 --> 00:03:30,050 You could have many lines of code in this code block here. 65 00:03:31,200 --> 00:03:31,900 So let's go ahead and 66 00:03:31,900 --> 00:03:37,800 let's add a new line that says that they're not allowed to use this. 67 00:03:37,800 --> 00:03:44,550 So, we'll say, Sorry you must be at least 13 to use this program. 68 00:03:45,650 --> 00:03:50,511 We're gonna go ahead and do a new line sequence in the quotes, and 69 00:03:50,511 --> 00:03:52,626 the semi-colon. 70 00:03:52,626 --> 00:03:57,800 Okay, now what we'll do is we'll tell the program that it's time to quit or exit. 71 00:03:57,800 --> 00:04:01,268 So far the only object that we've really used is called console, but 72 00:04:01,268 --> 00:04:02,580 we're about to see a new one. 73 00:04:02,580 --> 00:04:04,350 And that's the global system object. 74 00:04:05,440 --> 00:04:07,721 This object has a method on it which is called exit. 75 00:04:07,721 --> 00:04:11,426 Which as you might guessed, causes the program to exit. 76 00:04:11,426 --> 00:04:14,791 The exit method takes an argument for the status code, 77 00:04:14,791 --> 00:04:19,630 which zero means that the system exited intentionally, we had control of it. 78 00:04:19,630 --> 00:04:23,440 Any other non-zero value represents a status code that is used to state that it 79 00:04:23,440 --> 00:04:25,310 exited abnormally. 80 00:04:25,310 --> 00:04:26,362 So let's go ahead and write that out. 81 00:04:26,362 --> 00:04:31,042 So again, that's System with a big S dot exit, 82 00:04:31,042 --> 00:04:34,910 and then the status code of 0, great. 83 00:04:34,910 --> 00:04:35,692 I'm gonna save that. 84 00:04:35,692 --> 00:04:41,774 And let's give that a go and see if it works. 85 00:04:41,774 --> 00:04:44,214 Let's run that, and boom. 86 00:04:44,214 --> 00:04:48,785 Now because we hard coded, again, we hard coded the edge up here at 12. 87 00:04:48,785 --> 00:04:50,538 It is always gonna run into this if block. 88 00:04:50,538 --> 00:04:51,781 And these two statements are gonna run. 89 00:04:51,781 --> 00:04:53,850 And it's gonna exit and the rest of the code won't go. 90 00:04:53,850 --> 00:04:56,130 So we won't get prompted for the rest of these. 91 00:04:56,130 --> 00:04:57,180 So it's working, great! 92 00:04:58,590 --> 00:05:00,830 Wow, lots of good stuff! 93 00:05:00,830 --> 00:05:04,730 We use a new data type, we learned about if statements and code blocks. 94 00:05:04,730 --> 00:05:07,450 And learned how to safely exit from our program. 95 00:05:07,450 --> 00:05:10,630 One thing that I wanted to point out about the int data type was that it was 96 00:05:10,630 --> 00:05:14,090 all lowercase, unlike the other data type we had seen. 97 00:05:14,090 --> 00:05:16,180 Remember that was the string with a capital S. 98 00:05:17,380 --> 00:05:21,506 When a data type is lowercase it is a special kind of data type called 99 00:05:21,506 --> 00:05:22,397 a primitive. 100 00:05:22,397 --> 00:05:26,420 int is one of eight primitive data types that come out of the box with Java. 101 00:05:27,580 --> 00:05:30,150 Basically, primitives have special characteristics 102 00:05:30,150 --> 00:05:32,320 about the kind of data they can store. 103 00:05:32,320 --> 00:05:33,610 And that's about it. 104 00:05:33,610 --> 00:05:35,820 They don't give you methods or properties. 105 00:05:35,820 --> 00:05:37,340 They're just data. 106 00:05:37,340 --> 00:05:38,190 How primitive, right? 107 00:05:39,240 --> 00:05:42,370 For now, I just want you to be aware of the term primitive 108 00:05:42,370 --> 00:05:44,010 in case you run into it. 109 00:05:44,010 --> 00:05:47,145 So, int is a primitive data type, and string and 110 00:05:47,145 --> 00:05:51,213 all other non-primitive types are called object data types. 111 00:05:51,213 --> 00:05:53,143 Check the teachers notes for more information. 112 00:05:53,143 --> 00:05:57,884 [SOUND] Now our application is safely blocked from pre-teens. 113 00:05:57,884 --> 00:06:01,760 Well, actually, at the moment it's pretty much blocked permanently, right? 114 00:06:01,760 --> 00:06:05,665 We need to make that age variable dynamic by prompting our user. 115 00:06:05,665 --> 00:06:06,766 But before we get there, 116 00:06:06,766 --> 00:06:10,340 let's do a quick exercise to make sure all this new information is making sense.