Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newby - how to seperate program into classes

Status
Not open for further replies.

mashadt

Instructor
Apr 23, 2005
33
ZA
Hi

I am having problems seperating my program into classes rather than just having everything in one class.

The program I am working on reads data from file, places it into an array and then does stuff to it. At the moment I have two classes, one that takes user input, the other does everything else. I would like to be able to seperate the second class into classes that can read values into the array, check through the array and alter it, etc.

I have put a simplified version of my two classes at


If someone could suggest to me how to go about seperating these into neater
classes?

Thanks
 
i cant see any real reason why you would want to. the method isnt that big or complicated to warrent new classes. the general rule of thumb is, if your method is longer than 100 lines of code then you split it up, either into other methods or into other classes as the situation warrents it.

if you wanted you could put your error checking into another method that returns true if an error occurs and false if no error occurs. and like wise the stuff you do when no error occurs. call that method and run an if statement.

Code:
booean error = checkForErrors(filename);

if (error = true)
{
   //show the errors etc
}

else
{
    // call method that does stuff when no error occurs
}

i wouldn't create new classes for the code you have at the moment because new methods would be more applicable. creating classes out of this code would be an unnessary overhead as methods will do the job.
 
mashadt,


Your print class could be something like this:

public class PrintAll
{
String arrayToPrint[];

public PrintAll(String inArrayToPrint[])
{
arrayToPrint = inArrayToPrint;
}


public void print()
{
for(int counter = 0; counter < array.length; counter ++)
{
System.out.println(arrayToPrint[counter]);
}
}



}//public class PrintAll



In your ReaderTest class add a method:

public String[] getArray()
{
return array;
}


Then you can create an instance of the new class in class TestMain:

PrintAll printAll = new PrintAll(read.getArray());

And finally add this line:

printAll.print();



But I believe you may have a problem with the line:

array = new String[tLength];

since tLength is a variable.


If you would like to have a dynamic structure you may want to use a vector instead.


Alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top