12:46:36 In our final visit video. For this module, we're going to be talking about one of the most important components. 12:46:52 And that component is the storage space that we need to do our computation. In other words, those primitive data types that I have introduced to you along the way when we were looking at some of the keywords of the language. 12:47:02 And so these primitive data types are inherited or built in the language and they fall into 3 major categories. 12:47:08 Logic, character, and numeric. As you can see, the Boolean is our first. 12:47:15 Type and it is our logic. Primitive data type. It stores true or false. 12:47:22 It actually uses one bit. And we'll see what that one bit is set to when it means true or false. 12:47:28 The next type we have is the character. This utilizes the unicode characters, which you utilize 16 bits. 12:47:37 We will see an example. From the subset, the ASCII table, and how it fits into Unicode. 12:47:42 Remember when I talked about if you had a friend and you both created a secret code? You have to have the exact same secret code and so hence Unicode is universal around the world. 12:47:55 For the numerical values for these symbols. Now, the next group, the bite to short, the int in the long. 12:48:05 If you notice, they're all signed integers. The word signed means that the number can be both positive or negative on the number line. 12:48:12 If you note each one doubles in size by powers of 2, starting with the bite at 8 bits and ending at the long at 64 bits. 12:48:21 In the last column, you can see the range for those values. So the bike can only store small numbers as opposed to long, which can start store very large numbers. 12:48:31 Now let's look at the bite for a minute. Do you see how it goes from negative 1 28 to positive 1 27? 12:48:37 Well, 8 bits we can store 256 values. Why? For each bit, we can store a 0 or one. 12:48:46 So there's 2 options. Times 2 options for the second bit, third bit, etc., update bits. 12:48:52 In other words, 2 to the power 8, which is 256. So the range from negative 1 28 to positive 1 27 is a is 256 values. 12:49:04 Now we have to note The positive stops at 1 27 because as you're going to see shortly 0 is stored as a positive value and that short changes us one more positive number. 12:49:17 Now the last group that's numeric as well is the float in the double. And you can see the double again. 12:49:23 Doubles in size by a power of 2 and is more precise than the float. The float in the double allow for fractional portions of numbers. 12:49:33 One little side note on the float in the double when you're hard coding literals to give them initial values. 12:49:40 I would like you to note that you can express them using a notation. Okay, so let's take a look at all of these data types. 12:49:54 So here's our Boolean. It uses one bit. If that bit is set to 0, the value is false. 12:50:00 If the bit is set to one, the value is true. 12:50:06 Now our next primitive data type, as I noted the character uses Unicode, you can go to unicode. 12:50:13 Dot org and see all of the characters that have numerical values. But let's work with a more simple example. 12:50:20 What I'm showing you on the screen is the ASPI table, which I have provided for you. 12:50:24 And so single quote with A and it stars the character value A. Now it has some numerical values and we're gonna work through that but I want to bring up again. 12:50:37 Double quotes mean string literals. Remember, strings see that uppercase letter. 12:50:44 It's an object. It is not one of the primitive data types. It still is a type of storage space. 12:50:50 But it's more advanced than our primitive data types. Now, when you go to the course website, you'll find that ASCII table for the American standard code for information interchange. 12:51:03 What we're looking at on the screen is what we call just the ASCII table. It goes from 0 to 1 27. 12:51:09 From 1 28 to 2 55 is what's called the extended ASCII table. And there's some other symbols in it that are not often used, but please familiarize yourself with both parts of the table. 12:51:23 Now if we look at uppercase A and here's the big difference, you look at uppercase A, it has a decimal value of 65, the lowercase a has a decimal value of 97. 12:51:35 This is why Java is K-sensitive. Lower case A and uppercase A do not have the same numerical values. 12:51:43 So how do we know that this is uppercase A, this binary number here using 16 bits. 12:51:51 Well, I teach you a little trick. Binary values, each position can be either 0. Off and don't count it or one on and counted. 12:52:01 Now just like the decimal number system, each position because it's a positional number system gives meaning. 12:52:09 So the first position is the 10 to the 0. Or the ones column, the next one be the 10 to the one or the tenth column, the next 10 to the 2 or the hundreds column. 12:52:17 This is what you're familiar with. Well, in binary, our base is 2 instead of 10. 12:52:23 So this is the 2 to the 0 column to raise the power of 0. So that's the ones column, but the next one over is 2 to the one. 12:52:31 In other words, the two's column. And the next one over is the 2 to the power of 2 or the fours column. 12:52:38 And the next one, the 2 to the power of 3 or the eighth column. So if we keep going that way, let's look, we've got the ones, twos, fours, 8, 1632, and this one is in the 60 fours column. 12:52:53 So we have a 64 plus a one is 65. Well, sure enough, look at that. 12:52:59 Over case, a is decimal 65. So I've shown you how to convert this binary number to a decimal number. 12:53:06 But here it's listed as 41 hexadecimal. Alex, I'll explain that in a moment. 12:53:13 Okay, so. Our character data type uses those 16 bits, 2 full bytes. Now remember the ASCII table, which is a subset of code only needs those first, right most 8 bits. 12:53:28 So the remaining leftmost bits, the remaining 8, are padded with leading zeros. So here is that ASD table again. 12:53:37 And here is a as a hexadecimal number. Well, hexadecimal values are very handy for computer scientists because they can quickly convert to binary. 12:53:49 For every hexadecimal. Texas that's this like a decimal digit. 12:53:56 They're called hexadecimal hexets, each one of these can be represented in 4 bits. 12:54:01 Why? Hacks and decimal goes from 0 to 15. It goes 0 through 9 and ABC. 12:54:10 Okay, that's how we represent 15 because we need a single symbol to represent each of them. And so if we flipped all of these to one, it would be one plus 2 plus 4 plus 8. 12:54:22 Add that up, you end up with 15. So in other words, we could do the entire range of a hexadecimal hexit in 4 bits. 12:54:29 So now let's look. So how do we represent one? Well, we've already seen that. 12:54:34 So it's just one. How do we know this one is 4? Well, this is the one. 12:54:38 This column, the twos column and here's the fourth column. So 0 1 0 0 0 0 one is how we would write. 12:54:47 41 hexadecimal in binary. 12:54:58 Now, the next thing to think about is the fact that our integer values are signed. So we what we end up doing is our registers. 12:55:08 Our storage space. PAS to have one bit to represent whether the number is positive or negative. So the leftmost bit is known as the sign bit. 12:55:19 Okay, if the sign bit is set to 0, it's representing a positive value. If the sign bit is set to one, it represents a negative value. 12:55:29 And so this is the reason why on the positive end of the range we only go to positive 127 because imagine setting all those bits to 0. 12:55:40 Well, that's the equivalent of the number 0, but 0 has to have a sign bit. So its sign bit is set to positive. 12:55:48 So where in most numbers we think of 0 as neutral, neither positive nor negative when we look on the number line. 12:55:56 Computers actually store 0 as a positive value. And that's why we only go to positive 1 27 in the range. 12:56:04 Okay, now. Here's the thing. How in the heck do we Create those negative integer values. 12:56:14 Well, it's not as simple as flipping the sign bit to one and then putting the positive value in. 12:56:20 No, the computer uses something called the twos complement. Okay, so let's see how this is done, Shelley. 12:56:35 So we're gonna use the two's complement method. We're gonna represent assigned into their value. 12:56:38 And so there's our bits and you can see each position is a power of 2 to the 0 is 1, 2 to the one is 2, then 4, 8, 1632, and 64. 12:56:51 So if you added them all up, all of these were set to one and the sign bit set to 0. 12:56:54 That would give us the number positive 1, 27. So let's work with creating a negative number. 12:57:02 Now this is the algorithm for the two's complement method. Please make sure this is in your notes. 12:57:08 You're going to need it for this course and your discreet structures course. So the first thing we need to do, we want to represent negative 34. 12:57:17 So how do we do it? Well, there's 4 easy steps. First, I'll tell you the steps, then I'll demonstrate that. 12:57:23 So the first step is find the positive value equivalent in binary. 2, flip all the bits. Now this is critical if the but if it's a bite register it means 8 bits so if there are bits that are unused on the left side you have to pad them with leading zeros because in step 2 you're going 12:57:45 to flip them. So zeros become ones, ones become zeros. And the third step is to add one. 12:57:53 Now you have the negative value representation. Now please note in that step 3 when you add one. You're adding one to the value that you created in step 2. 12:58:06 So here we go. Let's find 34. So this is 34. How do we know? 12:58:11 Well, there's no no ones. We have a 2, no fours, no eights, no sixteens. 12:58:16 But the 32, so 32 plus 2 is 34. Great. Do we have 8 bits? 12:58:22 Great, we do. So now let's flip those bits. So zero's become ones, ones become zeros. 12:58:30 That's pretty easy. Then all we're gonna do is add one. Now the trick is one plus one. 12:58:36 Is not 2 we don't have that number binary. But we can carry it over to the twos column, the next column over. 12:58:45 So what happens is one plus one equals 2, which is the same as if you had 9 plus one equals 10. 12:58:51 What happens? You write a 0 and then you carry the 10 to the next Binary, we carry 2. 12:58:58 So one plus one is 2, we write a 0, we carry that one over. And so we have one plus 0 is one. 12:59:04 Did that make sense? Now we have. The representation of negative 34. This is what it would appear in the register. 12:59:14 For funds, prove that it works from negative 1 28. You'll discover it's an exercise in futility. 12:59:26 Now, what's the difference between declaration, declarations, and initializations? Okay, well, all variables must be declared before they're used. 12:59:35 So in good program practice, we're gonna declare all our native variables at the beginning of the method. 12:59:40 As you recall in our flowchart, that's going to be one of our first boxes right after the scanner. 12:59:46 We're gonna say the storage space that we need. Now although you can declare variable throughout the process just before you use it, it is in poor programming form. 12:59:56 So as a good programmer, learning good habits, you're always going to declare all of your variables at the start of the program. 13:00:05 Okay, so for example, this is a declaration. I declared something called mine known, so that's my identifier and I said it's an integer type. 13:00:18 So it's gonna store a whole number integer type. Okay. The default stored values usually set to 0, not all programming languages do that though. 13:00:29 Now this is called an initialization. The first time that I use the variable and give it a value. 13:00:36 See the assignment operator there, then equal sign means assign 27 to the storage space. Okay. So that was done in 2 steps. 13:00:49 So, it's in good programming form to initialize all variables, with a starting value. 13:00:56 So variables later on when we get into looping structures, you if we're going to accumulate a sum, you're gonna want to start that variable at 0. 13:01:06 If for some reason it's going to be a product that's multiplied, you're going to want to start at that one because if you started at 0 the first time and a number times 0 will still be 0. 13:01:15 Okay, now this next example into another num equals 34. That's a declaration initialization in one step. 13:01:26 Most of the time this is probably what you're going to do unless you're getting data from the user. 13:01:37 Okay, so. Variables store fixed size amount of data. Remember that those the range of the values we don't have everything. 13:01:47 Every number that we can represent, we have to pick the correct data type. For the data that we are storing. 13:01:53 Okay, so the trick is a few little things about Java. When you signing an integer value, in other words, you're going to hard code a literal in. 13:02:05 Java assumes it to be event type. Thus, if you want it to be along, in other words, you're gonna declare it to be a long variable but you're giving it that initial literal value. 13:02:16 You are going to need to do something called forcing and I'll show you that in a moment. 13:02:22 The same problem happens with floating point numbers. Java always assumes those literals that you hard code into be of double type. 13:02:30 And so if you are using the float data type, which you should for the cost of driving assignment. 13:02:38 Then what happens is you're going to need to force it to be a float because literals in Java are treated for integers as in. 13:02:49 And you have to force the log. As doubles for floating point and so you have to force the flow. 13:02:55 Now how do you do that? Well, forcing is really pretty easy. After you write the long literal. 13:03:02 Just put the letter out after it. Preferably uppercase. It's easier to read than the lowercase L which looks an awful lot like the number one. 13:03:10 And to force a flow literal to be afloat, again, just place the number and the letter F after it. 13:03:18 So for example, 63 L. Or 26 app for afloat. And as noted, use uppercase for readability. 13:03:28 It makes it a little bit easier. 13:03:31 Now, that was our forcing example. Now, there's another thing you need to know. Variables have rank. 13:03:40 The bite is the smallest. So think about it this way. If the bite only takes up 8 bits. 13:03:46 Imagine you're emptying your bathroom garbage can and it is small. And your kitchen garbage can is bigger but you wanna make one trip to the the garbage. 13:03:56 So you take the bathroom garbage can, worth of garbage, and you put it inside the kitchen garbage bag. 13:04:02 There's no problem with that. There's plenty of room. If you were to try to do that in reverse and shove all the garbage from the kitchen into the bathroom can, you'd probably get into trouble. 13:04:13 And so the variables are ranked from the lowest the bite to the biggest storage space we have as the double. 13:04:21 Okay. And widening conversion means you're assigning a lower rank variable to a higher rank. So in other words, you have something you stored in a bite and now you want to store it in a short or an int. 13:04:35 No problem. There's plenty of space. And Java is perfectly okay with that. Narrowing conversion is when you try to put a larger variable into a smaller spot. 13:04:48 This is when we get into trouble. Like trying to put all the garbage from the kitchen into the bathroom smaller can. 13:04:55 So you're going to get a compiler error that the computer is not gonna allow you to do it, but you're going to find sometimes that you'll have a floating point value with a fractional portion and you want to truncate it. 13:05:09 In other words, remove that fractional portion. Well, you can by Typecasting. So the cast operator allows a manual narrowing conversion. 13:05:19 This is how you get past the compiler. So for example, if I have a long X and a short Y. 13:05:26 What I can do is say why is gonna equal give me the short version of apps. So the value of access truncated to a short and stored in Y. 13:05:37 Now please note. The original long X is not touched in any way, shape or form. The value that is still in X is still contains the long value. 13:05:47 Why only contains it's truncated short version. Okay, this also works for floating point numbers and this is often times we have a floating point number and we want to remove the fractional portion. 13:06:02 So same thing. We have a float A equals 3.1 4 and we have an int B. 13:06:09 There we use the cast operator, and what happens is B only stores 3. Okay, this is very important. 13:06:18 Is probably on your final exam and you'll be utilizing this tool, the cast operator. Often as well as forcing throughout the course. 13:06:28 So please make sure that you understand these concepts and if you do not. Please, send me an email or drop by during office hours. 13:06:36 Now this next slide is on. Arithmetic operators assignment and combined assignment also called compound operators. 13:06:44 Most of these you're probably already familiar with, but there's a couple that I will spend a little bit more time with. 13:06:51 Okay, so the binary operators, meaning they use 2 operands, that meaning that they work with binary numbers, but they that they use 2 operands and the associativity means red from left to right. 13:07:05 These are ones you're probably already familiar with. The plus sign for addition, the minus for subtraction, the asterisk for multiplication. 13:07:13 The forward slash for division. Now please note the remainder is lost if we do integer. 13:07:20 Okay, so if you see on the screen there 2 divided by 17 both 2 and 17 are integer numbers. 13:07:27 If you do this division with the computer, 2 goes into 17. 8 times, that's 2 times 8 is 16 with the remainder of one. 13:07:37 We lose the fractional portion. So 2 divided by 17 will just end up with the number 8. 13:07:46 Now, that brings me to the next operator, the modulus operator, that percent sign, that's used to capture the remainder, value in integer division. 13:07:58 So like I said, 2 divided by 17 is 8, 8 times 2 is 1617 minus 16 is one. 13:08:04 So our remainder is one. So if I did 2 modulus 17. 13:08:13 Sorry, 17 MoD 2. I put that in correct order. What you will end up with. Is one. 13:08:21 So division gives us the quotient. And modulus gives us the remainder. 13:08:29 Okay, unary operators use one operand. We have the negative value, the the and that works from right to left. 13:08:39 Now. This is the assignment operator. We're in math class and then whatever is on the left side was equal to whatever was on the right side. 13:08:48 In computer programming this is assignment whatever is on the right side or our val is assigned to the left side or LV. 13:09:00 It's a binary operator. Like I said, his associativity is right to left and assigns the literal or expression value on the right. 13:09:07 To the identifier on the left. Very important to note. One last set of operators. These are called combined assignments. 13:09:18 Okay, and their associativity is right to left. There's plus equal minus equal multiply equal divide equal and modulus equal. 13:09:26 What they do. Is if I had. variable, let's say called some. 13:09:32 And I say some equals some plus one. It means. St in some, whatever its current value is and add one to it. 13:09:43 So whatever the current values in some, add one to it and then restored in some. Well, the shorthand notation. 13:09:50 Could be some plus equal one. That says the same thing. It basically says. Take what's in some, add one to it, and then restore it in some. 13:10:01 So those combined assignment operators as you get more comfortable and confident with Java, you can use them, to start with, you might want to just write out the entire command so that you understand what is happening. 13:10:15 Okay, so there's my example. X equals x plus 2 is the same as saying x plus equal to. 13:10:22 They both end up taking the current value in X, adding 2 to it and restoring. The value in X. 13:10:31 Okay, couple of other operators, increment and decrement. Okay. And they're unary operators. 13:10:40 They work on one operand and they have positioning prefix meaning they're in front of the variable and post fix, meaning they're after the variable. 13:10:48 Now this is very important. This again will also be something that is on. Your final exam. So understanding it. 13:10:56 Okay. Let me put this out here for you. So let's assume X equals one. 13:11:05 So when we say x equals one and then we go plus plus x. When it is prefixed, it means update the current value and use their current updated value. 13:11:18 So in that statement, used to and in storage the value is 2. The decrement works in the same way. 13:11:26 So it decrements it, use that decremented value, then that value is also in storage. Now post fix means use the current value in that variable and then update its value in memory. 13:11:40 So something like this happens. Let's say y equals 4. So why minus minus means in this statement, use the current value of y, which is 4, and then updated in storage to be 3. 13:11:56 Okay, because these statements on this screen are not in an output statement. It is not as obvious. Try putting these prefix and post fix operators in a little program with an output statement so that you can see how the prefix and post fix works. 13:12:14 I believe I might even have a sample program of this type posted for this week. 13:12:22 Okay, now the other thing to know is operator precedence. This should have been covered in your college algebra class. 13:12:28 I hope you remembered it will be recovered in your discrete structures class and a little bit in this class as well. 13:12:34 So operators have precedence. So if we have a long expression. The highest precedence is at unary operator and also near the top. 13:12:46 Let me put this in here. The increment and decrement operators. Followed by multiplication. 13:12:56 Division and modulus. Now notice they're all the same. So at that point we work with associativity. 13:13:01 So where you might have learned PEMDAS in school at some point, parentheses, exponent, multiplication, division, addition and subtraction. 13:13:09 One of the tricks is multiplication and division actually have the same precedent. So then we worry about associativity, meaning reading it from left to right. 13:13:19 And so the lowest precedent is addition and subtraction. And again, in PEM does, this is addition, If subtraction comes before addition, they both have the same precedence. 13:13:34 So associativity takes place. And we read it from left to right. So when in doubt, use parentheses to make sure that you're expressing the problem you want to express. 13:13:45 Okay, so let's look an example here. So if I have x equals 2 plus 5 times 4. 13:13:54 Let's stored an X. 13:13:59 Well, if we were in class, you would shout it out. Shout it out at home. Did you come up with 22? 13:14:04 Then you're correct. If it's 5 times 4. I'm multiplication as higher precedence over addition, which is 20 plus 2. 13:14:14 Okay, let's try another one. How about this? With parentheses x equals 2 plus 5 times 4. 13:14:20 What's the answer now? 13:14:26 Did you come up with 28? Then you're correct. Cause we do 2 plus 5 is 7 times 4 is 28. 13:14:34 Do you see how the precedents? Preserved the order. Okay, how about this one? Y equals 7 minus 2 plus 3. 13:14:47 Did you come up with 8? Great. Yeah, because addition and subtraction have the same precedence. 13:14:51 So then we use associativity and we read from left to right. So 7 minus 2 is 5 plus 3 is 8. 13:14:59 How about this? Y equals 7 minus 2 plus 3. 13:15:07 There's those parentheses again. Parentheses happen first, so 2 plus 3 is 5. 13:15:11 And 7 minus 5. The answer is 2. So when in doubt, use those parentheses to make sure you're expressing the expression as needed. 13:15:24 Okay, now. The math class, if you notice, math was just like system. It is inherent in the language. 13:15:32 So the map class, the math object has all sorts of nice features that you might like to use. For example, the one I'm about to show you might find beneficial or the pizza problem. 13:15:46 It's the power method. It uses 2 double value arguments and it raises the first argument to the power of the second argument. 13:15:56 Now, let's look at example and code. This is what it looks like. So if we had a variable X that was declared as a double. 13:16:05 And we said math, notice it's an object, upper case m dot pow. How is the power method that will do the calculations? 13:16:14 And so my first value is for raised to the power of 2. So x is going to equal 4 race the power 2 and what will be stored in X is 16. 13:16:27 Actually, it'll be 16.0 because it's a floating point value. 13:16:33 Sorry, a double, but it'll still have the 0. 13:16:37 Okay. Another method that's popular. We'll get a couple of them here. Let's put them off. 13:16:46 Our absolute value, ceiling. Floor, Max, Min. Random. Now please know random generates a double value greater than or equal to 0 and less than one. 13:17:01 Round which rounds. And square root. You might find all of these very, very useful. So one place to spend a little time exploring is maybe the Java language library, the math functions. 13:17:17 You're probably not gonna use all of them. These ones I've listed here are some of the common ones that you would probably use in programming one. 13:17:25 Now there is one other very useful tool in that math class. And the math class does have a named constant. 13:17:32 Remember Constance, they're all upper case. It has math dot pi. You're going to need math dot pi for the pizza pie problem. 13:17:42 It generates the number you see on the screen with more accuracy than just saying 3.1 4 to represent pi. 13:17:57 Now, a little brief introduction to scope. It's not just mouthwash. 13:18:03 So local variables means variables declared within a method like made only exist within that. Once the processes are commands within a particular method and so does the existence of the data storage for that method. 13:18:17 So as noted, we declare our variables at the start of main. Once main is run and the process is finished. 13:18:24 Those variables are gone. We will continue to expand on this topic as a course course progresses, especially when we get to writing methods other than main. 13:18:36 So here is your challenges. I finish up this module. I would like you to modify the payroll program to get the number of hours worked. 13:18:47 And the pay rate. From user input. So the program that I gave you, we hard coded those values in. 13:18:57 But now I'm gonna ask that you prompt and store. Values for hours worked and pay rate and modify this program. 13:19:07 As a reminder, this was our original flowchart. You can start with this flowchart, but you're going to have to add more. 13:19:16 Boxes and more information in order to get the hours and pay rate from the user. So you can use this as a starting point, but you will need to modify it. 13:19:29 Remember the process is linear so you might have to move some of the arrows around. And add to this flow chart. 13:19:39 I hope you enjoyed this module.