besoin d'aide pour un programme java (urgent =x )

besoin d'aide pour un programme java (urgent =x ) - Java - Programmation

Marsh Posté le 15-12-2014 à 19:37:50    

Bonsoir ,
 
Je dois réaliser un programme java dans lequel des loups mangent des moutons qui mangent de l'herbe (original :D), j'ai pour cela créer des classes Position (associée à vivant), Vivant (associée à vivant) , Herbe (fille de vivant) , Animal (fille vivant)  , Mouton (fille animal) , Loup (fille loup).
Mon main (appelé LetsEat) demande à l'uttilisateur le nombre d'herbe, de loups et de moutons puis je créer n* instances pour chacune d’entre elles, je veux ensuite afficher leur positions et leur noms.
Mon problème est le suivant; je ne parviens pas à obtenir la position d'une instance et à l'afficher mal grès toutes mes tentatives.
Je suis un novice en Java, j'ai tout appris sur le net (très peu de cours).
Je dois le rendre ce soir, donc c'est plutôt urgent (le prof nous a demandé de finir les classes j'aimerai approfondir un peu )  
 
 

Code :
  1. import java.lang.Math;
  2. import java.util.Random;
  3. public class Position {
  4. private int  xMax=100;
  5. private int yMax=100;
  6. private int X;
  7. private int Y;
  8. private Random r = new Random();
  9. public void Position(int x,int y){
  10. this.X=x;
  11. this.Y=y;
  12. }
  13. public void Position(){
  14. // this.X=(int)( Math.random()*( xMax - 1 + 1 ) ) + 1;
  15. //this.Y=(int)( Math.random()*( yMax - 1 + 1 ) ) + 1;
  16. this.X=r.nextInt(xMax);
  17. this.Y=r.nextInt(yMax);
  18. }
  19. public int getX(){
  20. return this.X;
  21. }
  22. public int getY(){
  23. return this.Y;
  24. }
  25. public void setX (int x){
  26. if(x>=0 && x<=100)
  27. this.X = x;
  28. }
  29. public void setY (int y){
  30. if(y>=0 && y<=100)
  31. this.Y = y;
  32. }
  33. public int distance(Position p){
  34. int d;
  35. d = Math.abs(this.X-(p.X)) + Math.abs(this.Y-(p.Y));
  36. return d;
  37. }
  38. }


 

Code :
  1. public class Vivant {
  2. protected  Position position;
  3. protected String  nom ;
  4. public void vivant() {
  5. this.position =new Position();
  6. }
  7. public Position getPosition() {
  8. return this.position;
  9. }
  10. public String getNom() {
  11. return this.nom;
  12. }
  13. public void setNom(String name) {
  14. this.nom=name;
  15. }
  16. public void affiche() {
  17. int pX =  this.position.getX();
  18. int pY =  this.position.getY();
  19. System.out.println("  nom : "+this.nom+" position x:" +pX+"y:"+pY);
  20. }
  21. }


Code :
  1. public class Animal extends Vivant{
  2. public void seDeplace() {
  3. this.position.setX(this.position.getX()+1);
  4. this.position.setY(this.position.getY()+1);
  5. }
  6. public void mange () {
  7. }
  8. }


 

Code :
  1. public class Herbe extends Vivant{
  2. private static int nbHerbe=0 ;
  3. public void Herbe() {
  4. this.nbHerbe= nbHerbe+1;
  5. }
  6. public int getNbHerbe() {
  7. return this.nbHerbe;
  8. }
  9. }


Code :
  1. public class Loup extends Animal {
  2. private static int nbLoup=0;
  3. public Loup() {
  4. this.nbLoup= nbLoup+1;
  5. }
  6. public int getnbLoup(){
  7. return this.nbLoup;
  8. }
  9. }


Code :
  1. public class Mouton extends Animal{
  2. private static int nbMouton=0;
  3. public void Mouton(){
  4. this.nbMouton= nbMouton+1;
  5. }
  6. public int getNbMouton(){
  7. return this.nbMouton;
  8. }
  9. }


Code :
  1. import java.util.*;
  2. import java.lang.Math;
  3. public class LetsEat
  4. {
  5. public static void main(String[] args) {
  6. int a,i;
  7. int j=0 ,c=0;
  8. //the user is asked to type the number of wolf, sheep and grass
  9. Scanner scl = new Scanner(System.in);
  10. System.out.println("Veuillez saisir le nombre de loups :" );
  11. int l = scl.nextInt();
  12. System.out.println("Vous avez saisi le nombre : \n" + l);
  13. Scanner scm = new Scanner(System.in);
  14. System.out.println("\nVeuillez saisir le nombre de moutons : " );
  15. int m = scm.nextInt();
  16. System.out.println("Vous avez saisi le nombre : \n" + m);
  17. Scanner sch = new Scanner(System.in);
  18. System.out.println("\nVeuillez saisir le nombre d'herbes : " );
  19. int h = sch.nextInt();
  20. System.out.println("Vous avez saisi le nombre : \n" + h);
  21. // a is the number of animals which includes wolves and sheeps  
  22. a= l + m;
  23. //Initialization of an Animal/Herbe array  
  24. Loup tabL[]= new Loup[l];
  25. Mouton tabM[]= new Mouton[m];
  26. Herbe tabH[]= new Herbe[h];
  27. String nomL[]= new String[l];
  28. String nomM[]= new String[m];
  29. String nomH[]= new String[h];
  30. // creation of each instances, l wolves and m sheeps h grasses  
  31. /* for(i=1;i<=l;i++){
  32. tabM[i]= new Loup();
  33. } */
  34. while(j<m){
  35.  tabM[j]= new Mouton();
  36.  nomM[j] = new String("Mouton " +(j+1));
  37.  tabM[j].setNom(nomM[j]);
  38.  tabM[j].affiche();
  39.  tabM[j].Mouton();
  40.  j= tabM[j].getNbMouton();
  41. }
  42. while(c<h) {
  43. tabH[c]= new Herbe();
  44. nomH[c] = new String("Herbe " +(c+1));
  45. tabH[c].vivant();
  46. tabH[c].setNom(nomH[c]);
  47. tabH[c].affiche();
  48. tabH[c].Herbe();
  49. c = tabH[c].getNbHerbe();
  50. }
  51. }
  52. }


Merci pour votre attention,

Reply

Marsh Posté le 15-12-2014 à 19:37:50   

Reply

Marsh Posté le 15-12-2014 à 21:51:10    

Bon j'ai corrigé mes erreurs liées au constructeur mais j'ai un autre problème, mes initialisations de position sont renvoyées nulles ex :

 

Veuillez saisir le nombre de loups :
2
Vous avez saisi le nombre :
2

 

Veuillez saisir le nombre de moutons :
2
Vous avez saisi le nombre :
2

 

Veuillez saisir le nombre d'herbes :
2
Vous avez saisi le nombre :
2
  nom : Mouton 1 position x:0y:0
  nom : Mouton 2 position x:0y:0
  nom : Herbe 1 position x:0y:0
  nom : Herbe 2 position x:0y:0
(j'ai essayé de mettre des super() dans constructeurs filles mais aucun changement notable)
C'EST BON CA MARCHE J'AI TROUVE MON ERREUR MERCI A VOUS !
Merci,


Message édité par darkeen le 15-12-2014 à 22:05:13
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed