<java>composition

domxaline Messages postés 204 Statut Membre -  
domxaline Messages postés 204 Statut Membre -
Bonjour,
package javaapplication1;
public class Potpie
{
 private int month;
 private int day;
 private int year;
 public Potpie(int m,int d,int y)
 {
     month=m;
     day=d;
     year=y;
     System.out.printf("The constructor for this is %s\n",this);
 }
 public String toString()
 {
     return String.format("%d/%d/%d",month,day,year);
 }
}
package javaapplication1;
public class Tuna
{
    private String name;
    private Potpie birthday;
    

    public Tuna(String theName,Potpie theDate)
    {
        name=theName;
        birthday=theDate;
    }
    public String toString()
    {
        return String.format("My name is %s","my birthday is %s",name,birthday);
    }
}
package javaapplication1;
public class Apples
{
 public static void main(String[]args)
   {
     Potpie potObject=new Potpie (4,5,6);
     Tuna tunaObject=new Tuna ("Greg",potObject);
     System.out.println(tunaObject);   
   }
)
        


le resutat de mon programe est suivant
The constructor for this is 4/5/6
My name is my birthday is %s

mais je veux que le resultat soit ainsi:
The constructor for this is 4/5/6
My name is Greg,my birthday is 4/5/6

quelqu'un peut m'aider pour trouver mon erreur s'il vous plaît

2 réponses

  1. xav3601 Messages postés 3390 Statut Membre 312
     
    Salut,

    Comment, c'est possible que ca t'écrives ca:
    The constructor for this is 4/5/6

    Alors que tu ne l'affiches jamais!
    Ensuite, tu affiches un objet, ca peut pas marcher! Faut afficher une string...

    Remplace les lignes adequates par:

    birthday=theDate.toString;

    Et

    System.out.println(tunaObject.toString());

    Et pis faut changer ta fonction toString!

    La culture c'est comme la confiture, moins en a plus on l'étale!
    0
    1. domxaline Messages postés 204 Statut Membre 10
       
      package javaapplication1;
      public class Tuna
      {
      private String name;
      private Potpie birthday;
      public Tuna(String theName,Potpie theDate)
      {
      name=theName;
      birthday=theDate.toString;
      }
      public String toString()
      {
      return String.format("My name is %s","my birthday is %s",name,birthday);
      }
      }
      package javaapplication1;
      public class Apples
      {
      public static void main(String[]args)
      {
      Potpie potObject=new Potpie (4,5,6);
      Tuna tunaObject=new Tuna ("creg",potObject);
      System.out.println(tunaObject.toString());
      }
      )

      j'ai corrigé le programme comme tu m'ai dit,maintenant,il affiche les erreur suivantes:
      The constructor for this is 4/5/6
      Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
      symbol: variable toString
      location: class javaapplication1.Potpie
      at javaapplication1.Tuna.<init>(Tuna.java:12)
      at javaapplication1.Apples.main(Apples.java:9)
      0
    2. xav3601 Messages postés 3390 Statut Membre 312
       
      Oui mais en fait faut revoir pas mal de morceau dans ton code!
      Y'a des erreurs un peu partout!
      les %s en java t'oublie par exemple!
      0
    3. domxaline Messages postés 204 Statut Membre 10
       
      ça y est,j'ai trouvé mon erreur
      voilà les codes corrigés:
      package javaapplication1;
      public class Potpie
      {
      private int month;
      private int day;
      private int year;
      public Potpie(int m,int d,int y)
      {
      month=m;
      day=d;
      year=y;
      System.out.printf("The constructor for this is %s\n",this);
      }
      public String toString()
      {
      return String.format("%d/%d/%d",month,day,year);
      }
      }
      package javaapplication1;
      public class Tuna
      {
      private String name;
      private Potpie birthday;


      public Tuna(String theName,Potpie theDate)
      {
      name=theName;
      birthday=theDate;
      }
      public String toString()
      {
      return String.format("My name is %s,my birthday is %s",name,birthday);
      }
      }
      package javaapplication1;

      public class Apples
      {
      public static void main(String[]args)
      {
      Potpie potObject=new Potpie (4,5,6);
      Tuna tunaObject=new Tuna ("Greg",potObject);
      System.out.println(tunaObject);
      }
      )

      merci beaucoup
      0
  2. V3n1 Messages postés 270 Date d'inscription   Statut Membre Dernière intervention   56
     
    Je savais pas que les "%s" , "%d"... fonctionné aussi en Java.

    En tout cas j'ai vraiment du mal avec ton mode de fonctionnement :S
    0
    1. xav3601 Messages postés 3390 Statut Membre 312
       
      J'avais même pas regardé ses fonctions toString()...
      Bref faut tout refaire là :S
      0