Results 1 to 2 of 2

Thread: Class design

  1. #1
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Rep Power

    Default Class design

    Hey all,

    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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default 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.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 11
    Last Post: 11-30-2011, 04:03 PM
  2. Class design best practices
    By vgehts in forum JDBC
    Replies: 4
    Last Post: 05-12-2011, 12:23 PM
  3. Class Design Problems (BlackJack)
    By ConMan in forum New To Java
    Replies: 4
    Last Post: 03-16-2011, 08:38 AM
  4. class design
    By dinosoep in forum New To Java
    Replies: 3
    Last Post: 12-07-2009, 10:20 AM
  5. Pls HeLp Me (Design a class named Fan)
    By faw in forum Advanced Java
    Replies: 5
    Last Post: 04-21-2008, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts