user481710
user481710

Reputation: 59

Java Objects classes and methods program trouble

My program is meant for two players. Each player gets to roll the dice three times each, where they can keep numbers that they roll each roll. If they keep one number from the first roll, they can continue to roll two more times until they choose to keep the number. the two dice are added together and the player with the highest sum wins. I have a player class (in charge of keeping the dice values), the pairOfdice class where the dice are rooled and the sum is determined, and the main game. When I run the program, I receive these errors from the diceGame class. I would like to know what these these errors mean. My code is not complete yet.

main game

import java.io.*;
import java.lang.*;
import java.util.*;



public class diceGame
{
   public static void main(String[] args) {

  pairOfDice dice;

  dice = new pairOfDice();

  playerGame player;

  player = new playerGame();

  int rollCount = 0;

 int holdA = 0, holdB = 0;
  do {
           dice.roll();    // Roll the first pair of dice.
            System.out.println("Dice 1: " + dice.getDiceA() + "\n" + "Dice 2: " + dice.getDiceB() + "\n" + "The total is: " + dice.getTotal());
    System.out.println("Do you want to hold the fist dice with a value of " + dice.getDiceA());
    if (holdA == 1){
      player.getHoldA();
          player.getHoldA(dice.getDiceA());
          System.out.println("Value of dice A is held");
    }
    System.out.println("Do you want to hold the second dice with a value of " + dice.getDiceB());
      if (holdB == 1){
            player.getHoldB(dice.getDiceB());
            System.out.println("Value of dice B is held");
    }

           rollCount++;
            }
  while (dice.getTurns() <= 3);
  }
}

Player Class

import java.io.*;

public class playerGame
{
     private int holdA = 0, holdB = 0;

     //constructor
     public playerGame(){
     }

      public void setHoldA (int valA){
         holdA = valA;
       }
     public void setHoldB (int valB){
     holdB = valB;
     }
     public void setPlayer(int player){
       player = player + 1;
     }

     public int getHoldA () {
       return holdA;
     }
     public int getHoldB () {
       return holdB;
     }    

       }

pairOfdice class

import java.io.*;

public class pairOfDice
{

  private int diceA = 0, diceB = 0, turns = 0;

  public pairOfDice() {//constructor
    roll();  
  }
  public void roll(){
    diceA = (int)(Math.random()*6) + 1;
    diceB = (int)(Math.random()*6) + 1;
    turns = turns +1;
  }

  public void setDice(int newDiceA, int diceA) {
    diceA = newDiceA;
  }
  public void setDiceB(int newDiceB, int diceB) {
    diceB = newDiceB;
  }
  public void setTurns(int turns) {
    turns = 0;
  }

  public int getDiceA() {
    return diceA;
  }
  public int getDiceB() {
    return diceB;
  }
  public int getTotal() {
    return (diceA + diceB);
  }
  public int getTurns() {
    return turns;
  }
}

Upvotes: 1

Views: 645

Answers (2)

StormeHawke
StormeHawke

Reputation: 6207

You're attempting to use a getter (which takes no arguments) to set a value. Naturally Java doesn't know what to do with this.

  player.getHoldA(dice.getDiceA());

should be

  player.setHoldA(dice.getDiceA());

Upvotes: 2

Jeff Storey
Jeff Storey

Reputation: 57192

Your methods getHoldA and getHoldB do not take any parameters, but you call them with a value player.getHoldA(dice.getDiceA()); Maybe you meant to call the setter?

Upvotes: 1

Related Questions