Results 1 to 4 of 4
- 04-28-2009, 06:27 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
[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?
- 04-28-2009, 07:11 PM #2
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-28-2009, 07:58 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
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.
- 04-29-2009, 01:44 AM #4
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.Still could'nt hurt to start creating classes with these sort of functions.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
how to create static array in class
By itaipee in forum New To JavaReplies: 1Last Post: 04-19-2009, 03:33 PM -
How to create class from a C structure
By jcardozo in forum New To JavaReplies: 8Last Post: 12-06-2008, 10:47 AM -
How to create a class in java?
By pawankumarom in forum New To JavaReplies: 2Last Post: 09-05-2008, 07:47 AM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM -
How to create your own Exception class
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks