Results 1 to 2 of 2
Thread: Class design
- 03-29-2012, 10:54 PM #1Member
- Join Date
- Jul 2011
- Rep Power
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!
- 03-30-2012, 11:09 AM #2
- Join Date
- Sep 2008
- Voorschoten, the Netherlands
- Blog Entries
- Rep Power
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.
Joscenosillicaphobia: the fear for an empty beer glass
- By 0026sd in forum New To JavaReplies: 11Last Post: 11-30-2011, 04:03 PM
- By vgehts in forum JDBCReplies: 4Last Post: 05-12-2011, 12:23 PM
- By ConMan in forum New To JavaReplies: 4Last Post: 03-16-2011, 08:38 AM
- By dinosoep in forum New To JavaReplies: 3Last Post: 12-07-2009, 10:20 AM
- By faw in forum Advanced JavaReplies: 5Last Post: 04-21-2008, 07:25 PM