Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,520 --> 00:00:02,710
You can update the value inside a variable.
2
00:00:03,850 --> 00:00:06,870
You saw that defining a variable and storing a value is pretty simple.
3
00:00:08,109 --> 00:00:13,210
You need to write the type of value we can store, this variable in particular can only store integer
4
00:00:13,210 --> 00:00:13,840
values.
5
00:00:15,330 --> 00:00:18,810
You also need to write the variable name and the value that you want to store.
6
00:00:20,380 --> 00:00:25,810
So available stores information about your app, but as time passes, things change and you need to
7
00:00:25,810 --> 00:00:27,070
update that information.
8
00:00:28,810 --> 00:00:32,470
That's why in this video, you're going to learn to update the value inside a variable.
9
00:00:35,170 --> 00:00:39,700
First thing I need you to do is create a new class by yourself inside the section to project make a
10
00:00:39,700 --> 00:00:44,620
new file named Buster Java and then make sure the bus class has the main method.
11
00:00:45,140 --> 00:00:46,900
Take a couple of seconds to do that.
12
00:00:53,530 --> 00:00:58,240
All right, how do you update variables if you want to update a variable, it's fairly simple.
13
00:00:58,240 --> 00:01:04,000
Each have to set it equal to a new value and the value inside changes every time you update it.
14
00:01:06,010 --> 00:01:12,070
I prepared a fun example so that we can learn more about this, you, the Java prodigy, you found a
15
00:01:12,070 --> 00:01:18,780
job at the Ministry of Transportation in the ministry wants you to build a Java program, the programming
16
00:01:18,800 --> 00:01:23,830
you to print, how many passengers are inside a bus, and then you need to print the number of passengers
17
00:01:23,830 --> 00:01:24,700
at every stop.
18
00:01:26,120 --> 00:01:30,530
So I'm going to assume you ready set the class up and before we start our code, you'll notice that
19
00:01:30,530 --> 00:01:32,650
the terminal output is pretty cluttered.
20
00:01:33,470 --> 00:01:38,630
You could just close the terminal, open a new terminal, but that's pretty lame if you ask me.
21
00:01:39,410 --> 00:01:42,740
We can instead clear the terminal output by writing.
22
00:01:42,740 --> 00:01:43,310
Clear.
23
00:01:45,550 --> 00:01:49,730
Nice, this command is very useful, and I'm going to add it to your cheat sheet.
24
00:01:50,910 --> 00:01:53,380
OK, so the bus driver is starting his shift.
25
00:01:53,680 --> 00:02:01,420
He boarded the bus and naturally the bus is empty and he starts with zero passengers and passengers
26
00:02:01,420 --> 00:02:02,710
is equal to zero.
27
00:02:03,190 --> 00:02:04,450
Don't forget your semicolon.
28
00:02:05,760 --> 00:02:13,190
All right, his first stop is the river station, let's say there he picks up nine passengers.
29
00:02:13,890 --> 00:02:17,550
So we need to update the number of passengers by nine passengers.
30
00:02:19,190 --> 00:02:21,050
Is equal to passengers.
31
00:02:23,190 --> 00:02:24,060
Plus nine.
32
00:02:28,370 --> 00:02:35,300
Then after you update the number of passengers, you can print the new value system, dot out dot print
33
00:02:35,300 --> 00:02:37,310
line passengers.
34
00:02:42,960 --> 00:02:47,730
All right, compiling the code, Java Sea Bass Java and running it.
35
00:02:51,810 --> 00:02:58,320
Clearly now there are nine passengers in the second line, you said passengers equal to itself, zero
36
00:02:58,320 --> 00:03:01,380
plus nine, and now it stores a value of nine.
37
00:03:05,470 --> 00:03:11,220
Now the bus driver is approaching the Sugar Mountain sweetshop in, your passengers go crazy for sweets.
38
00:03:11,860 --> 00:03:14,250
So there he drops off five passengers.
39
00:03:15,100 --> 00:03:21,790
Once again, you need to update the number of passengers that is passengers is equal to passengers minus
40
00:03:21,790 --> 00:03:22,300
five.
41
00:03:24,830 --> 00:03:31,640
And after you update the value print system, dot, dot, dot, print line passengers.
42
00:03:35,530 --> 00:03:38,650
And using the up key, go ahead and compile the code.
43
00:03:44,620 --> 00:03:46,910
All right, we started with zero passengers.
44
00:03:47,020 --> 00:03:50,800
The bus driver picked up nine and dropped off five.
45
00:03:52,210 --> 00:03:57,400
So the pattern follows four subtraction, the computer sets the value of passengers equal to itself,
46
00:03:57,550 --> 00:03:59,350
nine minus five.
47
00:04:00,710 --> 00:04:06,860
Now, there are four passengers in the bus and let's say the last stop is the post office and they're
48
00:04:06,860 --> 00:04:09,620
the bus driver drops off the remaining passengers.
49
00:04:12,010 --> 00:04:15,820
So passengers equal to passengers minus four.
50
00:04:19,850 --> 00:04:25,490
And we're going to print the value system, dot, dot, dot, print line passengers.
51
00:04:32,280 --> 00:04:34,680
Recompile your code and run it.
52
00:04:39,460 --> 00:04:41,410
And everything works out as expected.
53
00:04:42,520 --> 00:04:45,480
Your application updates information at every stop.
54
00:04:47,630 --> 00:04:51,620
The bus starts with zero passengers, picks up nine.
55
00:04:52,860 --> 00:04:54,180
Drops out five.
56
00:04:55,570 --> 00:04:56,920
And drops the rest.
57
00:04:59,640 --> 00:05:03,930
This is a pretty common way of updating integer variables, but there is an easier way.
58
00:05:05,100 --> 00:05:11,190
And it's known as the plus equals operator, the plus equals operator updates a variable by adding the
59
00:05:11,190 --> 00:05:12,330
value on the right.
60
00:05:15,230 --> 00:05:21,230
So you can replace the first update with the plus equals operator passengers plus equals nine.
61
00:05:24,170 --> 00:05:26,540
And if you ask me, this looks a lot better.
62
00:05:26,780 --> 00:05:28,000
Let's see if it works.
63
00:05:36,590 --> 00:05:37,830
And indeed, it does.
64
00:05:38,420 --> 00:05:45,950
Passenger starts at zero and the plus equals operator updates the variable by adding nine fairly intuitive.
65
00:05:46,640 --> 00:05:48,320
Now, what about these updates?
66
00:05:48,890 --> 00:05:51,160
Well, there's also the minus equals operator.
67
00:05:51,830 --> 00:05:55,880
This one, as you'll probably guess, it's tracks the value on the rights.
68
00:05:59,730 --> 00:06:03,390
So you can replace the remaining updates with the minus equals operator.
69
00:06:12,830 --> 00:06:14,120
Recompile the code.
70
00:06:17,770 --> 00:06:19,600
And everything works as expected.
71
00:06:21,720 --> 00:06:25,590
In both cases, it appears the variable by subtracting the value on the right.
72
00:06:30,190 --> 00:06:35,170
And in this lesson, you learn to update the value inside a variable, you built a Java program that
73
00:06:35,170 --> 00:06:41,920
updates the number of passengers when the bus picks up or drops passengers off plus equals and minus
74
00:06:41,920 --> 00:06:45,310
equal to update the variable using the value on the right.
75
00:06:46,460 --> 00:06:47,270
And that is all.
7396
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.