Kathryn shows you a solution to the "Dice Roll" challenge. In this solution, Kathryn creates a Dice class that represents a single die and uses the random module to simulate rolling a single die.
(theatrical music)…- [Instructor] Here's the solution…to the previous dice roll challenge.…We start off by creating a dice class…to represent a single dice in code…and so here we import java.util.Random…so that we could have our die be random…and they can be a random number from one to six.…Then we have an instance of the random class…actually be a property of our dice.…And in our constructor of the dice,…we go ahead and set this.rand…equal to a new Random number generator,…an instance of the random class.…
Then we have our roll method and inside of this roll method,…we set the currentRoll and the currentRoll…is going to have the value of the random number generators…nextInt between zero and five…and then we add one to that number…so that way our currentRoll will be between one and six.…And then we go ahead and return this currentRoll…at the end of the method.…Moving over to our main class,…we have this dice that we create.…It's an instance of the dice class…and then we print out two rolls.…
So running this method…
Author
Released
3/21/2018- Downloading Java 9 and choosing an IDE
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Debugging
- Working with inheritance and interfaces
- Learning lambda
Skill Level Beginner
Duration
Views
-
Introduction
-
Welcome34s
-
-
1. Getting Started
-
Exploring JShell3m 50s
-
Exploring an IDE2m 11s
-
Hello World in Java4m 6s
-
2. Java Basics
-
How do we create code?2m 8s
-
Variables and data types4m 6s
-
Strings7m 12s
-
Arrays8m 48s
-
Exploring documentation3m 1s
-
-
3. Control Flow
-
Decision-making with IF5m 37s
-
Comparing loops5m 18s
-
Debugging with an IDE4m 16s
-
Solution: Dice Roll4m 3s
-
4. Beyond the Basics
-
What is encapsulation?6m 40s
-
Working with inheritance9m 12s
-
Interfaces6m 6s
-
Functional programming1m 4s
-
Learning lambda7m 14s
-
Conclusion
-
Next steps37s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Solution: Dice Roll