Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Can't figure out how to roll individual dice.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Can't figure out how to roll individual dice.

    Alright so I am a beginner when it comes to java. I was working on a project in class and I eventually got stumped.
    So here is what I want to do. I need to figure out how to roll the three dice individually in the dice object.
    Here is my code for the dice class.

    public class Dice //class header
    {
    	//instance variables
    	private int die1;
    	private int die2;
    	private int die3;
     
    	/**
    	 * Default Constructor
    	 * Initialize the dice to 0's to indicate that they haven't been rolled yet
    	 * 
    	 */
    	public Dice ()
    	{
    		die1 = 0;
    		die2= 0;
    		die3 = 0;
    	}
     
    	/**
    	 * getDie1 method
    	 * Return the value stored in the instance variable die1
    	 * @return the face value of die1
    	 */
    	public int getDie1()
    	{
     
    		return die1;
    	}
     
    	/**
    	 * getDie2 method
    	 * Return the value stored in the instance variable die2
    	 * @return the face value of die2
    	 */
    	public int getDie2()
    	{
    		return die2;
    	}
     
    	/**
    	 * getDie3 method
    	 * Return the value stored in the instance variable die3
    	 * @return the face value of die3
    	 */
    	public int getDie3()
    	{
    		return die3;
    	}
     
    	/**
    	 * roll method
    	 * Each instance variable gets a random number in the range 1 to 6
    	 */
    	public void roll()
    	{
    		die1 = (int)(Math.random()*6+1);
    		die2 = (int)(Math.random()*6+1);
    		die3 = (int)(Math.random()*6+1);
    	}
     
    	/**
    	 *calcTotalRoll method - this method will calculate the total of the three die
    	 *@return the sum of the dice
    	 */
    	public int calcTotalRoll()
    	{
    		return die1 + die2 + die3;
    	}
     
    	/**
    	 *threeOfAKind method - this method will determine if all three of the dice have the same value
    	 *@return true if they have the same, false otherwise 
    	 */
    	public String threeOfAKind()
    	{
    		String aKind = new String("Three of a kind");
    		String notOfAKind = new String("Not three of a kind");
    		if ((die1==die2)&&(die2==die3)==true)
    			    return aKind;
    			else
    			    return notOfAKind;
    	}
     
    	/**
    	 * findHighestDie method - this method will determine which of the three die have the highest die
    	 * @return the value of the highest die
    	 */
    	public int findHighestDie()
    	{
    		if ((die1 > die2)&&(die1 > die3))
    		return die1;
    		else
    			if (die2 > die3)
    			return die2;
    			else
    				return die3;
    	}
     
    	public void rollOneDie()
    	{
     
     
     
     
    	}
     
    	/**
    	 * toString method
    	 * Create a String with the values of the three dice.
    	 * @return a reference to a String with the state of the object
    	 */
    	public String toString()
    	{
    		String str = new String(die1 + " " + die2 + " " + die3);
    		return str;
    	}
     
    }

    and here is my lab class

    public class Lab5App //class header
    {
    	public static void main(String[] args) //main method header
    	{
    		//Instantiated a Dice object
    		Dice theDice = new Dice();
    		// Testing the constructor and the toString
    		System.out.println("After instantiation: " + theDice.toString() + "\n");
    		//Tested the accessor method
    		System.out.println("Value of die1: " + theDice.getDie1());
    		System.out.println("Value of die1: " + theDice.getDie2());
    		System.out.println("Value of die1: " + theDice.getDie3() + "\n");
    		//Roll the dice
    	    theDice.roll();
    	    //Display the dice after being rolled
    		System.out.println("After rolling the dice: " + theDice.toString() + "\n");
    		//Display sum total of die face values
    		System.out.println("The roll total is: " + theDice.calcTotalRoll() + "\n");
    		//Displays whether the dies have alike face values
    		System.out.println(theDice.threeOfAKind() + "\n");
    		//Displays the highest value between the three die face values
    		System.out.println("The highest value of the three die is " + theDice.findHighestDie() + "\n");
    		//Rolls individual die's
     
     
     
    	}
     
    }

    As you can see. Whenever you roll a dice object, three different dice are rolled.

    The output she is expecting for the rollOneDice method is something like this: Rolling die 1 only: 1 2 5
    Rolling die 2 only: 4 6 3
    Rolling die 3 only: 5 5 2

    So each die is rolled individually three times I guess.

    I believe we are suppose to use if statements and toString but I am having trouble seeing where to start.

    She gave an example on the board which was like this:

    If ()
    die1 = ()()
    else
    if ()
    die2 = ....

    and she also wrote something like this on the board theDice.rollOneDice(2);

    Please help give me some sort of extra help on what to do.
    Thanks, I appreciate it.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Can't figure out how to roll individual dice.

    Looks like your instructor wants your rollOneDie() method to take an int parameter. Right now it doesn't.

    Then it looks like you're supposed to use that parameter to figure out which die should be rolled.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mortiz492 (October 19th, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can't figure out how to roll individual dice.

    Thanks! I figured it out.

Similar Threads

  1. Break down BigDecimal to individual values
    By tarkal in forum Java Theory & Questions
    Replies: 17
    Last Post: November 16th, 2011, 09:43 AM
  2. HELP random generator with roll() and print() method
    By disc_dido in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 18th, 2011, 01:50 PM
  3. Getting length of individual token?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 10th, 2011, 02:48 AM
  4. Controlling individual threads
    By youngstorm in forum Threads
    Replies: 4
    Last Post: October 19th, 2010, 03:28 PM
  5. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM