Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Business Rules in IU AVLTree Applet

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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?

    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) {
    		}
    	}
    public synchronized void pause(String process) {
    		showTree = tree.toCoordinates(25, getWidth(),true);
    		showStatus(process);
    	    update(getGraphics());
    		try {
    			wait(delay);
    		} 
    		catch (InterruptedException e) {
    		}
    	}
    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.
    Attached Files Attached Files


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Business Rules in IU AVLTree Applet

    yes especially the method toCoordinates and traverseToCoordinates

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default 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.

    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,"



    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?



    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.



    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.

Similar Threads

  1. New to Java : Need help with my integers rules
    By MartinRR in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2011, 01:14 PM
  2. why business logic should be moved out of JSP ?
    By nithinurs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 28th, 2011, 11:40 AM
  3. Calculating Business days in java
    By narranil2 in forum Algorithms & Recursion
    Replies: 2
    Last Post: August 17th, 2010, 12:08 PM
  4. Funny business with JFrame, JPanel and JLabel
    By JeffC in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2010, 12:26 PM