<java>composition
domxaline
Messages postés
188
Date d'inscription
Statut
Membre
Dernière intervention
-
domxaline Messages postés 188 Date d'inscription Statut Membre Dernière intervention -
domxaline Messages postés 188 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
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
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:
- <java>composition
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages
- Waptrick java voiture - Télécharger - Jeux vidéo
2 réponses
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!
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!
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)
Y'a des erreurs un peu partout!
les %s en java t'oublie par exemple!
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