Thread: how do i make these codes NOT RECURSIVE

1. Member
Join Date
Sep 2010
Posts
5
Rep Power
0

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 01:15 PM. Reason: mistake

2. Moderator
Join Date
Apr 2009
Posts
13,222
Rep Power
23
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. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
he said the method is recursive

4. Moderator
Join Date
Apr 2009
Posts
13,222
Rep Power
23
What method?

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

5. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
wrong codes
Last edited by XS-IV; 09-29-2010 at 01:56 PM. Reason: wrong codes

6. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
i'm new to java and programming in general, so i'm not sure what he meant by "enemyShip method is recursive"

7. Moderator
Join Date
Apr 2009
Posts
13,222
Rep Power
23
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. Moderator
Join Date
Apr 2009
Posts
13,222
Rep Power
23
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. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
he didnt say a particular 1. he just said that my "enemy Ship is recursive"

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

Posting Permissions

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