Deplacer mon ocean et poisson

Fermé
matt2421 Messages postés 16 Date d'inscription jeudi 19 mai 2016 Statut Membre Dernière intervention 28 novembre 2016 - Modifié par KX le 19/05/2016 à 17:41
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 - 19 mai 2016 à 18:56
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);
}
}
}

1 réponse

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
19 mai 2016 à 17:46
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 jeudi 19 mai 2016 Statut Membre Dernière intervention 28 novembre 2016
19 mai 2016 à 17:54
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 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
19 mai 2016 à 18:07
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 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
19 mai 2016 à 18:16
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 jeudi 19 mai 2016 Statut Membre Dernière intervention 28 novembre 2016
19 mai 2016 à 18:41
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 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
19 mai 2016 à 18:56
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