Question:
JAVA: How do I know what goes in a test class and what goes in the main?
Niki
2012-06-07 11:14:30 UTC
I'm trying to make a matching/memory game but I don't know what methods I should put where...
Three answers:
2012-06-07 13:16:58 UTC
When given a problem to solve or a project to write, it is a mistake to sit down and try to immediately write it. Unless the program is trivial, you're bound to get stuck doing that. It is necessary to design the program first. Your questions asks what methods should go where and what classes you should have, but you won't know that until you have designed the game.



To start with, what exactly are you trying to make? There are many different kinds of memory games out there. What kind have you been asked to make? Or if you have your choice of games, what kind have you decided on? Will the game have graphics or be text-based? If it has graphics, draw out the board or whatever on paper and have a solid idea about what it should look like. Think also about things like a scoring system (if needed), supporting multiple players (if that is a requirement) and so on. You need to have a clear idea in your head of what the game itself is supposed to be before you even think of writing any code.



After you have a solid design for the game itself, then you can think about the code. If the program is to be graphical--but doesn't require any custom painting--then you might consider creating a subclass of JFrame and having a different class where everything gets tied together. To be clear, I'm talking about if your game will be run by simple GUI controls like buttons and check boxes. If you are taking this approach and don't know much about GUI's, this link will help you get started:

http://docs.oracle.com/javase/tutorial/uiswing/index.html



If your game will be graphical and does need custom painting logic, I recommend creating a custom subclass of JPanel for rendering the game. The following link has some interesting simple game tutorials that might help you think about how to create your game:

http://zetcode.com/tutorials/javagamestutorial/
?
2012-06-07 11:27:17 UTC
There is no reason you should have a test class and a separate main class. To test something you use a method not an object, unless the object is the gameboard itself and the main class has two players that access the gameboard... the gameboard therefore must have the methods to test data members that it contains...



You need to understand how you want your game to work before starting to program and worry about methods later... use real life as a model!!!!



Lets say I'm playing battleship... I don't know the other players pieces so I call the method gameBoard.testPosition(B, 5); this calls the method testPosition in the gameBoard class... I cannot see the game pieces but the gameBoard can.



inside the gameBoard class is a method called testPosition, it takes my two inputs and test whether an opponent ship is in that spot... gameBoard class contains all positions as a data member.



I hope this gives you some guidance.. or at least a way to start thinking about your programs... I'm scatterbrained at the moment so there's that....
godfatherofsoul
2012-06-07 13:40:48 UTC
I'm assuming you're using Eclipse or JUnit test cases? In that case, your main class handles the functional part of your program. If you use a separate test class, it will make sure that the methods (maybe even constructors) of your main class behave as they're supposed to. So, if my Main class has an add(int a, int b) method, I would create a JUnit test case that made sure that method did what it was supposed to.



Again, keep your test methods in a separate class from your functional code. You should probably keep them in separate projects as well. What this does is when you start writing bigger projects, you won't need to deploy a bunch of test code that you may or may not want your customers messing with.


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