0% found this document useful (0 votes)
34 views4 pages

TP 5

The Triable interface defines methods for sorting objects: exchange to swap two elements, plusGrand to compare elements, and taille to get the size. The EntierTriable and Dictionnaire classes implement Triable to make arrays of integers and strings sortable. The triBulles static method in Triable uses a bubble sort algorithm to sort any Triable object. The Test class demonstrates sorting EntierTriable and Dictionnaire instances.

Uploaded by

imen gueddess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

TP 5

The Triable interface defines methods for sorting objects: exchange to swap two elements, plusGrand to compare elements, and taille to get the size. The EntierTriable and Dictionnaire classes implement Triable to make arrays of integers and strings sortable. The triBulles static method in Triable uses a bubble sort algorithm to sort any Triable object. The Test class demonstrates sorting EntierTriable and Dictionnaire instances.

Uploaded by

imen gueddess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//Triable.

java
public interface Triable {
void echange(int i, int j);

boolean plusGrand(int i, int j);

int taille();

static void triBulles(Triable T) {


boolean change = false;
do {
change = false;
for (int i = 0; i < T.taille() - 1; i++) {
if (T.plusGrand(i, i + 1)) {
T.echange(i, i + 1);
change = true;
}
}
} while (change);
}
}

//EntierTriable.java
import java.util.Scanner;

public class EntierTriable implements Triable {


public int taille_tab ;
int [] T;

@Override public void echange(int i, int j)


{
int tmp;
tmp = T[j];
T[j] = T[i];
T[i] = tmp;
}

@Override public boolean plusGrand(int i, int j)


{
return (T[j] < T[i]);
}
@Override public int taille()
{
return (this.taille_tab);
}

public EntierTriable(int x)
{

taille_tab = x;
T = new int[taille_tab];
for (int i = 0; i < taille_tab; i++)
{
Scanner s = new Scanner(System.in);
System.out.println("T[" + i + "]= ");
T[i] = s.nextInt();
}
}

public String toString()


{
String s = "";
for (int i = 0; i < taille_tab; i++)
{
s += T[i] + " ";
}
return s;
}
}

//Dictionnaire.java
import java.util.Scanner;
public class Dictionnaire implements Triable{
public int taille_tab ;
String [] T;

@Override public void echange(int i, int j)


{
String tmp;
tmp = T[j];
T[j] = T[i];
T[i] = tmp;
}

@Override public boolean plusGrand(int i, int j)


{
return (T[j].compareTo(T[i])<0);
}

@Override public int taille()


{
return (this.taille_tab);
}

public Dictionnaire(int x)
{

taille_tab = x;
T = new String[taille_tab];
for (int i = 0; i < taille_tab; i++)
{
Scanner s = new Scanner(System.in);
System.out.println("T[" + i + "]= ");
T[i] = s.nextLine();
}
}

public String toString()


{
String s = "";
for (int i = 0; i < taille_tab; i++)
{
s += T[i] + " ";
}
return s;
}
}
//Test.java
public class Test {
public static void main(String[] args)
{
EntierTriable t = new EntierTriable(3);
Triable.triBulles(t);
System.out.println(t);
Dictionnaire t1 = new Dictionnaire(3);
Triable.triBulles(t1);
System.out.println(t1);
}
}

You might also like