Business Rules in IU AVLTree Applet

• September 21st, 2012, 05:31 PM
glaydersonjonathan
Business Rules in IU AVLTree Applet
Good afternoon,

Guys I'm trying to finish a project that simulates a queue through graphical interface. I have a project of someone who did AVL tree that way. At first, I thought it would be easy to make the adjustment because as an AVL tree had all ready (most difficult), this would make a queue pretty easy. Well, I was wrong. Is ready my linked queue, it works perfectly, furthermore, most of the Applet is already mounted with the events of the operating knobs and so forth. But the applet's drawing methods are impossible to understand. I'm several weeks trying to figure out what this guy did, but no progress yet. Soon there's no way I build my queue, if not before understand how the methods of the original painting. Someone experienced can teach me the business rules involved in these methods?

Code :

public void paint(Graphics g) {
try {
for (int i = 1; i < showTree.length; i++)
g.drawLine(showTree[i][2],
showTree[i][1]+50,
showTree[showTree[i][4]][2],
showTree[showTree[i][4]][1]+50);
for (int i = 0; i < showTree.length; i++) {
int margin=-13;
if(showTree[i][0]<-99) margin = -13;
else if(showTree[i][0]<-9) margin = -10;
else if(showTree[i][0]<0) margin = -5;
else if(showTree[i][0]<10) margin = -2;
else if(showTree[i][0]<100) margin = -7;
else if(showTree[i][0]<1000) margin = -10;
if((char)showTree[i][3] == 'L')	g.setColor(Color.RED);
else if((char)showTree[i][3] == 'R')	g.setColor(Color.GREEN);
else g.setColor(Color.YELLOW);
g.fillOval(showTree[i][2]-12, showTree[i][1]+38,25,25);
g.setColor(Color.BLACK);
g.drawString(Integer.toString(showTree[i][0]), showTree[i][2]+margin, showTree[i][1]+54);
}
}catch(NullPointerException e) {
}
}

Code :

public synchronized void pause(String process) {
showTree = tree.toCoordinates(25, getWidth(),true);
showStatus(process);
update(getGraphics());
try {
wait(delay);
}
catch (InterruptedException e) {
}
}

Code :

public int[][] toCoordinates(int h, int w, boolean fixH) {
int c[][] = new int[size][5];
int i[] = { 0 };
int hStep = (height > 1) ? h / (height - 1) : 0;
hStep = fixH ? h : hStep;
traverseToCoordinates(root, i, c, 0, w / 2, hStep, w / 4, -1);
return c;
}
private void traverseToCoordinates(
TreeEntry e,
int i[],
int c[][],
int y,
int x,
int h,
int w,
int p) {
int curI = i[0];
if (e == null)
return;
if (e.element instanceof ElementObject) {
c[i[0]][0] = ((ElementObject) (e.element)).val; // integer value
c[i[0]][1] = y; // vertical position
c[i[0]][2] = x; // horizontal position
c[i[0]][3] = (int) e.balanceFactor; // balance factor
c[i[0]][4] = p; // index of parent in c[][]
i[0]++;
}
traverseToCoordinates(e.left, i, c, y + h, x - w, h, w / 2, curI);
traverseToCoordinates(e.right, i, c, y + h, x + w, h, w / 2, curI);
}

Of course it is not 100% object-oriented and organized as it should, for example, the method public int[][] toCoordinates (int h, int w, boolean fixH) it uses an array of 5 columns of information for guard positions X and Y that the lines should be drawn. Well if I understand the business rules, the logic of functioning of this tree, I can certainly mount a queue, which I believe is much easier to do. Attached is the Eclipse project these methods, you can see that the original project it works perfectly even when you increase the screen size gets better visualization of the tree. In the package modificado is my attempt to make such a row, yet fail because of this graphical interface..

Thanks for all the explanations.
• September 22nd, 2012, 07:16 AM
curmudgeon
Re: Business Rules in IU AVLTree Applet
So let me make sure that I understand this well: you want us to explain the logic behind someone else's program that you're borrowing?
• September 24th, 2012, 12:24 PM
glaydersonjonathan
Re: Business Rules in IU AVLTree Applet
yes especially the method toCoordinates and traverseToCoordinates
• September 24th, 2012, 11:06 PM
jps
Re: Business Rules in IU AVLTree Applet
I am not against the idea of using someone's code to learn from, but I am against making adjustments and using it as your own work.
Besides many times it turns out to be easier to design and implement a solution to a specific problem with that problem in mind rather than using code meant for a different problem.
More importantly using the modified code robs you of your chance to go through the process one more time. Since the best thing you can do is practice the process, you are ahead to do so now rather than later.

Quote:

Of course it is not 100% object-oriented and organized as it should
If you started from scratch and went according to plan, this problem should not exist and makes me ask myself, is this shortcut so short? ..."are impossible to understand. I'm several weeks trying to figure out what this guy did,"

Quote:

I thought it would be easy to make the adjustment
Did you draw up something to compare to? If so, the hard part is done, why not start from scratch? If not, how reliable is the comparison?

Quote:

Someone experienced can teach me the business rules involved in these methods?
If you want apples, plant apple trees, don't paint oranges red. In the end you can have plenty of high quality apples, or a handful of oranges that don't pass the test.

Quote:

yet fail because of this graphical interface..
Given a choice I would rather work up an entire project based on code I wrote (understand throughout) rather than some other code, especially other code I did not understand.

My advice is to tackle this on your own, and if you encounter something you do not understand, ask a question, and by project's end you will understand all of your code.