Question:
Can anybody find this below error of java program?
Ganesh Arayan
2014-01-03 08:39:51 UTC
package packman;

import javax.swing.JFrame;

import pacman.Board;


public class PacMan extends JFrame
{

public PacMan()
{
add(new Board());
setTitle("Pacman");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(380, 420);
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {
new PacMan();
}
}


I would like to learn why this error occurs during compile time and explain me some reasons to know

why this program compiles it showing error's reasons.......[ ( ' ' ]
Three answers:
Leo D
2014-01-03 11:04:33 UTC
The error might be in pacman.Board. There's no way to check because I don't have the code, but ignoring add(new Board()), there are no errors that I can see or test for.



These probably won't fix the error if it's in pacman.Board, but here are some tips:

- Try to use more descriptive names and less similar names. The main class, the package that it is in, and the package that the board is in are named PacMan, pacman and packman. This could get confusing.

- The only thing that your constructor should be doing is setting things. It shouldn't be making new things (other than itself) or doing any work other than setting things. You should pass the new board into the PacMan constructor rather than build it *in* the constructor.

- Don't call overrideable methods from the constructor. (Methods that are neither private, static nor final.)



Taking the last two things into account, you may want to move all of your configuration code into a factory method. This would look as so:



public PacMan() {}



public static PacMan newInstance(final Board board)

{

    final PacMan pacman = new PacMan();

    pacman.setTitle("Pacman");

    pacman.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pacman.setSize(380, 420);

    pacman.setLocationRelativeTo(null);

    pacman.add(board);

    pacman.setVisible(true);

    return pacman;

}



- Try not to couple things so tightly. Think of this: Does PacMan really have to be a JFrame? Could you instead make it a direct subclass of Object and build a JFrame to do that presentational work for PacMan? Could that JFrame built by PacMan be more abstract? Could the same code work with AWT Frames? At what score? What exactly is a pacman.Board?



Hope this helps! :)
?
2014-01-03 08:49:25 UTC
Muje nahi pata he
?
2014-03-26 01:44:02 UTC
download here: http://preview.tinyurl.com/okzj9nb and solve problem :D


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...