02-19-2004, 10:06 PM | #1 (permalink) |
Insane
Location: Chicago
|
[java] program help, possible instantiation problem.
Hey everyone, I am in need of some help with this program. It seemed like a simple program but I cannot get it to start. The problem comes up when I try to instatiate dice1 and dice2. It says it cannot start new GameDice() without an int. Even if I give it an int in the GameDice class, the dice1.getFace or the dice2.getFace does not do the job. Im sorry if this is not the proper way to ask for help but anything would be great. Thanks in advance. Here is the program./**
* This is a simple class the models dice * * @author (Danny Leal) * @version (2-18-04) */ public class GameDice { private int dieNumber = 1; private int diceRoll; public GameDice(int dice) { dieNumber = dice; } public void roll() { diceRoll = (int) (Math.random() * 6) + 1; //this will generate a random number between 1 and 6. } public int getFace() { return diceRoll; } } ///////////////THIS IS THE GAME PART////////////////////////// public class Game { public static void main(String args[]) { String playAgain = "y"; JOptionPane.showMessageDialog(null,"You will enter an amount of money to start with." + "You will the roll the dice, if the dice roll as a pair" + "you will win the amount of the dice. If the dice are not a pair" + "then your bank will be subracted the amount of the smaller dice"); GameDice dice1 = new GameDice(); GameDice dice2 = new GameDice(); String input = JOptionPane.showInputDialog(null,"Please enter the amount you want to play with in whole amounts only"); int userPurse = Integer.parseInt(input); do { userPurse = userPurse - 1; if (dice1.getFace() == dice2.getFace()) { JOptionPane.showMessageDialog(null,"Congradulations, the dice both show " + dice1.getFace() + " and you will be awarded $" + dice1.getFace() + " to your purse"); userPurse = dice1.getFace() + userPurse; } else { if (dice1.getFace() > dice2.getFace()) { JOptionPane.showMessageDialog(null,"Im sorry, you rolled a" + dice1.getFace() + " and a" + dice2.getFace() + ", your purse will have" + dice2.getFace() + "subtracted from your purse"); userPurse = userPurse - dice2.getFace(); } else { JOptionPane.showMessageDialog(null,"Im sorry, you rolled a" + dice1.getFace() + " and a" + dice2.getFace() + ", your purse will have" + dice1.getFace() + "subtracted from your purse"); userPurse = userPurse - dice1.getFace(); } } if (userPurse < 0) { playAgain = "n"; JOptionPane.showMessageDialog(null,"Im sorry, you have run out of money and are unable to play again."); } else { do { playAgain = JOptionPane.showInputDialog(null,"Would you like to play again? (Y)es or (N)o"); } while(!(playAgain.equalsIgnoreCase("y")||playAgain.equalsIgnoreCase("n"))); } } while(!(playAgain.equalsIgnoreCase("n"))); JOptionPane.showMessageDialog(null,"Thank you for playing"); } }
__________________
Jesus was a ruffies victim! Dan 3:20 |
02-19-2004, 10:23 PM | #2 (permalink) |
Junkie
Location: San Francisco
|
There is only one constructor defined for class GameDice, specifically one that takes an integer parameter. You can either make a new constructor that takes no parameters or change the statements
GameDice dice1 = new GameDice(); GameDice dice2 = new GameDice(); to pass an int. |
02-19-2004, 10:35 PM | #4 (permalink) |
Insane
Location: Chicago
|
ok, now I have a new problem, I went ahead and took out the parameters. It compiled great, but now dice1.getFace(); and dice2.getFace(); both seem to return 0. I have also tried passing an int through GameDice dice1 = new GameDice(1); and it still comes up with 0 when I try to use the getFace method.
__________________
Jesus was a ruffies victim! Dan 3:20 |
Tags |
help, instantiation, java, problem, program |
|
|