aspose file tools*
The moose likes Beginning Java and the fly likes communication between objects (use of static int) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "communication between objects (use of static int)" Watch "communication between objects (use of static int)" New topic
Author

communication between objects (use of static int)

dim mid
Greenhorn

Joined: Jan 10, 2006
Posts: 7
Greetings everybody! I'm stuglling with this little thing on a Java programme.

I have the following class

I want to have two new Dice objects that can communicate and not roll two consecutive sixes! Can anyone show me how to do that?

Thanks in advance
d.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

I want to have two new Dice objects that can communicate and not roll two consecutive sixes! Can anyone show me how to do that?


There is no reason to have dice communicate with each other. Just have the roller (object that call the roll() method) reject a second six (try again), if the first roll is also a six.

BTW, I don't think your dice is "fair". I think that certain numbers have a greater chance of appearing that others.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here are some things to think about...

One basic approach would be to roll a die, then roll a second die, and if both results are 6, then keep re-rolling the second die until you get something other than 6.

But rather than having Dice objects that "know" about each other, you might consider some object that controls the dice. Maybe call this a DiceRoller, and build it to handle any number of dice. This would make it easy to store the results within scope of each other (so you don't need to send messages between objects).

Also, I would ask whether you really need separate Dice objects. What's the difference between:
  • Creating two objects and calling roll on each of these, and...
  • Creating one object and calling roll twice?
  • For that matter, do you really need these to be objects? Or would it suffice to have a static method that just returns an int?


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    I see Henry beat me to the main point.

    But something else occurred to me: This is probably homework, in which case you need to stay within the parameters of the assignment. And the topic of this thread implies that the assignment is to create multiple objects and perhaps use a static int to enable their "communication." If that's the case, consider this...

    If a variable is static, it's associated with the class rather than any particular instance. Regardless of how many objects you create of that class, there will only be one of those variables. So, what if you have a Dice class with a static variable called lastNumberRolled, and each time a die rolls, it checks that variable to see what the last roll was...? Does that give you any ideas?
    dim mid
    Greenhorn

    Joined: Jan 10, 2006
    Posts: 7
    thanks for the quick reply! i should have mentioned that this is a part of an assignment! so there is nothing that I want to do personally ! It's just one of the questions that the tutor gave us and I'm stuck! The first one was to derive a class from dice that will not roll two consecutive sixes and the second one was to derive a class from dice that will roll only 50 sixes and 110 of the other values in 600 rolls, and last I have to make a change to the class Dice (the one that i written on my 1st post) so that two separate new derived objects can communicate and not roll consecutive sixes. And as a hint he mentions "static int"
    dim mid
    Greenhorn

    Joined: Jan 10, 2006
    Posts: 7
    that's funny we were writing the same time! thanks a lot
    dim mid
    Greenhorn

    Joined: Jan 10, 2006
    Posts: 7
    ok, i need more help i guess, i finished the other bit with the two sixes but I'm still stuck with the problem that I mentioned here with the two dice objects that can communicate!
    My code for non consecutive values of 6 is the below


    I guess it has to be similar with the above code cause it uses a static variable! but i just can't get my head when two objects are created! What are the possible ways of knowing when more than one objects is created (i guess a counter++ inside the class) and how can they tell each other that I got a six and you cannot! Please help me! Thanks a milion

    D.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    I'm not seeing the reason to subclass Dice. Basically, I'm thinking of defining a Dice class with a static variable that keeps track of the last number rolled. Because it's static, this variable would reflect the last number rolled by any Dice object.

    So one way to implement this is whenever a Dice object rolls a 6, it checks the lastNumberRolled variable to see if that's also 6. If so, it simply generates another number. (And when it settles on a number to return, it also updates the lastNumberRolled variable.)

    This assumes that whatever controls the rolling is alternating Dice objects, so that lastNumberRolled is, in fact, the last number rolled by the other instance of Dice.
    dim mid
    Greenhorn

    Joined: Jan 10, 2006
    Posts: 7
    so I need to have an if clause that checks the roll() and if it's equal to 6 we add to the static variable lastNumberRolled the value of 1 and so we know that the last roll was six. and we simply roll the dice again? right? one more question does all this has to be written inside the Dice class or I have to put something into the roll() class. Is the following code a correct start?? THANKS A LOT
    oh one more thing! in my main class I create the objects and then I just call the roll() method from the Dice class is this ok?

    [ January 12, 2006: Message edited by: dim mid ]
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    so I need to have an if clause that checks the roll() and if it's equal to 6 we add to the static variable lastNumberRolled the value of 1 and so we know that the last roll was six. and we simply roll the dice again? right?

    Basically, that's the idea. If the last roll was a '6' you keep rolling until you get something that's not a '6'. Consider that you might roll several 6's in a row. So instead of an "if" (which executes once at the most) you might consider a "while" (which executes as many times as needed). This isn't the most efficient way, but it will work.

    one more question does all this has to be written inside the Dice class or I have to put something into the roll() class.

    The Dice class contains a roll method. This is part of the behavior we want to implement in roll. In other words, include this in the roll method.

    in my main class I create the objects and then I just call the roll() method from the Dice class is this ok?

    In your main class, you create the objects (2 instances of Dice), and then you call the roll method on those instances. Something like...

    Dice dieOne = new Dice();
    Dice dieTwo = new Dice();
    dieOne.roll();
    dieTwo.roll();

    Note: I don't think you want to set lastNumberRolled to 1 until you are actually returning a '6'. (Also, if you're just using ones and zeros for true and false, you should make this a boolean type instead.)
    dim mid
    Greenhorn

    Joined: Jan 10, 2006
    Posts: 7
    This is my main class, d1 and d2 are the new objects that I want to communicate each other! I just have to spend a bit more time on it! You say that this "isn't effiecient way but it will work", I'm thinking even if we rolled a 6 and the last rolled was a six then if we roll again there might be a posibility to have a six again! but anyway i guess i have to "run" my mind a bit more so it could "spin" faster and work better thanks a lot
    [ January 13, 2006: Message edited by: dim mid ]
    dim mid
    Greenhorn

    Joined: Jan 10, 2006
    Posts: 7

    Is this correct? cheers *oh when i used a while {} loop it always returned a zero!
    [ January 12, 2006: Message edited by: dim mid ]
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by dim mid:
    ... You say that this "isn't effiecient way but it will work", I'm thinking even if we rolled a 6 and the last rolled was a six then if we roll again there might be a posibility to have a six again! ...

    If the last number rolled was a 6, you could simply generate a number from 1-5 instead of 1-6.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by dim mid:
    ... Is this correct? ...

    How are you testing it? Maybe something like this would convince you...

    Now, as Henry pointed out above, you don't have "fair" dice here (even if you remove the constraint of no consecutive 6's). I'm curious why you're calculating your roll number like that?

    Also, when I suggested a variable name of "lastNumberRolled," I thought it would hold the last number rolled -- an int between 1 and 6. But since it's a boolean, a more descriptive name might be better, like "lastRollSix" or something.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: communication between objects (use of static int)