Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2011, 02:39 AM   PM User | #1
hypertone
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
hypertone is an unknown quantity at this point
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..
hypertone is offline   Reply With Quote
Old 02-08-2011, 02:29 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
hypertone (02-09-2011)
Old 02-09-2011, 12:18 AM   PM User | #3
hypertone
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
hypertone is an unknown quantity at this point
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!
hypertone is offline   Reply With Quote
Old 02-09-2011, 01:37 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
hypertone (02-09-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:28 PM.


Advertisement
Log in to turn off these ads.