Click here to Skip to main content
13,736,502 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Using the Die class defined in Chapter 4, write a class called PairOfDice, composed of two Die objects. Include a constructor, methods to set and get each of the individual die values, a method to roll the two die, and a method that returns the current sum of the two die values.


What I have tried:

public class PairOfDice {
    Die die1 = new Die();
    Die die2 = new Die();
    
    public int getDie1Value() {
        return die1Value;
    }
    public int getDie2Value() {
        return die2Value;
    }
    public void setDie1Value(int die1) {
        die1Value = value;
    }
    public void setDie2Value(int die2) {
        die2Value = value;
    }
    public PairOfDice() {
        roll();
    }
    public int total() {
        return die1+die2;
}
}
Posted 12-Oct-18 10:10am
Updated 13-Oct-18 10:13am
Comments
0x01AA 12-Oct-18 16:21pm
   
1.) "Chapter 4" of what?
2.) Can you please describe what the Problem is? E.g. What you do-what you expect-what you get?
Otherwhise the only Thing I see is to flag this as "not a question" ...
Jenny Jiang 12-Oct-18 16:33pm
   
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;
}
}

this is the code from chaptor 4
Patrice T 12-Oct-18 16:41pm
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
0x01AA 12-Oct-18 16:37pm
   
And now? What Problem you have with it?
And btw. if the above code is important, better update your question with "Improve question" and add it ;)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your set methods are trying to set (and get) a variable that is not defined anywhere. You should be using the setFaceValue and getFaceValue methods of your individual Die objects.

[edit]
Also, your constructor calls the roll method, rather than creating the two dice, and the roll method is not defined anywhere so that will cause problems. Finally, your total method is trying to add the two Die objects together, rather than their values.
[/edit]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try to satisfy the requirements:
public class PairOfDice
{
  Die die1, die2;

  public PairOfDice()
  {
    die1 = new Die();
    die2 = new Die();
  }

  public int getFirstDieFaceValue()
  {
    return die1.getFaceValue();
  }

  public int getSecondDieFaceValue()
  {
    return die2.getFaceValue();
  }

  private int clamp(int value)
  {
    if ( value < 1) // here a public Die.MIN would have been useful
      value = 1;
    else if (value > 6) // here a public Die.MAX would have been useful
      value = 6;
    return value;
  }

  public void setFirstdDieFaceValue(int value)
  {
    die1.setFaceValue(clamp(value));
  }
  public void setSecondDieFaceValue(int value)
  {
    die2.setFaceValue(clamp(value));
  }

  public int roll()
  {
    return ( die1.roll() + die2.roll());
  }

  public int sumOfValues()
  {
    return (die1.getFaceValue() + die2.getFaceValue());
  }
  public static void main( String arg[] )
  {
    PairOfDice pod = new PairOfDice();
    System.out.printf("rolling the dices...\nResult = %d\n", pod.roll());
  } 
} 
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06-2016 | 2.8.180920.1 | Last Updated 13 Oct 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100