Deplacer mon ocean et poisson

matt2421 Messages postés 16 Date d'inscription   Statut Membre Dernière intervention   -  
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   -
salut! Voici trois classes que j'ai cree via Eclipse (Fish, ocean ,oceanlifemain) et je n'arrive pas a deplacer mon ocean et mon poisson de 5++.
besoin d'aide svp! Il y a un peu d'allemand dedans mais c'est pas important!
package infpp.oceanlife;
public class Fish {
 private int x;
 private int y;
 private String name;
 public Fish(int x, int y, String name) {
   this.x = x;
   this.y = y;
   this.name = name;
 }
 public int getX() {
   return this.x;
 }
 public void setX(int x) {
   this.x = x;
 }
 public int getY() {
   return this.y;
 }
 public void setY(int y) {
   this.y = y;
 }
 public String getName() {
   return this.name;
 }
 public void setName(String name) {
   this.name = name;
 }
/**
* 
* Rückgabewert eines Fisches
* 
*/
 public String toString() {
   return "Fish [x=" + x + ", y=" + y + ", name=" + name + "]";
 }
}

package infpp.oceanlife;
public class Ocean {
private int width;
private int depth;
private Fish fish; 
    public Ocean (int width, int depth, Fish fish) {
    this.width = width;
    this.depth = depth;
    this.fish = fish;
    }
    public int getwidth () {
   return this.width;
    }
    public void setwidth (int width) {
   this.width = width;
    }
    public int getdepth () {
   return this.depth;
    }
    public void setdepth (int depth) {
   this.depth = depth;
    }
    public Fish getfish () {
   return this.fish;
    }
    public void setfish (Fish fish) {
   this.fish = fish;
    }
    public void move () {
   int H = fish.getX();
   int V = fish.getY();
   System.out.println("Die aktuell position von dem Fische "  + H + " " + V);
    }
    public String toString () {
   return "Ocean [width = " + width + ", depth = " + depth + ", fish = " + fish + "]";
    }
}    

package infpp.oceanlife;
public class OceanLifeMain {
public static void main(String[] args) {
int width = 1024;
int depth = 768;
Ocean ocean = new Ocean(width, depth, new Fish(4 , 6 , "nemo"));
for (int i = 0; i < 5; i++) {
ocean.move ();
System.out.println(ocean);
}
}
}
A voir également:

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Bonjour,

À part l'affichage ta méthode move ne fais rien...

public void move() {
    int H = fish.getX();
    int V = fish.getY();
    System.out.println("Die aktuell position von dem Fische " + H + " " + V);
}

Si tu veux déplacer le poisson il faudrait utiliser setX ou setY.
1
matt2421 Messages postés 16 Date d'inscription   Statut Membre Dernière intervention  
 
Salut!!Tu veux dire changer getX par SetX? car je l'ai fait mais ca fonctionne pas. Suis pas trop un As en programmation.
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Le get c'est pour récupérer la valeur, le set c'est pour la modifier.

Par exemple, si tu veux aller à droite (x+1) tu peux faire :

public void move() {
    int previousX = fish.getX();
    int previousY = fish.getY();
    int nextX = previousX + 1;
    int nextY = previousY;
    fish.setX(nextX);
    fish.setY(nextY);
    System.out.println("Der Fisch bewegt von (x="+previousX+",y="+previousY
        +") bis (x="+nextX+",y="+nextY+")");
}

Remarque : je pense que c'est encore mieux de passer deux paramètres à la méthode move pour savoir de combien il faut se déplacer sur chaque axe.

public void move(int deltaX, int deltaY) {
    int previousX = fish.getX();
    int previousY = fish.getY();
    int nextX = previousX + deltaX;
    int nextY = previousY + deltaY;
    fish.setX(nextX);
    fish.setY(nextY);
    System.out.println("Der Fisch bewegt von (x="+previousX+",y="+previousY
        +") bis (x="+nextX+",y="+nextY+")");
}
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Remarque : il serait plus logique que la méthode move soit dans la classe Fish, ou qu'elle prenne en argument un objet Fish à déplacer.
0
matt2421 Messages postés 16 Date d'inscription   Statut Membre Dernière intervention  
 
Merci. Ca fonctionne parfaitement! Mais j'ai juste une question avant de refermer le sujet:

Que signifie ce fish.setX (next X); et ne pourrait -on pas l'ecrire de la meme forme que int previousX = fish.getX ()? Du genre int nextX =fish.setX (nextX); ?

Merci encore!!!!!!
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
C'est toi qui a définit les méthodes getX et setX, je ne fais qu'utiliser ton code...

 public int getX() {
   return this.x;
 }
 public void setX(int x) {
   this.x = x;
 }
0