|
I am just starting to attempt to learn java. I want to think of classes is independent little bits of code that do their job and then either deliver some bit of information to whoever called them or don’t. Either way they do their business and them move along.
Encapsulation., that’s what I am looking for here
import java.util.Random;
public class test11{
public static void main(String[] args){
die rrr = new die(10);
System.out.println(rrr);
}
}
class dice {
int die(int sides){
Random roll = new Random();
int ddd = roll.nextInt(sides)+1;
return ddd;
}
}
I want dice to return a random number based on the number of sides I send to the class. But all I get is some sort of id number, die@119c082. Moreover I want to be able to see how to do the work in a separate class and just call that class for the value or info it can give me and get that info back.
|
|
|
Well oddly enough I made it work but I don’t entirely understand how.
Finished product
import java.util.Random;
public class test11{
public static void main(String[] args){
int rrr = dice.die(10) ;
System.out.println(rrr);
}
}
class dice {
static int die(int sides){
Random roll = new Random();
int ddd = roll.nextInt(sides)+1;
return ddd;
}
}
I have to have dice.die(10). die(10) wont work. This is certainly easier than the whole new instance red herring I was on.
|
|
|
For more information about this area see the lesson Creating Classes and esp. Understanding Instance and Class Members.
import java.util.Random;
public class Access
{
public static void main(String[] args)
{
int sides = 10;
// access static method in Dice class
System.out.println("static access: " + Dice.roll(sides));
// create an instance of Dice and save a reference to it
// in the variable "dice"
Dice dice = new Dice();
// use the instance variable to call the "rollDie" method in Dice
int reps = 5;
for(int j = 0; j < reps; j++)
{
System.out.print(dice.rollDie(sides));
if(j < reps-1)
System.out.print(", ");
else
System.out.println();
}
// try it without saving a reference to the new instance of Dice
System.out.println("on the fly: " + new Dice().rollDie(10));
// we can also access a static method with an instance variable
// ie, an instance of a class can access a class/static
// method of that class
System.out.println("static method via instance: " + dice.roll(10));
// for this to work you must have a class named "die" with a
// constructor that takes an int argument lying around
// somewhere, the hex nummber is the object address
System.out.println("location of this object: " + new die(10));
// we can do this with the Dice class, too
// when you learn about using the "toString" method you can get
// some more meaningful information than just the address
System.out.println(new Dice());
}
}
class Dice
{
/**
* a static/class variable
* every/all instance/s of this class has/have access to this
* this is the only instance of this variable for
* (and it is available to) all instances of this class
*/
static Random seed = new Random();
/**
* an instance method
* you access this method with an instance (variable) of this class
* Dice die = new Dice(); // "die" is the instance variable
* die.rollDie(intValue);
*/
public int rollDie(int sides)
{
return 1 + seed.nextInt(sides);
}
/**
* a static method
* you access this method with static notation using the class name
* Dice.roll(intValue)
*/
public static int roll(int sides)
{
return 1 + seed.nextInt(sides);
}
}
class die
{
int i;
die(int v) { i = v; }
}
|
|
|
|
|
|
|
|
|
// |