Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
This project is going to test everything that you learn so far.
At this point, you know how to organize your code into functions, define functions that take parameters,
call functions and pass in arguments and return values from your function.
The DICE project is going to cover all of these concepts, but there is a twist.
Like every project, you're going to add interactivity to your application using scanner.
So let's get right into it.
First thing I'll need you to do is create a new class by yourself inside the Section four project,
create a new file named Dice Jack, and in the Dice Jack class, create the main method.
Now Dice Jack is a simple game.
The user can pick three numbers.
For example, I could pick the numbers four, three and five, and if you add them up, the sum is 12.
Then they need to roll the dice three times.
Let's assume they roll three, four and four.
The sum of those numbers is 11.
Now, I would win the game if the sum of my dice rolls is smaller than the sum of numbers that I choose.
And if the difference between the two numbers is less than three, in this case, I would win because
the sum of my dice rolls is smaller than the sum of my numbers.
And the difference between the two numbers is less than three.
So we're going to write code that performs two tasks.
The first task is to roll the dice, and the second task is to check if the user one.
Two tasks imply two functions.
So let's get started now.
Honestly, most of these tasks sound really easy.
It's nothing we haven't done before, but this one is going to require some strategizing.
Java provides a math function called the math.
Random math at random returns a decimal between zero and less than one.
So 0.99999.
The lowest extreme is zero.
The maximum value it can return is 0.9999 less than one, but extremely close.
Those are the two extremes.
Matt the random will return a value between this range.
Whenever you call maths random, the decimal that it returns is bound between zero and 0.99.
But what can we do in order to return a decimal between zero and less than six?
Pause the video and really think about your answer.
The correct answer is multiply its result by six.
Think about it, the minimum value is zero zero times six is still zero.
The maximum value that can be returned is less than 1.999.
Multiplied by six is 5.999 less than six.
Now the range becomes zero to less than six.
These are the two extremes.
Our code will always return a decimal that is bound between zero and less than six.
All right, Now, what can we do to ensure that our current code returns a decimal between one and less
than seven?
Pause the video and think about your answer.
The correct answer is to add one.
The minimum value from our current code is zero, and then zero plus one equals one.
The maximum value that our code can currently return is less than 65.999.
That plus one is going to equal 6.999.
Now the range becomes one to less than seven.
Those are the two extremes.
And our code will always return a decimal that is bound between one and less than seven.
Now here comes the cherry on top.
The minimum value that our current code returns is one.
If we typecast the minimum to an integer, it's still one.
The maximum value that our current code can return is 6.99999.
And if we typecast the maximum to an integer, we just cut off all the decimals and the maximum value
that our code can produce is six.
So now their range becomes 1 to 6.
Those are the two extremes.
Our updated code will always return a whole number that is bound between one and six.
And just like that, we have code that can be used to roll dice.
All right.
It is now time to code everything up.
All right, let's begin.
Our function is going to perform one task, which is rolling dice.
So we expect it to return an integer that represents the dice that was rolled.
Of course, it's going to be called roll dice.
And the roll dice function is not going to need any parameters to perform its task because everything
will be random.
Here we can say double random number.
Is equal to math.
Random.
Times six.
So math random.
What it's going to do is return a decimal that is greater than or equal to zero and less than one.
So it's going to return a decimal that is bound between zero.
And 0.9999999.
And whatever decimal that it ends up returning.
Once you multiply that by six, then that decimal is going to be bound between zero and less than 65.999.
All right.
And now here we can say random number plus equals one.
So whatever a decimal gets returned from this line.
If we were to add one, then that number can be anything from one to less than seven.
And if we were to return the type casted result, return whatever the random number is, but convert
it to an integer, then that number can be anything between 1 to 6 and the result that gets returned
will always be a whole number that is bound between one and six.
Now we can test it out by calling the function three different times and setting up some breakpoints.
So a roll, dice roll dice, roll, dice and roll.
One is equal to roll dice.
We'll call this a roll to.
Then we'll call this or all three.
And we're just going to print each roll.
All right.
Print the third dice roll.
Let us visualize the runtime.
I will step inside of roll dice.
So the result here is always going to be bound between zero and less than six.
In this case, we'll step over this line.
What gets returned is 1.93.
And the result here is always going to be bound between one and less than seven.
In this case, whatever we got from the first line plus one ends up being 2.9.
Typecasting this to an integer.
We get a dice roll of two and that return value gets stored in the variable role.
One Continuing to the next breakpoint.
Here we roll the dice a second time.
The result here is always going to be a decimal that is bound between zero and less than six.
In this case, we get a random number of.
0.28.
The result here will always be a decimal that is bound between one and less than seven.
In this case, whatever we got in line number 15 plus one.
Is 1.28.
The result here will always be a whole number between one and six, because all we're doing is cutting
off the decimals and we get a dice roll of one.
And here we're storing the return value inside of a variable called roll two.
So now we've got two dice rolls here.
We're rolling the dice for a third time, stepping inside the function.
The result here will always be a decimal that is bound between zero and less than six.
In this case, we get 2.30.
The result here will always be a decimal that is bound between one and less than seven.
In this case, whatever we got in line number 15 plus one.
And after typecasting, whatever we get from line 16, the result here is always going to be a whole
number between one and six.
In this case, we're going to get a dice roll of three, and then we store that return value in a variable
called row three.
And then, you know what?
Let's just let execution resume will press continue and then it's going to print all of our dice rolls
to one and three.
So now what I'll actually do is I'll say Java Sea Dice Jack, Java, Java, Dice Jack.
And I'm just going to keep running this code so I can get a variety of dice rolls.
Notice that I keep getting values between one and six.
So if you really want to test out your code, keep running it and make sure that your minimum is one
and your maximum is six.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.