Say that I have two classes, A and B, that are in the package Sample.
In class B, I have generated a random int x
that is either a 0 or 1. I want to print int x
in class A. What code should I use to do this?
Here is what I have in class B so far:
package Sample;
import java.util.Random;
public class B {
Random random = new Random();
public void printXfromB(){
System.out.println("What B says that 'x' is: " + getX());
}
public int getX() {
int x = random.nextInt(2); //x is either 0 or 1
return x;
}
}
And this is what I have for class A:
package Sample;
public class A {
B b = new B();
int x = b.getX(); //
public void printXfromA(){
System.out.println("What A says that 'x' is: " + x);
}
}
The problem is, class A is generating a random number that's independent of the original int x
in class B. How can I get class A to be dependent on class B so that they're printing out the same values?
Aucun commentaire:
Enregistrer un commentaire