I'm not sure what's wrong with it. It's saying
4 errors found:
File: J:\Java\Java Acces\bookClasses\craps.java [line: 11]
Error: J:\Java\Java Acces\bookClasses\craps.java:11: craps is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
File: J:\Java\Java Acces\bookClasses\craps.java [line: 24]
Error: J:\Java\Java Acces\bookClasses\craps.java:24: cannot find symbol
symbol : class JLabe1
location: class craps
File: J:\Java\Java Acces\bookClasses\craps.java [line: 54]
Error: J:\Java\Java Acces\bookClasses\craps.java:54: cannot find symbol
symbol : variable sumLabel
location: class craps
File: J:\Java\Java Acces\bookClasses\craps.java [line: 55]
Error: J:\Java\Java Acces\bookClasses\craps.java:55: cannot find symbol
symbol : variable sumLabel
location: class craps
And it's weird, because I have ALL of the required packages. I'm fairly new at this, but I really enjoy it. Any help would be greatly appreciated.
Here is the code
[code]//Craps
//import java core packages
import java.awt.*;
import java.awt.event.*;
//java extension packages
import javax.swing.*;
public class craps extends JApplet implements ActionListener {
//constant variables for game status
final int WON = 0, LOST = 1, CONTINUE = 2;
//other variables used
boolean firstRoll = true;
int sumOfDice = 0;
int myPoint = 0;
int gameStatus = CONTINUE;
//graffic UI components
JLabe1 die1Label, die2Label, sumlabel, pointLabel;
JTextField die1Field, die2Field, sumField, pointField;
JButton rollButton;
//set up GUI components
public void init ()
{
//open content pane and change its layout to a FlowLayout
Container container = getContentPane();
container.setLayout ( new FlowLayout () );
//create label and text field for die 1
die1Label = new JLabel ( "Die 1" );
container.add ( die1Label );
die1Field = new JTextField( 10 );
die1Field.setEditable ( false );
container.add ( die1Field );
//create label and text field for die 2
die2Label = new JLabel ( "Die 2" );
container.add ( die1Label );
die2Field = new JTextField( 10 );
die2Field.setEditable ( false );
container.add ( die2Field );
//create label and text field for sum
sumLabel = new JLabel ( "Sum is" );
container.add ( sumLabel );
sumField = new JTextField( 10 );
sumField.setEditable ( false );
container.add ( sumField );
//create label and text field for point
pointLabel = new JLabel ( "Point is" );
container.add ( pointLabel );
pointField = new JTextField( 10 );
pointField.setEditable ( false );
container.add ( pointField );
//create button use clicks to roll the die
rollButton = new JButton ( "Roll die" );
rollButton.addActionListener ( this);
container.add ( rollButton );
}
//process one roll of dice
public void actionPreformed ( ActionEvent actionEvent )
{
//first roll of dice
if ( firstRoll ) {
sumOfDice = rollDice();
switch ( sumOfDice) {
//win on the first roll
case 7: case 11:
gameStatus = WON;
pointField.setText( " " ); //clear point field
break;
//lose on the first roll
case 2: case 3: case 12:
gameStatus = LOST;
pointField.setText( " ");
break;
//remember point
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
pointField.setText ( Integer.toString ( myPoint ) );
firstRoll = false;
break;
}
}
//subsequent roll of dice
else {
sumOfDice = rollDice();
//determines game status
if ( sumOfDice == myPoint )
gameStatus =WON;
else
if (sumOfDice == 7 )
gameStatus = LOST;
}
displayMessage();
}
public int rollDice()
{
int die1, die2, sum;
die1= 1 + (int ) (Math.random() * 6 );
die2= 2 + ( int ) (Math.random() * 6) ;
sum = die1 + die2;
die1Field.setText ( Integer.toString ( die1 ) );
die2Field.setText ( Integer.toString (die2 ) );
sumField.setText ( Integer.toString ( sum ) );
return sum;
}
public void displayMessage ()
{
//if the game were to continue
if (gameStatus == CONTINUE )
showStatus ( " Roll Again. " );
else {
if (gameStatus ==WON )
showStatus ( "Player Wins!! :D " +
"Click Roll Dice to play again." );
else
showStatus( "Player Loses. " +
"Click Roll Dice to play again." );
firstRoll = true;
}
}
}
[/code]
Well with my very limited Java experience so far, this error:
[code]Error: J:\Java\Java Acces\bookClasses\craps.java:11: craps is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener[/code]Your craps class implements interface ActionListener, which is abstract and has a method actionPerformed that needs to be overridden. So you need a method called actionPerformed that returns the same data type (int, double) and accepts the same parameters as the interface.
Like I said though, I don't have all that much experience, and in my Java class we just started on interfaces/classes.
Which looking at your code again, you just misspelled actionPerformed, on line 74. The other errors, I don't know about.
Yes, make sure you have all functions that you're implementing from abstract classes and use [b]@Override[/b].
Thanks for the help!
But how do I do an @Override? I don't necessarily want it done for me, I just need help/hints/examples.
You just put @Override on the line above the method that overrides the one in the interface. It isn't necessary though, it's just good to do so.
[code]
@Override
public void actionPerformed ( ActionEvent actionEvent )
{
//first roll of dice
if ( firstRoll ) {
sumOfDice = rollDice();
switch ( sumOfDice) {
//win on the first roll
case 7: case 11:
gameStatus = WON;
pointField.setText( " " ); //clear point field
break;
//lose on the first roll
case 2: case 3: case 12:
gameStatus = LOST;
pointField.setText( " ");
break;
//remember point
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
pointField.setText ( Integer.toString ( myPoint ) );
firstRoll = false;
break;
}
}
[/code]
[QUOTE=GetBent;25547638]You just put @Override on the line above the method that overrides the one in the interface. It isn't necessary though, it's just good to do so.[/QUOTE]
What does that do? I never learned about that when I was taught Java.
[QUOTE=Werem00se;25538307]
File: J:\Java\Java Acces\bookClasses\craps.java [line: 11]
Error: J:\Java\Java Acces\bookClasses\craps.java:11: craps is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
[B]Explanation: You're not overwriting actionPerformed Properly (explained before)[/B]
File: J:\Java\Java Acces\bookClasses\craps.java [line: 24]
Error: J:\Java\Java Acces\bookClasses\craps.java:24: cannot find symbol
symbol : class JLabe1
location: class craps
[B]Explanation: On line 24 you use an object named JLabel1. There are no labels in scope with that name[/B]
File: J:\Java\Java Acces\bookClasses\craps.java [line: 54]
Error: J:\Java\Java Acces\bookClasses\craps.java:54: cannot find symbol
symbol : variable sumLabel
location: class craps
[B]Explanation: On line 54 you use an object named sumLabel. There are no labels in scope with that name[/B]
File: J:\Java\Java Acces\bookClasses\craps.java [line: 55]
Error: J:\Java\Java Acces\bookClasses\craps.java:55: cannot find symbol
symbol : variable sumLabel
location: class craps
[B]Explanation: On line 55 you use an object named sumLabel. There are no labels in scope with that name[/B]
[/QUOTE]
The last three errors are here because you used the wrong name to refer to your labels. Just change the name to the right one.
[editline]22nd October 2010[/editline]
When posting code you should use (no spaces in the [])
[code]
[ cpp ]yourcodehere[/ cpp ]
[/code]
It will do C++ syntax highlighting and will show the line numbers. (I don't know if there's one for java)
[QUOTE=Larikang;25562229]What does that do? I never learned about that when I was taught Java.[/QUOTE]
It's just a tool to help prevent misspelling a method name and make sure you are actually overriding a method. For example, you are trying to override a method called getSum from another class. In the child class you try to override that method like so:
[code]
@Override
getSmu(){
...
}
[/code]Because you have the @Override, and getSum is misspelled, it recognizes that it actually doesn't override anything and gives you an error.
Sorry, you need to Log In to post a reply to this thread.