<java>composition

Fermé
domxaline Messages postés 188 Date d'inscription lundi 16 mai 2005 Statut Membre Dernière intervention 7 mars 2018 - 3 juin 2010 à 09:42
domxaline Messages postés 188 Date d'inscription lundi 16 mai 2005 Statut Membre Dernière intervention 7 mars 2018 - 3 juin 2010 à 10:58
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





A voir également:

2 réponses

xav3601 Messages postés 3288 Date d'inscription lundi 10 novembre 2008 Statut Membre Dernière intervention 2 mars 2016 311
Modifié par xav3601 le 3/06/2010 à 10:00
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
domxaline Messages postés 188 Date d'inscription lundi 16 mai 2005 Statut Membre Dernière intervention 7 mars 2018 10
3 juin 2010 à 10:49
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
xav3601 Messages postés 3288 Date d'inscription lundi 10 novembre 2008 Statut Membre Dernière intervention 2 mars 2016 311
3 juin 2010 à 10:54
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
domxaline Messages postés 188 Date d'inscription lundi 16 mai 2005 Statut Membre Dernière intervention 7 mars 2018 10
3 juin 2010 à 10:58
ç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
V3n1 Messages postés 294 Date d'inscription vendredi 30 mai 2008 Statut Membre Dernière intervention 12 décembre 2010 56
3 juin 2010 à 09:57
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
xav3601 Messages postés 3288 Date d'inscription lundi 10 novembre 2008 Statut Membre Dernière intervention 2 mars 2016 311
3 juin 2010 à 09:59
J'avais même pas regardé ses fonctions toString()...
Bref faut tout refaire là :S
0