Results 1 to 4 of 4
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default [SOLVED] When should one create a new class?

    This may strike you as a strange question, but when is it acceptable to create a new class?

    For example, in an assignment I am currently working on I need to take in a list of words in a .txt file (each word on a new line and the file always ends with an empty line). I then need to sort the words in alphabetical order and output them into a new text file with the amount of words in the top line.

    My idea currently (may change at a later stage depending on how I approach this), is to read the input file into an array, sort the array, and then write the array contents to an output file.

    What I am having a problem with is when is it right to create a new class? Do I create a new class that manages the details of the input file, one for the output file and one as a Driver? Or do I merely keep it all bundled together in one class?

    If you could please provide reasoning for what you are suggesting that would be great. I mean, my own thoughts are that I keep it all in one class, because I can't really see the input file or the output file as separate entities, how do you decide what to do?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Not being a "class" expert, it depends on what you want to do:
    • If this is a one shot assignment, then it can be done in one class (it's fairly simple and straight foward).
    • If this is part of a series of building assignments, one assignment builds off the previous one, then creating different classes would probably be better. For example, you can keep all your file I/O methods in one class (find file, open file, read file, write file, etc). that way you can reuse the class methods over again. This would probably be a good practice and build up a library of useful classes...


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Not being a "class" expert, it depends on what you want to do:
    • If this is a one shot assignment, then it can be done in one class (it's fairly simple and straight foward).
    • If this is part of a series of building assignments, one assignment builds off the previous one, then creating different classes would probably be better. For example, you can keep all your file I/O methods in one class (find file, open file, read file, write file, etc). that way you can reuse the class methods over again. This would probably be a good practice and build up a library of useful classes...


    Luck,
    CJSL
    Thank you for the response.

    So there are no rules as to when you should and shouldn't create a class?

    I was just thinking just before I checked back here that I might create a class called "File.java" and put all the I/O stuff in there, right now it is all in one class, but it is pretty easy to move the bits that matter and put them in a separate class. I mean, it is all pretty standard stuff so it would be useful to keep the classes rather than rewrite this always.

    To clarify though, this assignment doesn't involve questions building off of one another, it is more to test the whole range of basic Java concepts. One involved inheritance, one involves this reading to an array thing - tests array usage, reading and writing to files etc., there is one involving a simple GUI done in swing that involves a single frame with a few menu items, buttons and a single text field.

    Still could'nt hurt to start creating classes with these sort of functions.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Still could'nt hurt to start creating classes with these sort of functions.
    I agree... get started on the right foot... remember that OOP models the world around us... for example, animals and fruits are not in the same class in the real world, therefore in Java they should have their own separate classes.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. how to create static array in class
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 03:33 PM
  2. How to create class from a C structure
    By jcardozo in forum New To Java
    Replies: 8
    Last Post: 12-06-2008, 10:47 AM
  3. How to create a class in java?
    By pawankumarom in forum New To Java
    Replies: 2
    Last Post: 09-05-2008, 07:47 AM
  4. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 PM
  5. How to create your own Exception class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:40 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
  •