Results 1 to 10 of 10
  1. #1
    XS-IV is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default how do i make these codes NOT RECURSIVE

    i submitted my codes and my teacher told me my codes is recursive. what does it means and how do i fix it ??
    Last edited by XS-IV; 09-29-2010 at 02:15 PM. Reason: mistake

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Well, first you put the code in CODE tags so it's formatted and readable, then you point out to us where the recursion is.

  3. #3
    XS-IV is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    he said the method is recursive

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    What method?

    And you appear to have removed the code making it even harder to see...

  5. #5
    XS-IV is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    wrong codes
    Last edited by XS-IV; 09-29-2010 at 02:56 PM. Reason: wrong codes

  6. #6
    XS-IV is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    i'm new to java and programming in general, so i'm not sure what he meant by "enemyShip method is recursive"

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Java Code:
    public class EnemyShip {
    	int gameWidth;
    	int position;
    	int velocity;
    	boolean destroyed;
    	char icon;
    	
    	EnemyShip() {
    		position = 0;
    		velocity = 0;
    		gameWidth = 0;
    		destroyed = false;
    		icon = 'e';
    	}
    	
    	EnemyShip(int pPosition, int pVelocity, int pGameWidth, char pIcon) {
    		position = pPosition;
    		velocity = pVelocity;
    		gameWidth = pGameWidth;
    		icon = pIcon;
    		destroyed = false;
    	}
    	
    	public void moveShip() {
    		//Don't move ship if game width is 0
    		if (gameWidth < 0) {
    			return;
    		}
    		
    		//Don't move ship if it is dead
    		if (destroyed) {
    			return;
    		}
    				
    		int tempVelocity = velocity;
    		while ((tempVelocity != 0)) {
    			//Check if boundary is reached
    			if ((position + tempVelocity) < -gameWidth ) {
    				tempVelocity += (position - gameWidth);
    				position = -gameWidth;
    				tempVelocity = -tempVelocity;
    				velocity = -velocity;
    			} else if ((position + tempVelocity) > gameWidth ) {
    				tempVelocity += (position - gameWidth);
    				position = gameWidth;
    				tempVelocity = -tempVelocity;
    				velocity = -velocity;
    			} else {
    				position += tempVelocity;
    				tempVelocity = 0;
    			}
    		}
    	}
    	
    	public char getIcon() {
    		return icon;
    	}
    
    	public void setIcon(char icon) {
    		this.icon = icon;
    	}
    
    	public boolean getDestroyed() {
    		return destroyed;
    	}
    	
    	public void setDestroyed(boolean pDestroyed) {
    		destroyed = pDestroyed;
    	}
    
    	public int getPosition() {
    		return position;
    	}
    
    	public int getVelocity() {
    		return velocity;
    	}
    	
    	public boolean inRangeOfFire(int pPosition) {
    		//Check if enemy is within fire position
    		if (!destroyed) {
    			if (position == pPosition) {
    				destroyed = true;
    				icon = 'X';
    				return true;
    			}
    		}
    		return false;
    	}
    	
    	public int getAbsolutePosition() {
    		return position + gameWidth;
    	}
    }
    CODE tags are the hash button in the reply window...

    Now, which method is recursive?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    There isn't an enemyShip() method.
    There's a couple of constructors, but they're not recursive.

    A recursive method is one which calls itself...

  9. #9
    XS-IV is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    he didnt say a particular 1. he just said that my "enemy Ship is recursive"

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Where in your code is there a call to a method?
    If there are no methods called, then its hard to have recursion.

Similar Threads

  1. two short codes
    By Libertyman in forum New To Java
    Replies: 7
    Last Post: 06-21-2010, 04:22 PM
  2. java codes
    By Balajee in forum AWT / Swing
    Replies: 1
    Last Post: 09-30-2008, 06:04 PM
  3. Assistant on my codes. SOS!!
    By sya1912 in forum Java Applets
    Replies: 16
    Last Post: 09-01-2008, 03:23 PM
  4. Replies: 9
    Last Post: 06-25-2008, 05:48 AM
  5. Posting codes and help
    By Java_Man in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •