@3man75, to learn how to adapt the code to a class, you should probably remove all the user-interaction, simplify it in main() as much as possible, then you can see how making objects is just an extension of the thinking involved in making functions. I'm going to write this in C++ style for brevity, but you can adjust it for Java style:
Let me show some steps to "evolve" a class from the bare code. The "code" here is only two lines. 1 to initialize the Random object, and another to store a random number from 1 to 6 in a variable called "result".
void main()
{
Random rng = new Random();
int result = rng.nextInt(6) + 1;
}
The first thing you can do is package this in a function so that it can be re-used:
int Roll()
{
Random rng = new Random();
return rng.nextInt(6) + 1;
}
void main()
{
int result = Roll();
}
The next level is to have a function parameter for the "sides":
int Roll(int sides)
{
Random rng = new Random();
return rng.nextInt(sides) + 1;
}
void main()
{
int result = Roll(6); // pass value "6" as the sides of the dice for the roll
}
To turn this into a class you just wrap a class block around the function:
class Dice
{
int Roll(int sides) // this is the same function as above, we've just packaged it in an object called "Dice"
{
Random rng = new Random();
return rng.nextInt(sides) + 1;
}
}
void main()
{
Dice dice; // make a dice so we can call it
int result = dice.Roll(6); // still passing 6 as the "sides" each time we roll
}
The last step is where we start actually using some OO specifics. Since objects can remember things, we can make a variant of the Dice object that remembers how many sides it has:
class Dice
{
int sides; // "data slot" to remember how many sides there are
Dice(int _sides) // constructor, which takes a temp value called "_sides"
{
sides = _sides; // copy the temp value to the permanent one
}
int Roll() // we don't need to pass a "sides" value any more, because a Dice object remembers how many sides it has
{
Random rng = new Random();
return rng.nextInt(sides) + 1;
}
}
void main()
{
Dice dice(6); // make a dice with 6 sides
int result = dice.Roll(); // roll our six-sided dice
}
Finally, we can notice that we're making a "new Random()" every time we roll the dice. This is not good, because a random object can be called over and over. We can make the variable "Random rng" be part of the class itself, and only initialize it when we create our Dice object:
class Dice
{
int sides; // "data slot" to remember how many sides there are
Random rng; // our random number generator is stored for this class
Dice(int _sides) // constructor, which takes a temp value called "_sides"
{
sides = _sides; // copy the temp value to the permanent one
rng = new Random(); // set up the rng only when the class is created
}
int Roll() // we don't need to pass a "sides" value any more, because a Dice object remembers how many sides it has
{
// we don't need to create a new rng here anymore each time we Roll
return rng.nextInt(sides) + 1;
}
}
void main()
{
Dice dice(6); // make a dice with 6 sides
int result = dice.Roll(); // roll our six-sided dice
}
You should be able to see how all these sections of code actually do the exact same thing, but they build on each other to become more modular, more general, and "object oriented".