# R1.01 : TP5

**Nom :** MAROUSE
**Prénom :** Grégoire
**Groupe :** C1

## Exercice 1
_Code :_
```java
class Compare {
	void principal(){
		testSontTousDiff();
	}
	/**
	* vérifie si deux tableaux n’ont aucune valeur commune
	* @param tab1 premier tableau
	* @param tab2 deuxième tableau
	* @return vrai si les tableaux tab1 et tab2 n’ont aucune valeur commune, faux sinon
	*/
	boolean sontTousDiff (int[] tab1, int[] tab2){
		boolean val = true;
		int i=0; 
		while (i<tab1.length && val){
			for (int j=0; j<tab2.length && val ; j++){
				if (tab1[i] == tab2[j]){
					val = false;
				}
			}
			i++;
		}
		return val;
	}
	/**
	* Teste la méthode sontTousDiff()
	*/
	void testSontTousDiff () {
		System.out.println ();
		System.out.println ("*** testSontTousDiff ");
		testCasSontTousDiff (new int[]{},new int[]{},true);
		testCasSontTousDiff (new int[]{1,2,3,4,5,6},new int[]{},true);
		testCasSontTousDiff (new int[]{1,2,3,4,5,6},new int[]{6,7,8,9,10,11,12,13,14,15},false);
		testCasSontTousDiff (new int[]{1},new int[]{11,1111,11,111,1111111},true);
		testCasSontTousDiff (new int[]{1,2,3},new int[]{1,2,3},false);
		testCasSontTousDiff (new int[]{0,-2,4},new int[]{1,2,-4},true);

	}
	/**
	* teste un appel de sontTousDiff
	* @param t1 tableau d'entiers
	* @param t2 tableau d'entiers
	* @param result le résultat attendu
	*/
	void testCasSontTousDiff (int[] t1,int[] t2, boolean result) {
		// Affichage
		System.out.print ("sontTousDiff (");
		displayTab(t1);
		System.out.print (", ");
		displayTab(t2);
		System.out.print(") \t= " + result + "\t : ");
		// Appel
		boolean resExec = sontTousDiff(t1,t2);
		// Verification
		if (resExec == result){
			System.out.println ("OK");
		} else {
			System.err.println ("ERREUR");
		}
	}
	
	/**
	 * Affiche le tableau t
	 * @param t tableau d'entier
	 */
	void displayTab(int[] t) {
		int i = 0;
		System.out.print("{");
		while(i<t.length-1){
			System.out.print(t[i] + ",");
			i++;
		}
		if(t.length>=1){
			System.out.print(t[i]+"}");
		}else{
			System.out.print("}");
		}
	}
}
```
_Exemple d'exécution_
```
*** testSontTousDiff 
sontTousDiff ({}, {}) 	= true	 : OK
sontTousDiff ({1,2,3,4,5,6}, {}) 	= true	 : OK
sontTousDiff ({1,2,3,4,5,6}, {6,7,8,9,10,11,12,13,14,15}) 	= false	 : OK
sontTousDiff ({1}, {11,1111,11,111,1111111}) 	= true	 : OK
sontTousDiff ({1,2,3}, {1,2,3}) 	= false	 : OK
sontTousDiff ({0,-2,4}, {1,2,-4}) 	= true	 : OK
```

## Exercice 2
_Code :_
```java
class Exo2 {
	void principal(){
		testCompare();
		testDecalerGauche();
		testDecalerGaucheN();
		testIndiceTab();
		testDecaleValeur();
	}

// ########## Décaler ########## 
	/**
	* décale les entiers d’un tableau d’une position vers la gauche
	* L’élément en 0 se retrouve à la fin du tableau
	* @param tab tableau d’entiers
	*/
	void decalerGauche (int[] tab){
		if (tab.length>1){
			int temp = tab[0];
			for (int i=0; i<tab.length-1; i++){
				tab[i] = tab[i+1];
			}
			tab[tab.length-1] = temp;
		}
	}
	/**
	* Teste la méthode decalerGaucheN ()
	*/
	void testDecalerGauche  () {
		System.out.println ();
		System.out.println ("*** testDecalerGauche  ");
		testCasDecalerGauche  (new int[]{3,10,6,20,7},new int[]{10,6,20,7,3});
		testCasDecalerGauche (new int[]{},new int[]{});
		testCasDecalerGauche (new int[]{1,2,1},new int[]{2,1,1});
		testCasDecalerGauche  (new int[]{1,2,3,4,5,6,7},new int[]{2,3,4,5,6,7,1});
		testCasDecalerGauche  (new int[]{1},new int[]{1});
	}
	/**
	* teste un appel de decalerGauche ()
	* @param t tableau d'entiers
	* @param result le résultat attendu
	*/
	void testCasDecalerGauche  (int[] t, int[] result) {
		// Affichage
		System.out.print ("decalerGauche  (");
		displayTab(t);
		System.out.print (") \t= ");
		displayTab(result);
		System.out.print("\t : ");
		// Appel
		decalerGauche(t);
		// Verification
		if (compare(t,result)){
			System.out.println ("OK");
		} else {
			System.err.println ("ERREUR");
			displayTab(t);
		}
	}
	
// ########## Décaler n fois ########## 
	/**
	* décale les entiers d’un tableau de n positions vers la gauche
	* @param tab tableau d’entiers
	* @param n entier nombre de cases à décaler
	*/
	void decalerGaucheN (int[] tab, int n){
		for (int i=0; i<n;i++){
			decalerGauche(tab);
		}
	}
	/**
	* Teste la méthode decalerGaucheN ()
	*/
	void testDecalerGaucheN  () {
		System.out.println ();
		System.out.println ("*** testDecalerGaucheN  ");
		testCasDecalerGaucheN  (new int[]{3,10,6,20,7},3,new int[]{20,7,3,10,6});
		testCasDecalerGaucheN (new int[]{},20,new int[]{});
		testCasDecalerGaucheN  (new int[]{3,10,6,20,7},0,new int[]{3,10,6,20,7});
		testCasDecalerGaucheN  (new int[]{3,5,7},3,new int[]{3,5,7});
		testCasDecalerGaucheN  (new int[]{1,2,3,4,5,6,7},6,new int[]{7,1,2,3,4,5,6});
		testCasDecalerGaucheN  (new int[]{1},5000,new int[]{1});
	}
	/**
	* teste un appel de decalerGaucheN ()
	* @param t tableau d'entiers
	* @param n nombre de décalage
	* @param result le résultat attendu
	*/
	void testCasDecalerGaucheN  (int[] t,int n, int[] result) {
		// Affichage
		System.out.print ("decalerGaucheN  (");
		displayTab(t);
		System.out.print (", "+n+") \t= ");
		displayTab(result);
		System.out.print("\t : ");
		// Appel
		decalerGaucheN (t,n);
		// Verification
		if (compare(t,result)){
			System.out.println ("OK");
		} else {
			System.err.println ("ERREUR");
			displayTab(t);
		}
	}

// ########## Trouver l'indice ########## 
	/**
	* cherche l’indice de la premiere occurrence d’une valeur dans un tableau
	* @param tab tableau d’entiers
	* @param v valeur à chercher
	* @return l’indice de la première valeur v dans tab si v est dans tab, -1 sinon
	*/
	int indiceTab (int[] tab, int v){
		boolean val = false;
		int index = -1;
		int i=0;
		while (i<tab.length && !val){
			if(tab[i] == v){
				val=true;
				index=i;
			}
			i++;
		}
		if (!val) {
			index=-1;
		}
		return index;
	}
	/**
	* Teste la méthode indiceTab()
	*/
	void testIndiceTab () {
		System.out.println ();
		System.out.println ("*** testIndiceTab ");
		testCasIndiceTab (new int[]{3,10,6,20,7},20,3);
		testCasIndiceTab(new int[]{},20,-1);
		testCasIndiceTab(new int[]{3,10,6,20,7},0,-1);
		testCasIndiceTab (new int[]{3,5,7},3,0);
		testCasIndiceTab (new int[]{1,2,3,4,5,6,7},7,6);
	}
	/**
	* teste un appel de indiceTab()
	* @param t tableau d'entiers
	* @param result le résultat attendu
	*/
	void testCasIndiceTab (int[] t,int v, int result) {
		// Affichage
		System.out.print ("indiceTab (");
		displayTab(t);
		System.out.print (", "+v+") \t= "+result+"\t : ");
		// Appel
		int resex = indiceTab(t,v);
		// Verification
		if (resex==result){
			System.out.println ("OK");
		} else {
			System.err.println ("ERREUR");
			displayTab(t);
		}
	}

// ########## Décaler par valeur ########## 
	/**
	* décale les valeurs d’un tableau de manière à ramener la valeur cherchée
	* à l’indice 0
	* Si la valeur n’est pas présente, le tableau n’est pas modifié
	* @param tab tableau d’entiers
	* @param v valeur à chercher
	*/
	void decaleValeur (int[] tab, int v){
		if (indiceTab(tab, v)>=0){
			decalerGaucheN(tab, indiceTab(tab, v));
		}
	}
	/**
	* Teste la méthode decaleValeur()
	*/
	void testDecaleValeur () {
		System.out.println ();
		System.out.println ("*** testDecaleValeur ");
		testCasDecaleValeur (new int[]{3,10,6,20,7},20,new int[]{20,7,3,10,6});
		testCasDecaleValeur (new int[]{},20,new int[]{});
		testCasDecaleValeur (new int[]{3,10,6,20,7},0,new int[]{3,10,6,20,7});
		testCasDecaleValeur (new int[]{3,5,7},3,new int[]{3,5,7});
		testCasDecaleValeur (new int[]{1,2,3,4,5,6,7},7,new int[]{7,1,2,3,4,5,6});
	}
	/**
	* teste un appel de decaleValeur()
	* @param t tableau d'entiers
	* @param v valeur à chercher
	* @param result le résultat attendu
	*/
	void testCasDecaleValeur (int[] t,int v, int[] result) {
		// Affichage
		System.out.print ("decaleValeur (");
		displayTab(t);
		System.out.print (", "+v+") \t= ");
		displayTab(result);
		System.out.print("\t : ");
		// Appel
		decaleValeur(t,v);
		// Verification
		if (compare(t,result)){
			System.out.println ("OK");
		} else {
			System.err.println ("ERREUR");
			displayTab(t);
		}
	}

// ########## Comparison de tableau ########## 
	/**
	* vérifie si deux tableaux sont identiques
	* @param tab1 premier tableau
	* @param tab2 deuxième tableau
	* @return vrai si les tableaux tab1 et tab2 sont identiques, faux sinon
	*/
	boolean compare(int[] tab1, int[] tab2){
		boolean val = true;
		if (tab1.length == tab2.length){
			int i=0;
			while (i<tab1.length && val){
				if (tab1[i] != tab2[i]){
					val = false;
				}
				i++;
			}
		}else {
			val=false;
		}
		return val;
	}
	/**
	* Teste la méthode compare()
	*/
	void testCompare () {
		System.out.println ();
		System.out.println ("*** testCompare ");
		testCasCompare(new int[]{},new int[]{},true);
		testCasCompare (new int[]{},new int[]{1,2,3},false);
		testCasCompare (new int[]{1,2,3},new int[]{1,2,3,4},false);
		testCasCompare (new int[]{0},new int[]{0},true);
		testCasCompare(new int[]{20,7,3,10,6},new int[]{20,7,3,10,6},true);
		testCasCompare (new int[]{0,-2,4},new int[]{1,2,-4},false);

	}
	/**
	* teste un appel de compare
	* @param t1 premier tableau
	* @param t2 deuxième tableau
	* @param result le résultat attendu
	*/
	void testCasCompare (int[] t1,int[] t2, boolean result) {
		// Affichage
		System.out.print ("compare(");
		displayTab(t1);
		System.out.print (", ");
		displayTab(t2);
		System.out.print(") \t= " + result + "\t : ");
		// Appel
		boolean resExec = compare(t1,t2);
		// Verification
		if (resExec == result){
			System.out.println ("OK");
		} else {
			System.err.println ("ERREUR");
		}
	}
	
// ########## Affichage de tableau ########## 
	/**
	 * Affiche le tableau t
	 * @param t tableau d'entier
	 */
	void displayTab(int[] t) {
		int i = 0;
		System.out.print("{");
		while(i<t.length-1){
			System.out.print(t[i] + ",");
			i++;
		}
		if(t.length>=1){
			System.out.print(t[i]+"}");
		}else{
			System.out.print("}");
		}
	}
}
```
_Exemple d'exécution_
```
*** testCompare 
compare({}, {}) 	= true	 : OK
compare({}, {1,2,3}) 	= false	 : OK
compare({1,2,3}, {1,2,3,4}) 	= false	 : OK
compare({0}, {0}) 	= true	 : OK
compare({20,7,3,10,6}, {20,7,3,10,6}) 	= true	 : OK
compare({0,-2,4}, {1,2,-4}) 	= false	 : OK

*** testDecalerGauche  
decalerGauche  ({3,10,6,20,7}) 	= {10,6,20,7,3}	 : OK
decalerGauche  ({}) 	= {}	 : OK
decalerGauche  ({1,2,1}) 	= {2,1,1}	 : OK
decalerGauche  ({1,2,3,4,5,6,7}) 	= {2,3,4,5,6,7,1}	 : OK
decalerGauche  ({1}) 	= {1}	 : OK

*** testDecalerGaucheN  
decalerGaucheN  ({3,10,6,20,7}, 3) 	= {20,7,3,10,6}	 : OK
decalerGaucheN  ({}, 20) 	= {}	 : OK
decalerGaucheN  ({3,10,6,20,7}, 0) 	= {3,10,6,20,7}	 : OK
decalerGaucheN  ({3,5,7}, 3) 	= {3,5,7}	 : OK
decalerGaucheN  ({1,2,3,4,5,6,7}, 6) 	= {7,1,2,3,4,5,6}	 : OK
decalerGaucheN  ({1}, 5000) 	= {1}	 : OK

*** testIndiceTab 
indiceTab ({3,10,6,20,7}, 20) 	= 3	 : OK
indiceTab ({}, 20) 	= -1	 : OK
indiceTab ({3,10,6,20,7}, 0) 	= -1	 : OK
indiceTab ({3,5,7}, 3) 	= 0	 : OK
indiceTab ({1,2,3,4,5,6,7}, 7) 	= 6	 : OK

*** testDecaleValeur 
decaleValeur ({3,10,6,20,7}, 20) 	= {20,7,3,10,6}	 : OK
decaleValeur ({}, 20) 	= {}	 : OK
decaleValeur ({3,10,6,20,7}, 0) 	= {3,10,6,20,7}	 : OK
decaleValeur ({3,5,7}, 3) 	= {3,5,7}	 : OK
decaleValeur ({1,2,3,4,5,6,7}, 7) 	= {7,1,2,3,4,5,6}	 : OK
```

## Exercice 3
_Code :_
```java
class Exo3 {
	void principal(){
		testCompteDiffVal();
	}
	/**
	* compte le nombre de valeurs différentes dans un tableau
	* @param tab tableau d’entiers
	* @return le nombre de valeurs différentes du tableau
	*/
	int compteDiffVal(int[] tab){
		int cpt;
		if (tab.length > 0){
			cpt = 1;
			if (tab.length>1){
				for (int i=0; i<tab.length-1; i++){
					boolean test=true;
					for (int j=i+1; j<tab.length; j++){
						if (tab[i] == tab[j]){
							test =false;
						}
					}
					if (test) {
						cpt+=1;
					}
				}
			}
		}else{
			cpt=0;
		}
		return cpt;
	}
	/**
	* Teste la méthode compteDiffVal()
	*/
	void testCompteDiffVal () {
		System.out.println ();
		System.out.println ("*** testCompteDiffVal ");
		testCasCompteDiffVal (new int[]{},0);
		testCasCompteDiffVal (new int[]{1,1,1,1,1,1,1},1);
		testCasCompteDiffVal (new int[]{1,2,3,4,5,6},6);
		testCasCompteDiffVal (new int[]{1},1);
		testCasCompteDiffVal (new int[]{0,0,2,3,0,2,1,3,3,0},4);
		testCasCompteDiffVal (new int[]{0,-2,4,2,-4,-0},5);
	}
	/**
	* teste un appel de compteDiffVal()
	* @param t tableau d'entiers
	* @param result le résultat attendu
	*/
	void testCasCompteDiffVal (int[] t, int result) {
		// Affichage
		System.out.print ("compteDiffVal (");
		displayTab(t);
		System.out.print (") \t= " + result + "\t : ");
		// Appel
		int resExec = compteDiffVal(t);
		// Verification
		if (resExec == result){
			System.out.println ("OK");
		} else {
			System.err.println ("ERREUR");
		}
	}
	
	/**
	 * Affiche le tableau t
	 * @param t tableau d'entier
	 */
	void displayTab(int[] t) {
		int i = 0;
		System.out.print("{");
		while(i<t.length-1){
			System.out.print(t[i] + ",");
			i++;
		}
		if(t.length>=1){
			System.out.print(t[i]+"}");
		}else{
			System.out.print("}");
		}
	}
}
```
_Exemple d'exécution_
```
*** testCompteDiffVal 
compteDiffVal ({}) 	= 0	 : OK
compteDiffVal ({1,1,1,1,1,1,1}) 	= 1	 : OK
compteDiffVal ({1,2,3,4,5,6}) 	= 6	 : OK
compteDiffVal ({1}) 	= 1	 : OK
compteDiffVal ({0,0,2,3,0,2,1,3,3,0}) 	= 4	 : OK
compteDiffVal ({0,-2,4,2,-4,0}) 	= 5	 : OK
```

## Exercice 4
_Code :_
```java
class Exo4 {
	void principal(){
		testEstSousChaine();
	}
	
	/**
	* teste si une chaîne est une sous-chaîne d’une autre
	* @param mot chaîne de caractères
	* @param phrase chaîne de carectères
	* @return vrai ssi mot est présent dans phrase
	*/
	boolean estSousChaine (String mot, String phrase){
		boolean sousChaine = false;
		int i=0;
		while (i<phrase.length() && !sousChaine){
			int car =0;
			if (mot.length()>0 && phrase.charAt(i)==mot.charAt(0)){
				for(int j=0;j<mot.length()&&j+i<phrase.length();j++){
					if (phrase.charAt(i+j)==mot.charAt(j)){
						car+=1;
					}
				}
			}
			if (car==mot.length()){
					sousChaine=true;
			}
			i++;
		}
		}
		return sousChaine;
	}
	
	/**
	* Teste la méthode estSousChaine()
	*/
	void testEstSousChaine(){
		System.out.println ();
		System.out.println ("*** testEstSousChaine ");
		testCasEstSousChaine ("ses","abcdsesdef",true);
		testCasEstSousChaine ("ses","abcdefse",false);
		testCasEstSousChaine ("ses","abcdef",false);
		testCasEstSousChaine ("","abcd",true);
		testCasEstSousChaine ("a","",false);
		testCasEstSousChaine ("f","abcdef",true);
		testCasEstSousChaine ("aaa","zyxwvutsrqponmlkjihgfedbcaa",false);
		testCasEstSousChaine ("aaa","	@~#^{`[aaa|~['('_(ÈÇ')=)=	",true);
		testCasEstSousChaine (" ","	",false); //phrase == tabulation
		testCasEstSousChaine (" ","   ",true); //phrase == plusieurs espace
	}
	/**
	* teste un appel de estSousChaine()
	* @param mot chaîne de caractères
	* @param phrase chaîne de carectères
	* @param result le résultat attendu
	*/
	void testCasEstSousChaine (String mot, String phrase, boolean result) {
		// Affichage
		System.out.print ("estSousChaine (\""+mot+"\", \""+phrase+"\") \t= " + result + "\t : ");
		// Appel
		boolean resExec = estSousChaine(mot,phrase);
		// Verification
		if (resExec == result){
			System.out.println ("OK");
		} else {
			System.err.println ("ERREUR");
		}
	}
	
	/**
	 * Affiche le tableau t
	 * @param t tableau d'entier
	 */
	void displayTab(int[] t) {
		int i = 0;
		System.out.print("{");
		while(i<t.length-1){
			System.out.print(t[i] + ",");
			i++;
		}
		if(t.length>=1){
			System.out.print(t[i]+"}");
		}else{
			System.out.print("}");
		}
	}
}
```
_Exemple d'exécution_
```
*** testEstSousChaine 
estSousChaine ("ses", "abcdsesdef") 	= true	 : OK
estSousChaine ("ses", "abcdefse") 	= false	 : OK
estSousChaine ("ses", "abcdef") 	= false	 : OK
estSousChaine ("", "abcd") 	= true	 : OK
estSousChaine ("a", "") 	= false	 : OK
estSousChaine ("f", "abcdef") 	= true	 : OK
estSousChaine ("aaa", "zyxwvutsrqponmlkjihgfedbcaa") 	= false	 : OK
estSousChaine ("aaa", "	@~#^{`[aaa|~['('_(ÈÇ')=)=	") 	= true	 : OK
estSousChaine (" ", "	") 	= false	 : OK
estSousChaine (" ", "   ") 	= true	 : OK
```

## Exercice 5
_Code :_
```java
class Exo5 {
	final int LG_TAB = 10 ;
	void principal(){
		saisirEtTrier();
	}
	
	/**
	* Crée et saisit un tableau trié de LG_TAB entiers
	* @return tableau trié de LG-TAB entiers
	*/
	int[] saisirEtTrier () {
		int[] t = new int[LG_TAB];
		int k = 0;
		while (k < t.length) {
			t[k] = SimpleInput.getInt ("Entrer un entier : ");
			for (int i = 0; i <=k; i++){
               int index = i;  
               for (int j = i + 1; j <=k; j++){
                    if (t[j] < t[index]){ 
                        index = j;
                    }
                }
                int min = t[index];
                t[index] = t[i]; 
                t[i] = min;
            }
			k += 1;
			displayTab(t);
		}
		return t;
	}
	
	/**
	 * Affiche le tableau t
	 * @param t tableau d'entier
	 */
	void displayTab(int[] t) {
		int i = 0;
		System.out.print("{");
		while(i<t.length-1){
			System.out.print(t[i] + ",");
			i++;
		}
		if(t.length>=1){
			System.out.println(t[i]+"}");
		}else{
			System.out.println("}");
		}
	}
}
```
_Exemple d'exécution_
```
Entrer un entier : 1
{1,0,0,0,0,0,0,0,0,0}
Entrer un entier : 2
{1,2,0,0,0,0,0,0,0,0}
Entrer un entier : 0
{0,1,2,0,0,0,0,0,0,0}
Entrer un entier : -5
{-5,0,1,2,0,0,0,0,0,0}
Entrer un entier : 8
{-5,0,1,2,8,0,0,0,0,0}
Entrer un entier : -1
{-5,-1,0,1,2,8,0,0,0,0}
Entrer un entier : 0
{-5,-1,0,0,1,2,8,0,0,0}
Entrer un entier : 3
{-5,-1,0,0,1,2,3,8,0,0}
Entrer un entier : -0
{-5,-1,0,0,0,1,2,3,8,0}
Entrer un entier : 2
{-5,-1,0,0,0,1,2,2,3,8}
```