Correction TP Java Temps Réel
Correction TP Java Temps Réel
Correction TP Java Temps Réel
Exercice 1 :
public class Compteur extends Thread{
private String nom;
private int N;
public Compteur(String nom,int max){
super();
this.nom=nom;
this.N=max;
}
public void run(){
for(int i=1; i<this.N; i++){
System.out.println(this.nom+": " +i);
try {
Thread.sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println (this.nom+" a termin!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Compteur c1=new Compteur("comp1",20);
Compteur c2=new Compteur("comp2", 10);
c1.start();
c2.start();
try {
c1.join();
c2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Tout est termin.");
}
}
Exercice 2 :
public class counterAZ implements Runnable{
public void run(){
for(char i='a'; i<='z'; i++)
System.out.println("counterAZ: "+i);
}
}
Programme principal:
public class prog {
public static void main(String[] args) {
counterNum c1=new counterNum();
counterAZ c2=new counterAZ();
Thread t1=new Thread(c1);
Thread t2=new Thread(c2);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Correction de Threads.java :
public class Threads extends Thread {
static int j = 1;
public void run() {
for (int i = 1; i<=20; i++) {
System.out.println(j++ + " " + getName()); }
}
public static void main(String[] args) {
Threads t1 = new Threads();
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Threads t2 = new Threads();
t2.start();
}
}
Exercice 3 :
public class compteBC {
private float solde=0;
public float getSolde(){
return this.solde;
}
public void debit(float deb){
System.out.println("dbit de: "+deb );
this.solde=solde-deb;
}
public void credit (float cred){
System.out.println("crdit de: "+cred);
this.solde=solde+cred;
}
}
Correction de OperationsNulles.java: