Im quite a beginner at java, and in my program i have been doing alot of cutting and pasting (i.e. repeating lines of code). The current number of lines of code for an alpha alpha version is 1394 and its probably down to me not understanding java very well. Ok, i have an array of 100 textboxes. i have a JButton that when pressed gets a random number between one and ten. This random number then effectively runs alot of code, and dictates the textbox starting location. The purpose of the code is to change colors. As such, i have had to retype out all the code from (random number = 1 ... random number = 10). So rather that retype all this code i want to type it out once and then the program will reference back to it when needed. I believe that this is dont through Classes. To my understanding, if object A and object B both have identical "parts" (eg values, or calculations), you use a class, and refer back to that class rather than retype everything. I am not very proficient at doing this however and would like help.
Do i (A) create a class within the current code so its only typed once, and refer back to it (B) create a new class file that sits in the same folder as the application and refer back to it
As there is so much code, it isnt really applicable to post it all but ill try to post current structure for you.
if (RandomNumber == 1) Do taskA; if (RandomNumber == 1) Do taskB; if (RandomNumber == 1) Do taskC;
if (RandomNumber == 2) Do taskA; if (RandomNumber == 2) Do taskB; if (RandomNumber == 2) Do taskC;
Using the above example do i create Classes "taskA" "taskB" "taskC" and initate them when a RandomNumber is identified? if this is the correct way, would it be recoded as follows?
if (RandomNumber == 1) JTextareaA.taskA; if (RandomNumber == 1) JTextAreaA.taskB; if (RandomNumber == 1) JTextAreaA.taskC;
if (RandomNumber == 2) JTextareaB.taskA; if (RandomNumber == 2) JTextAreaB.taskB; if (RandomNumber == 2) JTextAreaB.taskC;
If im not on the right track please guide me. Thankyou in advance,
I think to answer your question, if what you're looking for is altering similar objects in similar ways, you might be better suited into making a method in your class (rather than an entirely new class) for each kind of change, and having it take the object that needs to be modified/called as a parameter. So, if you're trying to change a JTextArea to a certain color (for example), you might call it by:
Which would make it quick and easy and extensible (allows for addition of more text areas and colors, for instance).
Also, assuming that the structure is how you're showing it (three conditionals in a row for each state that the random number could be in), I'd start by suggesting using code-blocks instead of multiple conditionals, as it'll simplify your code:
(where, obviously, the ... is the rest of the states the RandomNumber can be in.)
That should immediately reduce the lines of code. Another idea might be to use a switch statement:
Either one should help you, if I'm understanding what you're asking. (Who knows, I could be totally off-base.)
(Modified to clarify, after a coworker pointed out I wasn't entirely clear on what I meant.) [ February 23, 2005: Message edited by: Theodore Casser ]
Theodore Jonathan Casser
SCJP/SCSNI/SCBCD/SCWCD/SCDJWS/SCMAD/SCEA/MCTS/MCPD... and so many more letters than you can shake a stick at!
yes you have been a help - showing how i could restructure my code to cut out alot of lines. However, as my program stands, "taskA" "taskB" etc are 57 lines each, which are repeated for each random number.
if (RandomNumber == 1) Do taskA for JTextAreaA; //the word "taskA" represents 57 lines being typed out
if (RandomNumber == 2) Do taskA for JTextAreaB; //as you can see i have to retype out the SAME 57 lines
To cut out the duplication of lines, do i create a class called "taskA" which contains those 57 lines, then use (as you suggested)
Do i create a new file for this class or do i include it in the main code?
thanks in advance, joseph [ February 23, 2005: Message edited by: joseph mcgratton ]
The key principle that you're practicing here is making your code generic. Where you have 57 lines that operate on textArea1 and then the same 57 lines operating exactly the same way on textArea2, you can put them all into a method that takes a JTextArea as the others have already said.
Once you have that done, I'd say the next good spot to genericize would be the 10 random number cases. Let's say that the above simplifies your code to this:You can replace the 10 explicit if blocks using an array of JTextAreas.Anywhere you find that you're performing the same tasks where only the objects on which you operate change you have a great opportunity to simplify your code.
By the way, what do you have against FireWire?
Police line, do not cross. Well, this tiny ad can go through:
Free, earth friendly heat - from the CodeRanch trailboss