Section 2.3 Worked Example: Create Instance of Random
Subgoals for using objects (creating instances).
- Declare variable of appropriate class datatype.
- Assign to variable: keyword new, followed by class name, followed by ().
Determine whether parameter(s) are appropriate (API)
- Number of parameters
- Data types of the parameters
You can watch this video or read through the content below it.
Subsection 2.3.1 SG1: Declare variable of appropriate class datatype.
In the code block below, the variable name
randGenwas selected to better describe this object’s purpose as a random number generator.
Subsection 2.3.2 SG2: Assign to variable: keyword new, followed by class name, followed by ().
randGen = new Random();
Subsection 2.3.3 SG3: Determine whether parameter(s) are appropriate (API)
The figure below shows the Java documentation, so we can determine what parameter(s) we need. If we wanted a specific seed value, we could use that, but for most purposes, allowing the constructor to select its own seed is ok.
You have attempted of activities on this page.