Results 1 to 2 of 2
  1. #1
    toby is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with my assignment java

    I have an assignment:
    Design and implement a class called PairOfDice, composed of tow die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that return the current sum of the die.
    Create a driver class called RollingDice2 to instantiate and use a PairofDice object.

    This is what I have so far I just wanted to see if I'm at least on the right track for this program.

    Java Code:
    public class PairofDie
    {
       private final int MAX = 6;
       private int faceValue;
       public Die1()
       {
    	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;
    }
    }
       public Die2()
       {
    	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;
    }
    }
    Thanks

  2. #2
    carl is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default

    Ok, to start, you'll need these variables for your PairofDie class:
    Java Code:
    private Die die1;
    private Die die2;
    Now, for die1 and die2, you'll notice they are declared as Die objects. This means that you'll have to create a Die class that will create a Die object, like so:
    Java Code:
    class Die
    {
      private final int MAX = 6;
      private int facevalue;
    
      public Die()
      {
        /* Constructor */
      }
      public void roll()
      {
        facevalue = (int)(Math.random() * MAX) + 1;
      }
    }
    In the above, you'll include your methods for getting and setting the values of the Die facevalue.

    Then in your your PairofDie constructor, you'd create your two dice:
    Java Code:
    public PairofDie()
    {
      die1 = new Die();
      die2 = new Die();
    }
    Rolling your dice would be a matter of:
    Java Code:
    public void rolldice()
    {
      die1.roll();
      die2.roll();
    }
    Then you'd include a method to return the sum of die1 and die2 in your PairofDie class.

    After all that, you'll create that RollingDice class that'll have the main method which will instantiate and utilize the PairofDie class.

    3 files (and classes) in all: Die.java, PairofDie.java, and RollingDice.java.
    Greetings.

Similar Threads

  1. Java assignment - couple methods don't know how to figure out
    By Snowboardmylife in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 11:52 AM
  2. Java assignment
    By xtianah77 in forum New To Java
    Replies: 1
    Last Post: 02-18-2008, 12:54 AM
  3. Please help... assignment for school
    By confused2000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2007, 09:12 AM
  4. for Assignment plz help
    By assamhammad in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 09:35 PM
  5. java assignment, need help bad.
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 05:53 PM

Posting Permissions

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