Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,830 --> 00:00:07,960
In part one, you learn to use scanner methods next line, which works for the user to enter a string
2
00:00:07,960 --> 00:00:13,900
value and next and to watch waits for the user to enter an integer next double, which waits for the
3
00:00:13,900 --> 00:00:17,590
user to enter a double value and using these scanner methods.
4
00:00:17,620 --> 00:00:22,330
We ask the user a lot of questions and the user responded to each one.
5
00:00:23,140 --> 00:00:27,100
So in this lesson, our application is going to respond back to the user.
6
00:00:30,260 --> 00:00:36,260
All right, after the user answers every question, it would be nice to respond with a thank you for
7
00:00:36,260 --> 00:00:38,090
answering all five questions.
8
00:00:39,080 --> 00:00:43,680
Now, should we hard code the number five directly inside the print statement?
9
00:00:44,240 --> 00:00:45,080
I don't think so.
10
00:00:45,770 --> 00:00:51,990
Imagine this was a bigger application with dozens of questions or I dare say hundreds of questions.
11
00:00:52,610 --> 00:00:55,790
Are you going to keep counting the questions every time you add a new one?
12
00:00:56,720 --> 00:01:00,460
I'd be more comfortable having Java count the number of questions for me.
13
00:01:01,460 --> 00:01:06,740
So what I'm going to do is make an entire variable counter and first it's going to store the value of
14
00:01:06,740 --> 00:01:07,210
zero.
15
00:01:08,150 --> 00:01:14,090
Then we can use the add one operator plus plus that we learned about and that's going to increase the
16
00:01:14,090 --> 00:01:16,850
counter value by one every time we ask a question.
17
00:01:21,800 --> 00:01:27,560
And so in the survey costs, we're going to make it into variable counter at the very top end counter
18
00:01:28,040 --> 00:01:29,690
and set it equal to zero.
19
00:01:30,860 --> 00:01:34,640
And now we're going to increase the counter value by one after each question.
20
00:01:35,630 --> 00:01:37,370
Counter plus plus here.
21
00:01:40,540 --> 00:01:42,130
Counter plus plus.
22
00:01:45,460 --> 00:01:47,050
Counter plus plus.
23
00:01:50,070 --> 00:01:52,800
And do that two more times.
24
00:01:59,700 --> 00:02:00,780
One last time.
25
00:02:07,690 --> 00:02:11,170
Once the code gets to hear, the counter value should be five.
26
00:02:11,680 --> 00:02:14,230
So at this point, we have the users values.
27
00:02:14,240 --> 00:02:15,720
We counted the questions.
28
00:02:15,730 --> 00:02:17,080
It's time to respond.
29
00:02:18,160 --> 00:02:24,760
We're going to print a message that refers to the user by name, so system, dot, dot, print line.
30
00:02:26,700 --> 00:02:27,990
We're going to say thank you.
31
00:02:30,420 --> 00:02:33,120
Break the string and put the user's name.
32
00:02:41,750 --> 00:02:47,480
And then we'll reconnect the string and say thank you, some name for answering all.
33
00:02:52,260 --> 00:02:54,450
Break the string and put the counter value.
34
00:03:01,730 --> 00:03:08,900
And then right questions at the end, so this should say thank you some person, for answering all X
35
00:03:08,900 --> 00:03:09,950
amount of questions.
36
00:03:13,050 --> 00:03:14,460
Let's recompile the code.
37
00:03:22,400 --> 00:03:23,540
And runit.
38
00:03:25,040 --> 00:03:26,010
What is your name?
39
00:03:26,060 --> 00:03:31,200
Sam will say a dollar fifty four dollars and fifty cents.
40
00:03:31,760 --> 00:03:33,410
Five and seven.
41
00:03:34,250 --> 00:03:37,550
And check it out, the application responds.
42
00:03:37,560 --> 00:03:42,800
It refers to the user by name and thanks them for answering all five questions.
43
00:03:44,450 --> 00:03:50,060
Now, if you look at your terminal, the response and the last input are a bit squished, it looks kind
44
00:03:50,060 --> 00:03:50,690
of awkward.
45
00:03:53,010 --> 00:03:59,760
There there's a special symbol, again, that creates a new line of space, the backslash is an escape
46
00:03:59,760 --> 00:04:00,380
character.
47
00:04:00,960 --> 00:04:09,270
It tells Jarvis Stop the next character special and the character that follows is and where and again
48
00:04:09,270 --> 00:04:10,440
creates a new line.
49
00:04:13,880 --> 00:04:17,269
So in your code, we're going to add again at the beginning of our response.
50
00:04:23,750 --> 00:04:25,820
All right, let's run our code.
51
00:04:38,600 --> 00:04:44,980
And check it out, it does create a new line of space for us, Jarvis sees the slash in Java, knows
52
00:04:44,990 --> 00:04:49,010
Slash is an escape character, sits there and creates a new line of space.
53
00:04:49,550 --> 00:04:53,150
And after the new line, it prints the rest of the string as normal.
54
00:05:11,610 --> 00:05:15,690
All right, we have that figured out now the app is going to tell the user how much more money they
55
00:05:15,690 --> 00:05:17,540
spend on fast food over coffee.
56
00:05:18,270 --> 00:05:22,410
So we'll say system dot out, dot print line.
57
00:05:23,220 --> 00:05:26,280
Your fast food expenses are.
58
00:05:30,040 --> 00:05:35,950
Break the string and here we'll need to divide the food price by the coffee price.
59
00:05:38,840 --> 00:05:44,090
And we're going to blend the result of this operation as part of this long string, let's reconnect
60
00:05:44,090 --> 00:05:48,730
another string that says times more than your coffee expenses.
61
00:05:53,120 --> 00:05:54,770
When I recompile my code.
62
00:06:08,040 --> 00:06:12,000
Your fast food expenses are three times more than your coffee expenses.
63
00:06:12,360 --> 00:06:16,710
Notice that when decimals are involved in the operation, you get back a decimal.
64
00:06:18,030 --> 00:06:22,170
All right, now we're going to tell the user how much they spend on coffee and food in a week, so we
65
00:06:22,170 --> 00:06:25,070
need to multiply the amount and price for food and coffee.
66
00:06:27,630 --> 00:06:31,350
System dart out the front line.
67
00:06:32,390 --> 00:06:33,120
Weekly.
68
00:06:33,260 --> 00:06:34,520
You spend.
69
00:06:36,600 --> 00:06:37,710
In dollars.
70
00:06:40,970 --> 00:06:47,720
Break the string and we're going to blend in the result of multiplying coffee amount by coffee price.
71
00:06:49,050 --> 00:06:51,900
Then we'll reconnect the string to say on coffee.
72
00:06:56,240 --> 00:07:00,260
We'll do the exact same thing for food system out front line.
73
00:07:03,710 --> 00:07:12,080
Weekly you spend in dollars will blend in the result of multiplying the food amount by the food price.
74
00:07:21,990 --> 00:07:25,050
And will conclude the string to say on food.
75
00:07:27,610 --> 00:07:33,550
All right, the recompiling the code and running it, I'm going to fast forward.
76
00:07:38,660 --> 00:07:39,960
And this looks good.
77
00:07:40,220 --> 00:07:44,690
Once again, the result is a decimal because the price values are of type double.
78
00:07:51,450 --> 00:07:53,280
Have another look at the final output.
79
00:07:56,250 --> 00:08:03,180
You'll notice that an empty line precedes each question, so we're going to add a slash and before the
80
00:08:03,180 --> 00:08:04,290
start of each question.
81
00:08:05,890 --> 00:08:06,640
And here.
82
00:08:16,840 --> 00:08:17,830
And here.
83
00:08:32,850 --> 00:08:34,559
All right, rerun your app.
84
00:08:39,610 --> 00:08:43,059
And notice now that the output looks a lot cleaner.
85
00:08:52,860 --> 00:08:59,070
Congratulations, you built your first dynamic application in Java, the application starts by asking
86
00:08:59,070 --> 00:09:04,350
these are questions and using scanner, it picks up each answer and stores it in a variable.
87
00:09:17,230 --> 00:09:21,520
And now that we have every value stored in a variable, the application can respond.
88
00:09:22,820 --> 00:09:28,580
As a final result, you built a fully interactive application where the user communicates with the app
89
00:09:28,580 --> 00:09:30,680
and the app communicates back.
90
00:09:32,110 --> 00:09:37,390
And that's all for your first dynamic up there is much more to come in this course, and I hope you're
91
00:09:37,390 --> 00:09:37,960
excited.
8596
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.