Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Basic Dice Problem - I'm confused :(

    Hello,

    Using the Die class, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.

    Okay, so from my understanding I need 3 classes/files then, right?

    public class Die
    {
    private final int MAX = 6; // maximum face value

    private int faceValue; // current value showing on the die


    public Die()
    {
    faceValue = 1;
    }


    public int roll()
    {
    faceValue = (int)(Math.random() * MAX) + 1;

    return faceValue;
    }


    public void setFaceValue (int value)
    {
    faceValue = value;
    }


    public int getFaceValue()
    {
    return faceValue;
    }


    public String toString()
    {
    String result = Integer.toString(faceValue);

    return result;
    }
    }

    THEN
    public class PairOfDice
    {

    Die die1 = new Die();
    Die die2 = new Die();
    int value1 = 1;
    int value2 = 1;
    int total = value1 + value2;


    //Method to roll both dice and return the combined result.
    public int roll()
    {
    value1 = die1.roll();
    value2 = die2.roll();
    total = value1 + value2;
    return total;

    }

    //Method to returns the current combined dice total.
    public int getTotal()
    {
    return total;
    }

    //Method to returns the current value of the first die.
    public int getDie1()
    {
    return value1;
    }

    //Method to returns the current value of the second die.
    public int getDie2()
    {
    return value2;
    }

    }

    FINALLY
    public class RollingDice2
    {

    public static void main (String[] args)
    {

    PairOfDice dice = new PairOfDice();

    System.out.println(dice);

    }
    }


    .. and now when I look back at it, I'm just lost. Where did I go wrong and what should I do? I really appreciate comments/help!

    Thanks
    Last edited by hypertone; 02-08-2011 at 03:16 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Nope, this is quite good. The only thing your missing is actually doing something with the pair of die (you can't print it out quite like that either).

    In your main method, you simply need a dice.roll(); call. That's it, that will roll your die.

    Simplify your code in your paired dice class - you don't need to track the totals ever:
    PHP Code:
    public class PairOfDice
    {

    Die 
    die1 = new Die();
    Die 
    die2 = new Die();

    //Method to roll both dice and return the combined result.
    public int roll()
    {
    return (
    die1.roll() + die2.roll());
    }

    //Method to returns the current combined dice total.
    public int getTotal()
    {
    return 
    die1.getFaceValue() + die2.getFaceValue();
    }

    //Method to returns the current value of the first die.
    public int getDie1()
    {
    return 
    die1.getFaceValue();
    }

    //Method to returns the current value of the second die.
    public int getDie2()
    {
    return 
    die2.getFaceValue();
    }


    As you can see above, try to chain as many things together as you can instead of tracking things individually - centralization is a key point to OOP; you only want to manage this data at one level if possible.

    Now when you call dice.roll() in main, it will return an integer with the new die totals which you can capture and print out if you like. If you want to use System.out.println or any type of string evaluation on a class, you need to override the toString method on object in order to have it change the looks (otherwise you tend to get just a simple identifier for the object [myobject@234a33 for example]).

    For a bonus tip, always override the toString. Always. Write it in a way that makes sense to display (I'll use the stringBuffer class to write, though standard appending works just fine as well):
    PHP Code:
    public String toString()
    {
        
    StringBuffer sResult = new StringBuffer("Die[");
        
    sResult.append("faceValue=" this.getFaceValue());
        
    sResult.append("]");

        return 
    sResult.toString();
    }

    // And I'll add one in pair as well:
    public String toString()
    {
        
    StringBuffer sResult = new StringBuffer("PairOfDice[");
        
    sResult.append("die1=" this.die1.toString()); // .toString should be optional, just a habit I've gotten into much like using this. when not necessary.
        
    sResult.append(", die2=" this.die2.toString()); 
        
    sResult.append("]");

        return 
    sResult.toString();

    I suggest this since now you can use a System.out.println(dice), and it will show the contents and chain it to the die1 and die2 toString calls to display something much like PairOfDice[die1=Die[faceValue=1], die2=Die[faceValue=4]]. This is exceedingly helpful with debugging. If you have a particular format you'd like to output, I'd recommend not using the toString to do so if you can avoid it, and choosing instead to call a particular method to do the formatting.

    These are just tips to help you out, in particular the pairofdice class doesn't actually need to change as it appears to be functional to me. I assumed that at this level you are not dealing quite with arrays or exceptions or anything like that (in which case I'd suggest creating a Dice class instead of pairOfdice, and pushing as many die as you like onto it).

    Good luck, post back with any other questions you may have. Also, in the future use the [code][/code] or [php][/php] tags instead of [icode][/icode] in order to preserve the format of your code.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    hypertone (02-09-2011)

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hey thanks a lot for your help Fou-Lu! I am required to write this program a certain way so some of the suggestions you've mentioned are either too advanced for me or I'm not allowed to use them.

    To be quite honest, I've having a hard time understanding your toString method and explanation. Do I need to create another toString method in the main driver? I cannot change my toString method because I initially declared it as INT. Also, I don't need to output anything fancy, just what the dice rolls.

    PHP Code:
    public class Die
    {
       private final 
    int MAX 6;  // maximum face value

       
    private int faceValue;  // current value showing on the die

       //-----------------------------------------------------------------
       //  Constructor: Sets the initial face value.
       //-----------------------------------------------------------------
       
    public Die()
       {
          
    faceValue 1;
       }

       
    //-----------------------------------------------------------------
       //  Rolls the die and returns the result.
       //-----------------------------------------------------------------
       
    public int roll()
       {
          
    faceValue = (int)(Math.random() * MAX) + 1;

          return 
    faceValue;
       }

       
    //-----------------------------------------------------------------
       //  Face value mutator.
       //-----------------------------------------------------------------
       
    public void setFaceValue (int value)
       {
          
    faceValue value;
       }

       
    //-----------------------------------------------------------------
       //  Face value accessor.
       //-----------------------------------------------------------------
       
    public int getFaceValue()
       {
          return 
    faceValue;
       }

       
    //-----------------------------------------------------------------
       //  Returns a string representation of this die.
       //-----------------------------------------------------------------
       
    public String toString()
       {
          
    String result Integer.toString(faceValue);

          return 
    result;
       }

    PHP Code:
    public class PairOfDice
    {

        Die 
    die1 = new Die();
        Die 
    die2 = new Die();
        
        private 
    int faceValue;

        
    //Method to roll first dice and return the result
        
    public int rollOne()
        {
        return 
    die1.roll();
        }
        
        
    //Method to roll second dice and return the result
        
    public int rollTwo()
        {
        return 
    die2.roll();
        }
        
        
    //Method to return the current combined dice total
        
    public int getTotal()
        {
            return 
    die1.getFaceValue() + die2.getFaceValue();
        }
        
        
    //Method to set the current value of the first die
        
    public void setDie1(int number)
        {
            
    faceValue number;
        }
        
        
    //Method to return the current value of the first die
        
    public int getDie1()
        {
            return 
    die1.getFaceValue();
        }    
        
        
    //Method to set the current value of the first die
        
    public void setDie2(int number)
        {
            
    faceValue number;
        }

        
        
    //Method to return the current value of the second die
        
    public int getDie2()
        {
            return 
    die2.getFaceValue();
        }    

    PHP Code:
    public class RollingDice2
    {

        public static 
    void main (String[] args
        {

            
    PairOfDice firstDice = new PairOfDice();
            
    PairOfDice secondDice = new PairOfDice();
            
            
    firstDice.setDie1(2);
            
    firstDice.getDie1();
            
            
    System.out.println(firstDice);
            


            
    secondDice.setDie2(4);
            
    secondDice.getDie2();
            
            
    System.out.println(secondDice);

        }

    As you predicted, my output is such as:
    PairOfDice@164f1d0d
    PairOfDice@23fc4bec

    Thanks again for any comments/hints/help!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, toString belongs in whichever class you want to represent as a string, such as the pair of dice. toString cannot return an int and override the initial toString itself, and I don't think that Java is satisfied with an overloaded toString.

    Your PairOfDice can get away with a simple one:
    PHP Code:
    public String toString()
    {
        return 
    "" getTotal(); // Or cast it; this should work in Java

    0
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    hypertone (02-09-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •