how do i make these codes NOT RECURSIVE

• 09-29-2010, 01:07 PM
XS-IV
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 ??
• 09-29-2010, 01:13 PM
Tolls
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.
• 09-29-2010, 01:40 PM
XS-IV
he said the method is recursive
• 09-29-2010, 01:43 PM
Tolls
What method?

And you appear to have removed the code making it even harder to see...
• 09-29-2010, 01:44 PM
XS-IV
wrong codes
• 09-29-2010, 01:46 PM
XS-IV
i'm new to java and programming in general, so i'm not sure what he meant by "enemyShip method is recursive"
• 09-29-2010, 01:47 PM
Tolls
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?
• 09-29-2010, 01:49 PM
Tolls
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...
• 09-29-2010, 01:50 PM
XS-IV
he didnt say a particular 1. he just said that my "enemy Ship is recursive"
• 09-29-2010, 03:47 PM
Norm
Where in your code is there a call to a method?
If there are no methods called, then its hard to have recursion.