Jump to content

Java question

namarino

Hi guys! I have this little lab to do for computer science class but I'm having trouble. I admit that during the class that my teacher explained this, I wasn't paying attention, but all he went over is how to use the math class that is included in the java library so I could look that up and how to use it. I was wondering if someone could just explain to me that second paragraph. I don't quite understand how instance variables play a roll in making a random number generator. Wouldn't you just make a method that generates the number and takes input and then just reference it in the main method? I'm not looking for anyone to do it for me, I just want someone to essentially help me to understand that second paragraph and how instance variables would come into play in a case like this. Thanks guys.

post-162792-0-36123700-1416650451_thumb.

Link to comment
Share on other sites

Link to post
Share on other sites

I think this is what he wants

 

public class DiceRoller {
 
private int rolledNumber; // field that construtor will set
 
public DiceRoller(int dice, int sides) {
// make random
Random random = new Random(35);
set instance variable with method
this.rolledNumber = rollEm(dice, sides);
}
 
//method to randomize roll
//can be private since we only need the contructor to use it.
 
private int rollEm(int dice, int sides) {
// this method is where the magic happens, 
// i will leave that up to you
int total = dice * sides; // not actual way to get rolling
return total;
}

Much info 

Link to comment
Share on other sites

Link to post
Share on other sites

I think what he means is the constructor will set the field using a random number generator and a method.

 

Edit:

You will need a getter to grab the total after or make the field public(not recommended)

Much info 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×