Correction TD2
Exercice 1
Écrire un programme qui permet de rechercher un entier dans un tableau. Si l’entier est trouvé, le
programme retourne la première position où se trouve l’entier. Sinon le programme lève une exception
NoSuchElementException.
Solution :
import java.util.NoSuchElementException;
public class RechercherEntierTabException {
public static void rechFonction(int x,int t[] ){
int i, position=-1;
for(i =0; (i<t.length && position==-1); i++)
if(t[i]==x)
position=i;
if(position!=-1)
System.out.println("La valeur ("+x +") se trouve à la
position : "+i);
else throw new NoSuchElementException();
}
public static void main(String[] args) throws NoSuchElementException
{
int n = 3; int tab[]={1,0,8,9,3,8,8,12,4,2};
try{
rechFonction(n, tab);
}catch(NoSuchElementException e)
{ e.printStackTrace();
System.out.println("La valeur : ("+n +") recherchée ne se
trouve pas :)");
}
}
}
Exercice 2
Soit le programme suivant qui calcule la factorielle d’un entier passé comme paramètre à travers la
ligne de commande :
public class TestFact {
public static void main( String[] args){
int n=integer.parseInt(args[0]);
if(n==0)
System.out.println ("La factorielle de" +n+"est: 1");
else {
int f = 1;
for(int i=2;i<= n ;i++)
f = f*i;
System.out.println ("La factorielle de" +n+"est:"+f);
}
}
}
1/3
1) Reprendre ce programme en attrapant les différentes exceptions de façons à préciser la difficulté à
l’utilisateur, lorsque le calcul de factorielle est impossible.
Les exceptions à prévoir sont :
Il n’y pas des paramètres sur la ligne de commande
Le paramètre n’est pas entier.
Le paramètre est négatif.
Le paramètre est trop grand pour ce programme.
Indications :
- Si le args[0] n’est pas un entier la méthode parseInt lance une exception instance de
NumberFormatException( du package java.lang).
- Si l’accès à un tableau se fait avec un indice erroné (indice négatif ou supérieur à la taille du
tableau) une exception instance de ArrayIndexOutOfBoundsException (du package
java.lang) est lancée.
2) Modifier le programme pour calculer et afficher la factorielle d'un entier entré au clavier.
Solution :
import java.io.*;
class Factorielle
{
public static void main(String[] argv) throws IOException
{
int i, n, factorielle = 1;
System.out.println("Indiquez l'entier dont on souhaite la factorielle "
+ "sur la ligne de commande");
BufferedReader a = new BufferedReader(new
InputStreamReader(System.in));
String s = a.readLine();
try
{
n = Integer.parseInt(s);
if (n < 0) throw new ExceptionNegatif(n);
for (i = 2; i <= n; i++)
{
if (factorielle > Integer.MAX_VALUE / i) throw new
ExceptionGrand (n + " est trop grand pour ce programme,\n");
factorielle *= i;
}
System.out.println("La factorielle (" + n + " ) est : " +
factorielle);
} catch (ArrayIndexOutOfBoundsException exc)
{
System.out.println("Indiquez l'entier dont on souhaite la
factorielle " + "sur la ligne de commande");
}
catch(NumberFormatException exc)
{
System.out.println("L'argument doit être entier");
}
catch(ExceptionNegatif exc)
2/3
{
System.out.println(exc + " : la factorielle n'est pas
définie");
}
catch(ExceptionGrand exc)
{
System.out.println(exc);
}
}
}
---------------------------------------------------------------------------
---
class ExceptionNegatif extends Exception
{
int valeur;
ExceptionNegatif(int val)
{
valeur = val;
}
public String toString()
{
return valeur + " est négatif";
}
}
---------------------------------------------------------------------------
---
class ExceptionGrand extends Exception
{
ExceptionGrand(String s)
{
super(s);
}
}
3/3