1 00:00:00,660 --> 00:00:03,570 We can use math operators to operate on values. 2 00:00:04,930 --> 00:00:12,250 So far, you learned about five types of values into four whole numbers, long for large numbers, double 3 00:00:12,250 --> 00:00:19,870 for decimals, car for single characters and string for a string of characters also known as text. 4 00:00:20,950 --> 00:00:24,040 You can use math operators to play with these values and operate on them. 5 00:00:24,490 --> 00:00:27,850 And so in the next two videos, we're going to look at all the math operators in Java. 6 00:00:28,980 --> 00:00:35,820 Namely, the plus operator, which is used to add values, the minus operator to subtract values, the 7 00:00:35,820 --> 00:00:41,700 multiplication operator represented by the asterisk, the division operator represented by the front 8 00:00:41,700 --> 00:00:49,410 slash the modulus operator, which returns the remainder of a division, the add one operator, which 9 00:00:49,410 --> 00:00:55,230 increases their value by one B. Subtract one operator, which decreases a value by one. 10 00:00:55,650 --> 00:01:02,700 The add by operator, which increases the value by the number on the right, and you subtract by operator, 11 00:01:02,700 --> 00:01:05,360 which decreases the value by the number on the rights. 12 00:01:06,360 --> 00:01:07,440 What a list. 13 00:01:07,590 --> 00:01:07,920 All right. 14 00:01:07,920 --> 00:01:11,250 For this lesson, we're going to start by looking at the first four operators. 15 00:01:15,260 --> 00:01:20,000 First thing I'll need you to do is create a new class by yourself inside the section to folder, making 16 00:01:20,000 --> 00:01:25,070 you file name math operations Java and make sure the class has a main method. 17 00:01:31,560 --> 00:01:36,870 OK, first, we need to make some variables because math operators like you operate on values, so we'll 18 00:01:36,870 --> 00:01:39,570 need to set up some variables with store values inside them. 19 00:01:42,910 --> 00:01:49,180 We'll start by making two into variables and bag of sweets one. 20 00:01:53,890 --> 00:01:55,150 Is equal to five. 21 00:01:58,050 --> 00:01:59,160 And int. 22 00:02:02,970 --> 00:02:04,290 Bag of sweets to. 23 00:02:05,580 --> 00:02:06,600 Is equal to 10. 24 00:02:07,520 --> 00:02:09,620 Now we're going to make two long variables. 25 00:02:11,060 --> 00:02:13,070 Long stars in Milky Way. 26 00:02:18,160 --> 00:02:26,500 Is equal to and there are 2.5 billion stars, civil right to five and eight zeroes, one, two, three, 27 00:02:26,500 --> 00:02:28,340 four, five, six, seven, eight. 28 00:02:28,950 --> 00:02:34,250 And don't forget the L to reassure Jova that, hey, we are going to store this in a long variable and 29 00:02:34,250 --> 00:02:35,920 that will say long. 30 00:02:38,050 --> 00:02:39,700 Stars in. 31 00:02:41,620 --> 00:02:42,760 Andromeda. 32 00:02:43,900 --> 00:02:52,410 Is equal to and there are a trillion stars, so we'll say one and 12 zeroes, but don't forget the al. 33 00:02:56,850 --> 00:03:03,930 Finally will make two double variables, double test score is equal to six point seven. 34 00:03:07,390 --> 00:03:10,030 And double bonus marks. 35 00:03:13,520 --> 00:03:20,900 Is equal to 2.5 in any case, we created six variables to enter variables. 36 00:03:22,720 --> 00:03:26,620 Too long variables and two double variables. 37 00:03:27,700 --> 00:03:32,080 Now we can start using operators and as we play around with these operators, you're going to notice 38 00:03:32,080 --> 00:03:36,220 a very interesting pattern in operation between home numbers. 39 00:03:36,220 --> 00:03:42,340 Always returns a whole number and an operation between decimals will always preserve the decimal. 40 00:03:43,240 --> 00:03:46,090 I want you to keep this in mind as we go through every operator. 41 00:03:49,560 --> 00:03:54,690 We'll start with the plus operator, you've seen it before, the plus operator adds to values and the 42 00:03:54,690 --> 00:03:56,410 symbol is without a doubt, the plus side. 43 00:03:57,240 --> 00:04:02,760 So we're going to add every pair of end long and double values and print each results system. 44 00:04:02,760 --> 00:04:04,920 Dot out, dot print line. 45 00:04:06,820 --> 00:04:10,390 Bag of sweets, one plus bag of sweets, two. 46 00:04:17,570 --> 00:04:25,640 Once again, systemd out front line stars in Milky Way, plus stars in Andromeda. 47 00:04:28,250 --> 00:04:34,310 Third time system got out front line test score, plus bonus marks. 48 00:04:40,890 --> 00:04:42,210 We're in a compiler code. 49 00:04:55,970 --> 00:05:01,400 Now, I ask you, isn't it nice when things are consistent, because notice that adding two whole numbers 50 00:05:01,400 --> 00:05:07,460 returns a whole number, you saw this from adding the Enten long values and just the same adding to 51 00:05:07,460 --> 00:05:08,920 decimals gives a decimal. 52 00:05:09,080 --> 00:05:10,620 You saw this for the double value. 53 00:05:11,060 --> 00:05:15,650 It's always nice to see a pattern because it makes coding easier when you can predict what type of return 54 00:05:15,650 --> 00:05:17,600 value, what type of result you're going to get. 55 00:05:18,670 --> 00:05:22,630 But don't forget, the plus symbol is special because it goes beyond just numbers. 56 00:05:23,090 --> 00:05:25,560 We can use it to blend values into a string. 57 00:05:26,300 --> 00:05:30,530 So we'll start with the bag of sweets now before you do what I'm about to do. 58 00:05:30,560 --> 00:05:32,030 Just watch me do it first. 59 00:05:32,030 --> 00:05:33,800 I'll give you time to pause the video in a bit. 60 00:05:34,550 --> 00:05:40,010 But what I'm going to do is go to the resources article, math operators, resources, and I'm going 61 00:05:40,010 --> 00:05:41,990 to copy every sentence from part one. 62 00:05:45,300 --> 00:05:46,650 Pace it at the very bottom. 63 00:05:49,720 --> 00:05:51,910 Then, while everything is highlighted. 64 00:05:54,360 --> 00:05:59,880 Press command slashed the comment at all, if you're using a Mac or control slash for windows, and 65 00:05:59,880 --> 00:06:02,720 this is probably a good place to pause the video and do what I just did. 66 00:06:10,220 --> 00:06:14,960 All right, by the way, don't mind the front slashes their only comments and have zero effect on your 67 00:06:14,960 --> 00:06:15,320 code. 68 00:06:16,020 --> 00:06:20,030 Besides, if you did the workbooks and I encourage you not to skip any of them, you would have already 69 00:06:20,030 --> 00:06:23,210 encountered comments in any case. 70 00:06:23,240 --> 00:06:26,870 I'm going to cut and paste the first sentence into a print statement. 71 00:06:31,650 --> 00:06:33,420 When I put string quotes around it. 72 00:06:38,450 --> 00:06:43,340 And now look for the placeholder, disconnect the string on both ends. 73 00:06:47,640 --> 00:06:50,520 And you're going to pace the operation on to the placeholder. 74 00:06:55,970 --> 00:06:58,240 Put it in brackets, that's really important. 75 00:07:02,360 --> 00:07:08,810 Then use the plus symbol to blend the result of the math operation into the strike call. 76 00:07:10,770 --> 00:07:13,080 Compile the code and run it. 77 00:07:17,510 --> 00:07:21,710 This looks good, plus embeds the number inside the string. 78 00:07:22,820 --> 00:07:27,500 Now I'm going to minimize the file explorer so that you can see everything I'm doing and notice that 79 00:07:27,500 --> 00:07:30,950 when blending a math operation into a string, I put it inside brackets. 80 00:07:31,310 --> 00:07:33,120 I'll tell you why in just a second. 81 00:07:33,890 --> 00:07:36,620 For now, let's do the same thing for the long and double values. 82 00:07:37,190 --> 00:07:39,560 Copy each sentence into a string, quote. 83 00:07:46,800 --> 00:07:49,320 Substitute the operation for the placeholder. 84 00:08:03,230 --> 00:08:05,660 And then blend that result into the drink. 85 00:08:08,300 --> 00:08:10,760 And I'm just going to quietly do the same thing for the last one. 86 00:08:38,880 --> 00:08:39,289 All right. 87 00:08:39,309 --> 00:08:44,039 Hope that wasn't too hard on compile the code, runit, run it. 88 00:08:47,590 --> 00:08:48,810 This looks pretty good. 89 00:08:50,120 --> 00:08:53,930 All right, now let's try removing the brackets, do it for the first one. 90 00:09:00,940 --> 00:09:02,530 Now recompile the code and run it. 91 00:09:09,550 --> 00:09:13,090 I'm pretty sure that five plus 10 isn't 500 in ten. 92 00:09:14,760 --> 00:09:18,750 That's because Java didn't actually add the numbers up, it just connected them. 93 00:09:20,110 --> 00:09:25,990 If you don't put brackets, things are going to happen from left to right, the string connects to the 94 00:09:25,990 --> 00:09:26,780 number five. 95 00:09:27,400 --> 00:09:28,720 So now you're sentences. 96 00:09:28,900 --> 00:09:31,060 Fred and George collected five. 97 00:09:33,270 --> 00:09:36,290 And now this new updated sentence connects to the number 10. 98 00:09:37,110 --> 00:09:42,720 There are new sentences, Fred and George collected five, ten, and then we connect the last string, 99 00:09:42,720 --> 00:09:47,250 which results in the sentence being Fred and George collected five, 10 suites. 100 00:09:48,390 --> 00:09:53,280 But when you put brackets, it tells you that this is a math operation, make sure to add these two 101 00:09:53,280 --> 00:09:57,970 numbers first, then whatever the final result is, can that that into the string? 102 00:09:58,800 --> 00:10:03,990 Long story short, just to remember to put operations in brackets if you want to display the result 103 00:10:03,990 --> 00:10:05,670 into a string, that's all. 104 00:10:06,660 --> 00:10:07,080 All right. 105 00:10:07,080 --> 00:10:11,700 Recompile the code and run it and you get the result that we expect. 106 00:10:12,900 --> 00:10:14,730 Go ahead and delete these three comments now. 107 00:10:21,010 --> 00:10:26,800 All right, let's talk about the minus operator, this one is used to subtract two numbers and the code 108 00:10:26,800 --> 00:10:29,320 for subtracting stuff is the hyphen. 109 00:10:32,290 --> 00:10:39,550 So back in our code, we're going to attract every pair of end, long and double values system out print 110 00:10:39,580 --> 00:10:40,090 line. 111 00:10:41,780 --> 00:10:45,140 Bag of sweets to mine, his bag of sweets one. 112 00:10:54,250 --> 00:10:56,530 System systemd out print line. 113 00:10:59,910 --> 00:11:04,530 Stars in Andromeda, minus stars in the Milky Way. 114 00:11:09,410 --> 00:11:11,930 And system dot our print line. 115 00:11:22,810 --> 00:11:24,370 Minus bonus marks. 116 00:11:27,590 --> 00:11:29,720 OK, let's compile the code. 117 00:11:35,930 --> 00:11:42,560 And the pattern stays intact, subtracting whole numbers returns a whole number, the supplies to the 118 00:11:42,560 --> 00:11:47,420 end in long values and subtracting decimals returns a decimal. 119 00:11:49,060 --> 00:11:54,610 Once again, we can join every member into a string, you're going to copy the sentences down below 120 00:11:54,610 --> 00:11:57,070 and we're going to copy each one into string quotes. 121 00:12:00,410 --> 00:12:01,700 One by one. 122 00:12:15,580 --> 00:12:15,920 Great. 123 00:12:15,940 --> 00:12:20,020 We're done with that now we're going to disconnect e string where there is a placeholder. 124 00:12:20,750 --> 00:12:21,490 So here. 125 00:12:35,710 --> 00:12:41,620 Once you do that, substitute each placeholder at the operation whose return value we want to blend 126 00:12:41,620 --> 00:12:42,490 into the string. 127 00:12:46,510 --> 00:12:51,760 And notice that I'm putting each math operation inside brackets, if you don't do that, you'll get 128 00:12:51,760 --> 00:12:52,230 an error. 129 00:12:53,020 --> 00:12:56,890 So, yeah, like I said before, if you're trying to blend the result of an operation into a string, 130 00:12:57,340 --> 00:13:00,910 whatever the operation is, put it in brackets and you should be good. 131 00:13:25,280 --> 00:13:28,760 All right, now recompile the code and run it. 132 00:13:32,310 --> 00:13:33,030 Looks good. 133 00:13:38,290 --> 00:13:43,510 The next operative we can talk about is the multiplication operator, which we use to multiply numbers 134 00:13:43,690 --> 00:13:47,230 and encode the symbol to multiply numbers as the asterisk. 135 00:13:48,270 --> 00:13:55,560 So we're going to multiply the intense and double values system, dot out, dot print line. 136 00:13:57,960 --> 00:14:02,010 Bag of sweets, one times, bag of sweets to. 137 00:14:06,030 --> 00:14:07,470 We'll do the same thing here. 138 00:14:09,380 --> 00:14:11,150 Front line test score. 139 00:14:16,590 --> 00:14:18,660 Times bonus marks. 140 00:14:22,620 --> 00:14:24,060 We're in a compiler code. 141 00:14:29,210 --> 00:14:30,410 Then run it. 142 00:14:34,420 --> 00:14:39,850 And the pattern is strong because once again, multiplying whole numbers returns a whole number and 143 00:14:39,850 --> 00:14:42,340 multiplying decimals preserves the decimal. 144 00:14:43,640 --> 00:14:48,410 And once again, trying to connect each result into a string, so we'll copy these sentences into street 145 00:14:48,410 --> 00:14:49,040 books. 146 00:14:58,380 --> 00:14:59,460 One by one. 147 00:15:12,070 --> 00:15:17,950 And we're going to substitute each placeholder with the results for multiplying each pair of numbers. 148 00:15:55,160 --> 00:15:59,600 All right, that is that you can go out and compile the code and run its. 149 00:16:02,550 --> 00:16:05,310 And our storyline is really developing. 150 00:16:07,080 --> 00:16:12,120 You're likely wondering why and multiply the long values, and that's because multiplying two billion, 151 00:16:12,120 --> 00:16:19,500 500 million by a one trillion, as I calculate it, equals to sextillion 500 quintillion. 152 00:16:19,800 --> 00:16:21,000 That's a lot of illions. 153 00:16:21,900 --> 00:16:27,140 And the range for long variables is nine quintillion, so even long can't store that big of a number, 154 00:16:27,780 --> 00:16:31,950 and if you were to try to perform this multiplication, it wouldn't work because Java is just going 155 00:16:31,950 --> 00:16:33,240 to return a random number. 156 00:16:34,900 --> 00:16:39,640 In any case, the last operation we're going to talk about is the division operator, and it's represented 157 00:16:39,640 --> 00:16:40,290 by the front. 158 00:16:41,710 --> 00:16:43,710 And we can use it to divide numbers. 159 00:16:43,930 --> 00:16:50,890 We're going to start with the end and long values system output line, bag of sweets, too, divided 160 00:16:50,890 --> 00:16:52,180 by a bag of sweets one. 161 00:16:56,700 --> 00:17:00,630 And once again, system, dot, dot, print line. 162 00:17:05,500 --> 00:17:09,040 Stars in Andromeda, divided by the stars in the Milky Way. 163 00:17:13,760 --> 00:17:14,930 Compile this code. 164 00:17:18,520 --> 00:17:24,339 And run it and as I expected, dividing whole numbers returns a whole number. 165 00:17:25,290 --> 00:17:27,670 We see this through the end and long values. 166 00:17:28,440 --> 00:17:30,480 All right, now we'll divide the double values. 167 00:17:32,170 --> 00:17:37,330 System not out front line test score divided by bonus marks. 168 00:17:49,460 --> 00:17:54,590 And like always, double preserves the decimal places, no matter what operation you're using. 169 00:17:55,810 --> 00:18:00,040 All right, well, unless they going to do before we wrap up this video's to connect each number to 170 00:18:00,040 --> 00:18:00,550 a string. 171 00:18:01,090 --> 00:18:04,270 So, as always, copy each sentence into a string, quote. 172 00:18:06,180 --> 00:18:06,960 One by one. 173 00:18:06,990 --> 00:18:07,700 Take your time. 174 00:18:16,960 --> 00:18:22,660 Once you do delete the comments and now we're going to replace each placeholder with the result of each 175 00:18:22,660 --> 00:18:23,260 division. 176 00:18:50,290 --> 00:18:52,540 All right, we can go back and compile the code. 177 00:18:55,650 --> 00:18:56,580 And runit. 178 00:18:59,330 --> 00:19:00,150 Great stuff. 179 00:19:00,170 --> 00:19:04,460 We created quite the storyline, and if you feel like things were getting repetitive, this means you're 180 00:19:04,460 --> 00:19:06,290 getting used to it, which is very good. 181 00:19:07,520 --> 00:19:12,680 And so in this lesson, we used four of the nine math operators, you also saw a very nice pattern. 182 00:19:13,130 --> 00:19:18,800 A math operation between whole numbers will always return a whole number and a math operation between 183 00:19:18,800 --> 00:19:21,170 decimals will always preserve the decimal. 184 00:19:21,920 --> 00:19:26,120 And it's nice to see this pattern because it makes coding easier when you understand how things are 185 00:19:26,120 --> 00:19:26,810 going to behave. 186 00:19:27,470 --> 00:19:32,210 We saw this pattern when we applied the first four operators and now I'm going to show you how to use 187 00:19:32,210 --> 00:19:33,290 the remaining five.