Tableau avec object

Fermé
domxaline Messages postés 188 Date d'inscription lundi 16 mai 2005 Statut Membre Dernière intervention 7 mars 2018 - Modifié par domxaline le 11/09/2016 à 19:07
 domxaline - 14 sept. 2016 à 11:06
Bonjour,
je suis perdu avec mon tableau objet;
voilà je dois faire 2 méthode dans une classe
1.saisi
2.saisiTaille // celui ci augmente la taille fur et à mesure

et je dois faire une autre classe avec la main dans la quelle on doit avoir:
toto.tab[5]=1; // entrer une valeur dans l'indice [5] du tableau
toto.taille++ //augmente la taille tableau au fur à mesure.

quelqu'un peut m'expliquer que signifie ces paramètres
paramètre du 1er méthode S : tabvar : TtabVar, E : n : entier
paramètre du 2eme méthode S tabvar : TtabVar

Ceci l'algorithme de ma classe

Action saisiTtabVar(S : tabvar : TtabVar, E : n : entier)
Var : i : entier
Début
    pour i de 0 à n-1 faire
           lire(tabVar.tab[i]
    Tabvar.taille=n;
Fin

Action saisieTtabVar(S tabvar : TtabVar)
reponse chaine de caractère
Début
        Tabvar.taille=0
        do
            écrire(voulez vous entrer un entier?)
            lire reponse
            si reponse!=non alors
              lire(tabvar.tab[tabvar.taille])
              Tabvar.taille++
          while(reponse==non ou tabvar.taille=max
Fin

voilà la programme que j'avais écrit, je sais que j'avais fait n'importe quoi, ce pour ça je vous demande un coup de main svp
import java.util.Scanner;
public class TtabVar
{
private static final int MAX = 5;
//final int  Max=5;
 static int[]tabX;
  static int tailleX;
  public void initialiser(int []tab,int taille)
  {
      taille=tailleX;
      tab=tabX;
  }
  public void saisi(int n,int[] tab)
  {
      tab=n;
      for(int i=0;i<tab.length-1;i++)
      {
          System.out.println(TtabVar.tabX[i]);
          TtabVar.tailleX=n;
      }
  }
  public void SaisiTaile()
  {
      //String reponse="non";
      TtabVar.tailleX=0;
      String reponse="non";
      do
      {
         Scanner sc=new Scanner(System.in);
         System.out.println("Voulez vous entrer un entier ?");
         reponse=sc.nextLine();
          if(reponse!="non")
          {
              System.out.println(TtabVar.tabX[TtabVar.tailleX]);
              TtabVar .tailleX++;
          }
      }while((reponse=="non") ||(TtabVar.tailleX==MAX) );
  }
}

public class Test_TtabVar
{
    public static void main(String[] args)
    {
      TtabVar toto=new TtabVar();
      toto.saisi();
      toto.SaisiTaile();
    }
}
A voir également:

4 réponses

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
11 sept. 2016 à 18:50
Bonjour,

Dans les conventions utilisés pour cet algorithme, E représente une Entrée et S une sortie (on pourrait également avoir ES).

Donc
saisiTtabVar(S tabvar : TtabVar, E n : entier)
est une fonction qui prend en entrée un entier "n" et donne en sortie un "TtabVar" tabvar.

Quant à
saisieTtabVar(S tabvar : TtabVar)
il donne également en sortie un "TtabVar" tabvar, mais sans avoir besoin de l'entier "n" en entrée.

En Java cela se traduirait par des méthodes
TtabVar saisiTtabVar(int n)
et
TtabVar saisiTtabVar()
où TtabVar est une classe à définir.
0
domxaline Messages postés 188 Date d'inscription lundi 16 mai 2005 Statut Membre Dernière intervention 7 mars 2018 10
11 sept. 2016 à 19:11
ok, merci pour votre aide
maintenant pouvez vous corriger le programme que j'avais écrit svp
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015 > domxaline Messages postés 188 Date d'inscription lundi 16 mai 2005 Statut Membre Dernière intervention 7 mars 2018
11 sept. 2016 à 19:17
Commences déjà par le modifier en prenant en compte ce que signifiait l'algo...
0
domxaline Messages postés 188 Date d'inscription lundi 16 mai 2005 Statut Membre Dernière intervention 7 mars 2018 10
11 sept. 2016 à 21:31
modifier bien sur,
je vous ai dit que je suis complètement perdu avec objet, tableau et ses paramètres. mon problème est là
0
j'ai première parti corrigé moi est ce que c'est bien ou pas

public class TabVar
{
public static int[] affiche(int[] toto)
{
for(int i=0;i<toto.length;i++)
{
System.out.print(toto[i]+", ");
}
return null;
}
}
public class TestTabVar
{
public static void main(String[] args)
{
int toto[]={1,2,3,4,5};
System.out.println(TabVar.affiche(toto));
toto[4]=1;
System.out.println(TabVar.affiche(toto));
}
}

mon ouput est:
1, 2, 3, 4, 5, null
1, 2, 3, 4, 1, null

comment enlever ce null, si j'enleve return il donne une erreur
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
12 sept. 2016 à 18:48
Tu fais
System.out.println(TabVar.affiche(toto));
or
TabVar.affiche
retourne null, donc ça affiche null... C'est normal !

Cependant je doute que ton exercice te demande de créer une méthode
int[] affiche(int[] toto)
. Peut être que si tu suivais les consignes de l'énoncé (à défaut de comprendre ce que tu fais) tu n'aurais pas ce genre de bugs.
0
j'ai récrit mon programme
maintenant j'ai erreur suivante:
1, 2, 3, 4, 5, null
1, 2, 3, 4, 1, null
Voulez vous entrer un entier ?
oui
Exception in thread "main" java.lang.NullPointerException
at TabVar.afficheTaille(TabVar.java:33)
at TestTabVar.main(TestTabVar.java:10)

voilà mon programme:
import java.util.Scanner;
public class TabVar 
{
	static int taille;
	static int[]tab;
	static int[]tabX;
	static final int max=5;
	public static void initialiser(int tailleX,int[]tabX)
	{
		taille = tailleX;
		tab = tabX;
	}
	public static int[] affiche(int[] toto) 
	{
		for(int i=0;i<toto.length;i++)
		{
			System.out.print(toto[i]+", ");
		}
		return null;
	}
	public static int[] afficheTaille(int taille) 
	{
		TabVar.taille=0;
		String reponse="non";
		
		do
		{
			Scanner sc=new Scanner(System.in);
			System.out.println("Voulez vous entrer un entier ?");
			reponse=sc.nextLine();
			if(reponse!="non")
			{
				System.out.println(TabVar.tab[TabVar.taille]);
				TabVar.taille++;
			}
		}while((reponse=="non")||(TabVar.taille==max));
		return null;
	}
}

0
bonjour j'ai corrigé la première méthode de mon programme
pour la deuxième méthode afficheTaille donne une erreur message
voilà ce message :
1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 1,
Voulez vous entrer un entier ?
oui
Exception in thread "main" java.lang.NullPointerException
at TabVar.afficheTaille(TabVar.java:36)
at TestTabVar.main(TestTabVar.java:12)

voilà mon programme:
import java.util.Scanner;
public class TabVar 
{
	static int taille;
    static int[]tab;
    static int[]tabX;
	private static char[][] toto;
    static final int max=5;

    public static void affiche(int[]toto)
    {
        for(int i=0;i<toto.length;i++)
        {
            System.out.print(toto[i]+", ");
        }
        System.out.println();
    }
    
    public static void afficheTaille(int[]toto,int taille)
    {
        TabVar.taille=0;
        String reponse="non";
         
        do
        {
            Scanner sc=new Scanner(System.in);
            System.out.println("Voulez vous entrer un entier ?");
            reponse=sc.nextLine();
            if(reponse!="non")
            {
                System.out.println(TabVar.toto[TabVar.taille]);
                TabVar.taille++;
            }
        }while((reponse=="non")||(TabVar.taille<max));
    }
}

public class TestTabVar 
{
	public static void main(String[] args) 
	{
		int taille=0;
		int toto[]={1,2,3,4,5,6};
		TabVar.affiche(toto);
		toto[5]=1;
		TabVar.affiche(toto);
		TabVar.afficheTaille(toto,taille);
	}
}
0