-7

I think my issue is with the PairOfDice class because I need to only use an object containing both dice values in the EyesHaveIt class. I keep getting errors like: PairOfDice.java:16: error: class, interface, or enum expected public int getDie1() ^

PairOfDice.java:19: error: class, interface, or enum expected } ^

PairOfDice.java:21: error: class, interface, or enum expected public int getDie2() ^

PairOfDice.java:24: error: class, interface, or enum expected } ^

EyesHaveIt.java:44: error: cannot find symbol int die1 = dieOne.getDieOneValue(); ^ symbol: method getDieOneValue() location: variable dieOne of type PairOfDice EyesHaveIt.java:45: error: cannot find symbol

Here is my attempt at trying to fix it. Thanks!

Here's my Die Class:

import java.util.Random;

public class Die

{

public static Random generator = new Random();

public int roll ()

{

      return generator.nextInt(6) + 1;  // Randome # dice between 1-6

}

}

\\Here's my PairOfDice class:

public class PairOfDice
{
   private int die1Value;
   private int die2Value;

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

      die1Value = die1;
      die2Value = die2;
    }
}   
   public int getDie1()
   {
      return die1;
   }

   public int getDie2()
   {
      return die2;
   } 

}

Here is the EyesHaveIt class where I'm trying to use that code:

import java.util.Scanner;

public class EyesHaveIt
{
   Scanner kb = new Scanner(System.in);
   private int turnScore;
   private int computerTotalScore;
   private int playerTotalScore;
   private int computerTurnNumber;
   private int playerTurnNumber;
   public int roundPoints;
   private String userName;

   public void init(String name)
   {
      userName = name;
   }

   public void playGame()
   {
      computerTurn();
   }

   public void computerTurn()
   {
      turnScore = 0;
      System.out.println("Computer Turn: ");
      while (turnScore < 20)
      {
         rollTheDice();
      }
   }

   public void rollTheDice()
   {
      PairOfDice dieOne = new PairOfDice();
      PairOfDice dieTwo = new PairOfDice();
      int die1 = dieOne.getDieOneValue();
      int die2 = dieTwo.getDieOneValue();
      System.out.println("Rolled: " + die1 + " and " + die2);
   }

This class will be an abstraction of a physical pair of standard six-sided dice. Only one object of this class will be created by the EyesHaveIt object and used throughout the game by both players. You should consider this object to be a pair of dice. When a player rolls during a turn, that player is rolling the PairOfDice object. Players never roll a Die object directly. The client of the PairOfDice class will roll the PairOfDice object, get the values of each die in the pair, etc. This class should have exactly two attributes: two objects of the class, Die. This is the only class that uses the Die class.

  • In **PairOfDice.java** you have an extra right brace immediately before the `getDie1()` method. Remove it. – skomisa Apr 12 '20 at 23:40

1 Answers1

1

The Die class is fine the way it is:

import java.util.Random;

public class Die {

    public static Random generator = new Random();

    // Roll of a 'Single Die'
    public int roll() {
        return generator.nextInt(6) + 1;  // Random # dice roll between 1-6
    }
} 

In your current PairOfDice class however, you have two specific class global instance member variables (of int data type) named die1Value and die2Value yet in the constructor for the very same class you declare two Die's (die1 and die2) and assign them to die1Value and die2Value respectively:

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

      die1Value = die1; 
      die2Value = die2;
}

You can't do this since die1 and die2 are not int's, they are Die's. Also, die1 and die2 should have also been declared as instance member variables. What should have been done is:

 public class PairOfDice {

     private int die1Value;
     private int die2Value;

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

     // PairOfDice Constructor...
     public PairOfDice() {
        this.die1Value = die1.roll();
        this.die2Value = die2.roll();
    }

    public int getDie1() {
        return this.die1Value;
    }

    public int getDie2() {
        return this.die2Value;
    }

    // Roll of a 'Pair Of Dice'
    public void roll() {
        this.die1Value = die1.roll();
        this.die2Value = die2.roll();
    }
}

Now, die1Value and die2Value are actually assigned int's (a random roll value of each die).

I don't know anything about the game called EyesHaveIt and I really don't feel like researching it but looking at the code I'm sure it doesn't play with two sets of dice (4 dice - Each declaration of PairOfDice consists of two dice).

I think the Pair of dice used to play the game should be declared as a class member variable rather than declaring a new set of dice each time you need a dice roll. With the addition of the roll() method within the PairOfDice class, you can use the same pair of dice indefinitely. Here is a simple example of using these two classes, we're going to throw a pair of dice 10 times:

public class JustADemo {

    private final PairOfDice dice = new PairOfDice();
    private int dice1;
    private int dice2;

    public static void main(String args[]) {
        // Done this way to avoid the need for statics.
        new EyesHaveIt().startDemo(); 
    }

    private void startDemo(String[] args) {
        // Roll a Pair of dice 10 times...
        for (int i = 0; i < 10; i++) {
            System.out.println("Roll #" + (i + 1) + ":");
            rollTheDice();
            System.out.println();
        }
    }

    private void rollTheDice() {
        dice.roll();
        dice1 = dice.getDie1();
        dice2 = dice.getDie2();
        System.out.println("Face of Die 1: --> " + dice1);
        System.out.println("Face of Die 2: --> " + dice2);
    }
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22