Currently teaching myself java and am running into a few problems with designing classes and knowing what to put in them. Ive gotten my hands on the Head First Object Orientated Analysis and Design book so will be giving that a read soon but in the mean time hope someone can help.
I want to write a simple program to help me remember key points for the SCJP exam. Like a flash card with a question and answer.
I want to be able to add questions and save them to a text file or something. Could some one give me a general jist of to what classes I should have in this program.
I have a Card class with two String instance variables. One for question and one for answer. I have getter and setters for both.
I have a Deck class with an ArrayList for holding the cards. Im not quite sure what other methods to put in this class. Like should I have an addCard method? Or should that be in a seperate class??
Next I made a MainGui class that sorted out all the GUI bits. Not sure what methods tho to have in this class either. Should I have a method to set a label to the card question in the GUI class??
Sorry if i havent explained myself well. Its hard to even get across where I am gettin stuck. Any help would be great!
Re: Class design
Start with a QA class (Question Answer); it should have a constructor with two parameters: the question and the answer; to getter methods make sense but having two separate setter methods is tricky; i.e. you can change the question to something completely different so that the question/answer combination doesn't make sense anymore. I'd have no setters at all.
The Deck class should have no duplicate QA objects, therefore the Deck class can be a Set<QA> but (also) therefore the QA class should override the equals() and hashCode() methods (this is a technicality, well explained in the API docs). Adding a QA (or a Collection<QA>) makes sense; removing them also makes sense (see the API docs for the Set<T> interface for ideas).
For the GUI part: it should be able to display a question and get an answer from the user. Checking whether or not the answer is correct is not a GUI job (GUIs are supposed to be utterly stupid). Maybe a QA object can check whether or not an answer is corrrect.
Saving/loading stuff is easy: one line for the question and one line for the answer in a file should do fine.
Think of QA objects, a Set<QA> and a GUI and a 'controller'; think what you want to be able to do and what object should do it; that defines the methods you want and the interaction of the objects.