Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:03,210
This project is going to test everything that you learn so far.
2
00:00:04,540 --> 00:00:10,330
At this point, you know how to organize your code into functions, define functions that take parameters,
3
00:00:10,570 --> 00:00:15,130
call functions and pass in arguments and return values from your function.
4
00:00:15,460 --> 00:00:19,630
The DICE project is going to cover all of these concepts, but there is a twist.
5
00:00:19,780 --> 00:00:23,930
Like every project, you're going to add interactivity to your application using scanner.
6
00:00:23,950 --> 00:00:25,750
So let's get right into it.
7
00:00:28,210 --> 00:00:33,000
First thing I'll need you to do is create a new class by yourself inside the Section four project,
8
00:00:33,010 --> 00:00:39,580
create a new file named Dice Jack, and in the Dice Jack class, create the main method.
9
00:00:45,160 --> 00:00:47,350
Now Dice Jack is a simple game.
10
00:00:47,350 --> 00:00:49,210
The user can pick three numbers.
11
00:00:49,210 --> 00:00:55,110
For example, I could pick the numbers four, three and five, and if you add them up, the sum is 12.
12
00:00:55,120 --> 00:00:57,730
Then they need to roll the dice three times.
13
00:00:57,730 --> 00:00:59,860
Let's assume they roll three, four and four.
14
00:00:59,890 --> 00:01:02,140
The sum of those numbers is 11.
15
00:01:03,650 --> 00:01:09,260
Now, I would win the game if the sum of my dice rolls is smaller than the sum of numbers that I choose.
16
00:01:10,430 --> 00:01:15,890
And if the difference between the two numbers is less than three, in this case, I would win because
17
00:01:15,890 --> 00:01:19,460
the sum of my dice rolls is smaller than the sum of my numbers.
18
00:01:20,430 --> 00:01:23,250
And the difference between the two numbers is less than three.
19
00:01:25,430 --> 00:01:28,150
So we're going to write code that performs two tasks.
20
00:01:28,160 --> 00:01:33,620
The first task is to roll the dice, and the second task is to check if the user one.
21
00:01:35,310 --> 00:01:37,260
Two tasks imply two functions.
22
00:01:37,260 --> 00:01:39,330
So let's get started now.
23
00:01:39,330 --> 00:01:42,000
Honestly, most of these tasks sound really easy.
24
00:01:42,000 --> 00:01:46,410
It's nothing we haven't done before, but this one is going to require some strategizing.
25
00:01:48,750 --> 00:01:50,970
Java provides a math function called the math.
26
00:01:51,180 --> 00:01:55,860
Random math at random returns a decimal between zero and less than one.
27
00:01:55,860 --> 00:01:58,110
So 0.99999.
28
00:02:00,660 --> 00:02:02,550
The lowest extreme is zero.
29
00:02:03,560 --> 00:02:09,590
The maximum value it can return is 0.9999 less than one, but extremely close.
30
00:02:09,860 --> 00:02:11,570
Those are the two extremes.
31
00:02:12,730 --> 00:02:15,850
Matt the random will return a value between this range.
32
00:02:16,000 --> 00:02:22,300
Whenever you call maths random, the decimal that it returns is bound between zero and 0.99.
33
00:02:31,240 --> 00:02:35,530
But what can we do in order to return a decimal between zero and less than six?
34
00:02:35,530 --> 00:02:38,320
Pause the video and really think about your answer.
35
00:02:43,280 --> 00:02:46,400
The correct answer is multiply its result by six.
36
00:02:47,400 --> 00:02:52,230
Think about it, the minimum value is zero zero times six is still zero.
37
00:02:52,530 --> 00:02:57,140
The maximum value that can be returned is less than 1.999.
38
00:02:57,150 --> 00:03:01,740
Multiplied by six is 5.999 less than six.
39
00:03:01,740 --> 00:03:04,480
Now the range becomes zero to less than six.
40
00:03:04,500 --> 00:03:06,350
These are the two extremes.
41
00:03:06,360 --> 00:03:11,790
Our code will always return a decimal that is bound between zero and less than six.
42
00:03:34,650 --> 00:03:40,050
All right, Now, what can we do to ensure that our current code returns a decimal between one and less
43
00:03:40,050 --> 00:03:40,830
than seven?
44
00:03:40,950 --> 00:03:43,230
Pause the video and think about your answer.
45
00:03:48,460 --> 00:03:50,590
The correct answer is to add one.
46
00:03:52,750 --> 00:03:57,790
The minimum value from our current code is zero, and then zero plus one equals one.
47
00:03:59,120 --> 00:04:04,620
The maximum value that our code can currently return is less than 65.999.
48
00:04:04,790 --> 00:04:08,260
That plus one is going to equal 6.999.
49
00:04:09,080 --> 00:04:12,410
Now the range becomes one to less than seven.
50
00:04:12,440 --> 00:04:14,270
Those are the two extremes.
51
00:04:14,270 --> 00:04:19,550
And our code will always return a decimal that is bound between one and less than seven.
52
00:04:44,430 --> 00:04:46,520
Now here comes the cherry on top.
53
00:04:46,530 --> 00:04:49,740
The minimum value that our current code returns is one.
54
00:04:49,740 --> 00:04:54,000
If we typecast the minimum to an integer, it's still one.
55
00:04:54,270 --> 00:04:59,400
The maximum value that our current code can return is 6.99999.
56
00:04:59,400 --> 00:05:05,460
And if we typecast the maximum to an integer, we just cut off all the decimals and the maximum value
57
00:05:05,460 --> 00:05:07,590
that our code can produce is six.
58
00:05:08,530 --> 00:05:10,610
So now their range becomes 1 to 6.
59
00:05:10,630 --> 00:05:12,170
Those are the two extremes.
60
00:05:12,190 --> 00:05:18,190
Our updated code will always return a whole number that is bound between one and six.
61
00:05:19,140 --> 00:05:23,310
And just like that, we have code that can be used to roll dice.
62
00:05:55,690 --> 00:05:56,230
All right.
63
00:05:56,230 --> 00:05:58,690
It is now time to code everything up.
64
00:06:01,290 --> 00:06:02,760
All right, let's begin.
65
00:06:02,760 --> 00:06:06,660
Our function is going to perform one task, which is rolling dice.
66
00:06:06,660 --> 00:06:11,250
So we expect it to return an integer that represents the dice that was rolled.
67
00:06:11,280 --> 00:06:13,710
Of course, it's going to be called roll dice.
68
00:06:13,710 --> 00:06:19,080
And the roll dice function is not going to need any parameters to perform its task because everything
69
00:06:19,080 --> 00:06:20,180
will be random.
70
00:06:20,190 --> 00:06:23,040
Here we can say double random number.
71
00:06:24,410 --> 00:06:26,270
Is equal to math.
72
00:06:26,690 --> 00:06:27,440
Random.
73
00:06:28,930 --> 00:06:30,010
Times six.
74
00:06:31,340 --> 00:06:32,930
So math random.
75
00:06:32,930 --> 00:06:39,410
What it's going to do is return a decimal that is greater than or equal to zero and less than one.
76
00:06:39,410 --> 00:06:45,500
So it's going to return a decimal that is bound between zero.
77
00:06:46,950 --> 00:06:50,070
And 0.9999999.
78
00:06:52,000 --> 00:06:54,790
And whatever decimal that it ends up returning.
79
00:06:54,790 --> 00:07:02,570
Once you multiply that by six, then that decimal is going to be bound between zero and less than 65.999.
80
00:07:02,950 --> 00:07:03,940
All right.
81
00:07:04,180 --> 00:07:07,540
And now here we can say random number plus equals one.
82
00:07:07,540 --> 00:07:11,170
So whatever a decimal gets returned from this line.
83
00:07:11,170 --> 00:07:16,960
If we were to add one, then that number can be anything from one to less than seven.
84
00:07:19,670 --> 00:07:26,660
And if we were to return the type casted result, return whatever the random number is, but convert
85
00:07:26,660 --> 00:07:33,950
it to an integer, then that number can be anything between 1 to 6 and the result that gets returned
86
00:07:33,950 --> 00:07:37,730
will always be a whole number that is bound between one and six.
87
00:07:37,760 --> 00:07:43,160
Now we can test it out by calling the function three different times and setting up some breakpoints.
88
00:07:43,160 --> 00:07:46,910
So a roll, dice roll dice, roll, dice and roll.
89
00:07:46,910 --> 00:07:49,160
One is equal to roll dice.
90
00:07:52,650 --> 00:07:54,450
We'll call this a roll to.
91
00:07:55,280 --> 00:07:57,170
Then we'll call this or all three.
92
00:08:00,020 --> 00:08:02,390
And we're just going to print each roll.
93
00:08:02,420 --> 00:08:03,590
All right.
94
00:08:04,910 --> 00:08:06,280
Print the third dice roll.
95
00:08:06,290 --> 00:08:07,910
Let us visualize the runtime.
96
00:08:11,670 --> 00:08:13,650
I will step inside of roll dice.
97
00:08:15,250 --> 00:08:20,560
So the result here is always going to be bound between zero and less than six.
98
00:08:20,740 --> 00:08:23,500
In this case, we'll step over this line.
99
00:08:23,800 --> 00:08:26,410
What gets returned is 1.93.
100
00:08:27,600 --> 00:08:32,659
And the result here is always going to be bound between one and less than seven.
101
00:08:32,669 --> 00:08:38,850
In this case, whatever we got from the first line plus one ends up being 2.9.
102
00:08:39,299 --> 00:08:41,850
Typecasting this to an integer.
103
00:08:41,850 --> 00:08:47,730
We get a dice roll of two and that return value gets stored in the variable role.
104
00:08:47,730 --> 00:08:50,580
One Continuing to the next breakpoint.
105
00:08:50,580 --> 00:08:52,920
Here we roll the dice a second time.
106
00:08:52,920 --> 00:08:58,680
The result here is always going to be a decimal that is bound between zero and less than six.
107
00:08:58,680 --> 00:09:02,100
In this case, we get a random number of.
108
00:09:03,350 --> 00:09:05,090
0.28.
109
00:09:06,010 --> 00:09:11,710
The result here will always be a decimal that is bound between one and less than seven.
110
00:09:12,040 --> 00:09:16,930
In this case, whatever we got in line number 15 plus one.
111
00:09:18,370 --> 00:09:19,870
Is 1.28.
112
00:09:20,470 --> 00:09:25,240
The result here will always be a whole number between one and six, because all we're doing is cutting
113
00:09:25,240 --> 00:09:28,690
off the decimals and we get a dice roll of one.
114
00:09:28,690 --> 00:09:33,190
And here we're storing the return value inside of a variable called roll two.
115
00:09:34,600 --> 00:09:37,270
So now we've got two dice rolls here.
116
00:09:37,270 --> 00:09:40,900
We're rolling the dice for a third time, stepping inside the function.
117
00:09:43,030 --> 00:09:48,190
The result here will always be a decimal that is bound between zero and less than six.
118
00:09:48,220 --> 00:09:51,730
In this case, we get 2.30.
119
00:09:52,600 --> 00:09:57,650
The result here will always be a decimal that is bound between one and less than seven.
120
00:09:57,670 --> 00:10:02,020
In this case, whatever we got in line number 15 plus one.
121
00:10:02,260 --> 00:10:08,470
And after typecasting, whatever we get from line 16, the result here is always going to be a whole
122
00:10:08,470 --> 00:10:10,740
number between one and six.
123
00:10:10,750 --> 00:10:17,590
In this case, we're going to get a dice roll of three, and then we store that return value in a variable
124
00:10:17,590 --> 00:10:18,910
called row three.
125
00:10:20,510 --> 00:10:21,410
And then, you know what?
126
00:10:21,410 --> 00:10:27,710
Let's just let execution resume will press continue and then it's going to print all of our dice rolls
127
00:10:27,710 --> 00:10:29,210
to one and three.
128
00:10:30,130 --> 00:10:36,880
So now what I'll actually do is I'll say Java Sea Dice Jack, Java, Java, Dice Jack.
129
00:10:37,570 --> 00:10:42,760
And I'm just going to keep running this code so I can get a variety of dice rolls.
130
00:10:42,790 --> 00:10:47,200
Notice that I keep getting values between one and six.
131
00:10:48,740 --> 00:10:54,950
So if you really want to test out your code, keep running it and make sure that your minimum is one
132
00:10:55,010 --> 00:10:57,350
and your maximum is six.
12438
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.