TD N4:
//exo1
public class banque {
private int Ncompte;
private double solde;
private String CIN;
public banque(int Ncompte,double solde,String CIN){
[Link]=Ncompte;
[Link]=solde;
[Link]=CIN;
public void deposer(double somme){
solde+=somme;
public void retirer (double somme){
if(solde>=somme){
solde=solde-somme;
else {
[Link](" Retrait impossible");}
public double avoirSolde(){
return solde;
public String avoirInf(){
return ("Ncompte"+Ncompte+" CIN "+CIN);
public static void main(String[] args) {
banque compte= new banque(1, 5000.75, "AB1200");
[Link]([Link]());
[Link]([Link]());
[Link](500);
[Link]([Link]());
[Link](200);
[Link]([Link]());
[Link]([Link]());
//exo3
abstract class figure_2pts{
protected double xa,ya,xb,yb;
abstract void calcul_perimetre();
abstract void calcul_surface();
public figure_2pts(double xxa,double yya,double xxb,double yyb){
xa = xxa;
ya = yya;
xb = xxb;
yb = yyb;
public void affiche(){
[Link]("point a = "+xa+""+ya+"point b = "+xb+""+yb);}
class rectangle extends figure_2pts {
private double l1,l2;
public rectangle(double xxa, double yya, double xxb, double yyb) {
super(xxa, yya, xxb, yyb);
l1 =[Link](xb-xa);
l2 =[Link](xb-xa);}
public void calcul_perimetre(){
double per=l1*2+l2*2;
[Link]("le perimetre de rectengle est: "+per);
public void calcul_surface(){
double surf=l1*l2;
[Link]("la surface de rectengle est: "+surf);}
public static void main(String[] args) {
rectangle rct=new rectangle(2, 01, 03, 03);
rct.calcul_perimetre();
rct.calcul_surface();
[Link](); }}
//exo5
class Pers_admin implements itf_calcul {
private String nom,specialite;
private int age,indice;
private double prime;
public Pers_admin(String n,String s,int a,int i){
nom=n;
specialite=s;
age=a;
indice=i;
public void calcul_prime(){
prime=age*valeur[indice];
[Link]("la prime est "+prime);
public void affiche(){
[Link]("le nom est "+nom+",l'age: "+age+", la specialite est "+specialite);
}}
//exo5
public interface itf_calcul {
double[]valeur={33.2,13.2,5.8};
public void calcul_prime();
public class test5 {
public static void main(String[] args) {
Pers_admin p1=new Pers_admin("chaima", "RT", 20, 1);
p1.calcul_prime();
[Link]();}}
TD N5:
//ex1:
public class exo1{
public static void main(String[] args) {
try{
int x=10/0;
[Link]("x="+x);}
catch(Exception e){
[Link]("Exception"+e);
[Link]();
int x=10/1;
[Link]("x="+x);
[Link]("fin de main");}
}}
//exo2 1
public class EntNat{
private int n;
public EntNat (int n) throws ErrConst{
if(n<0) throw new ErrConst(null);
this.n=n;
public int getN(){
return n;
}}
// ex2 2
class ErrConst extends Exception{
public ErrConst(String String){ }}
//exo2 3
public class test_EntNat{
public static void main(String[] args) {
try{
EntNat n1=new EntNat(5);
[Link]("n="+[Link]());
EntNat n2=new EntNat(-7);
[Link]("n="+[Link]());
catch(ErrConst e){
[Link]("Erreur construction ");}}}
//exo3 1
public class ville {
private String nomVille,nomPays;
private int nbrHabitants;
private char categorie;
public ville(String pNom,int pNbr,String pPays) throws ErrCons{
if(pNbr<0) throw new ErrCons("Vous avez donne un nombre negatif");
[Link]("Creation d'une ville avec des parametres!");
nomVille=pNom;
nbrHabitants=pNbr;
nomPays=pPays;
[Link]();
public void setNombreHabitants(int nbre) throws ErrCons{
if(nbre<0) throw new ErrCons("Vous avez donne un nombre negatif");
nbrHabitants=nbre;
[Link]();
//definir la categorie de la ville
private void setCategorie(){
int borneSup[]={0,1000,10000,100000,500000,1000000,5000000,10000000};
char categorie[]={'?','A','B','C','D','E','F','G','H'};
int i=0;
while(i<[Link] && [Link] >= borneSup[i])
i++;
[Link] = categorie[i];}
public String affiche(){
return "\t"+[Link]+" est une ville de "+[Link]+" ,elle comporte :"
+[Link]+" habitants => elle est donc de categorie :"+[Link];
class ErrCons extends Exception{
ErrCons(String msg)
{super(msg);}
public static void main(String[] args) {
try{
ville v1=new ville("oran", 10, "algerie");
[Link](-2000);
catch (ErrCons e){
[Link]([Link]()); }}}
(JE N'AI PAS LA SUITE)
//exo4
public class finall{
//private static Exception now;
public static void f(int n){
try{
if(n!=1) throw new Except ();
catch(Except e){
[Link]("catch dans f-n=" +n);
return;
finally{
[Link]("dans finally-n= "+n);
public static void main(String[] args) {
f(1);
f(2);}}
//exo 4
public class Except extends Exception{}
TD N6:
//exo1
import [Link].*;
import [Link].*;
public class MaFenetre extends JFrame implements MouseListener {
public MaFenetre(){
setTitle("Gestion de clics");
setBounds(20,20,400,200);
addMouseListener(this);
public void mouseClicked(MouseEvent ev){} // ki tcliki
public void mousePressed(MouseEvent ev){} // ki tcliki w tmchi
public void mouserReleased(MouseEvent ev){} // ki tdorki w ttlgui
public void mouseEntered(MouseEvent ev){}
public void mouseExited(MouseEvent ev){}
@Override // erreur dans class name???
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'mouseReleased'");
public static void main(String[] args) {
MaFenetre fen=new MaFenetre();
[Link](true);
}
//ex2
import [Link].*;
import [Link].*;
public class exo2 extends JFrame implements MouseListener {
public exo2() {
setTitle("Gestion de clics");
setBounds(10,20,300,200);
addMouseListener(this);
public void mouseClicked(MouseEvent ev){}
public void mousePressed(MouseEvent ev) {
[Link]("appui en "+ [Link]()+" "+[Link]());
public void mouseReleased(MouseEvent ev) {
[Link]("relachement "+[Link]()+" "+[Link]());}
public void mouseEntered(MouseEvent ev) {}
public void mouseExited(MouseEvent ev) {}
public static void main(String[] args) {
exo2 f=new exo2();
[Link](true);
}}
//exo 2 partie 2
import [Link].*;
import [Link].*;
public class exo22 extends JFrame {
public exo22() {
setTitle("Gestion de clics");
setBounds(10,20,300,200);
addMouseListener(new Ecouteur());
}}
class Ecouteur implements MouseListener{
public void mouseClicked(MouseEvent ev){}
public void mousePressed(MouseEvent ev) {
[Link]("appui en "+ [Link]()+" "+[Link]());
public void mouseReleased(MouseEvent ev) {
[Link]("relachement "+[Link]()+" "+[Link]());}
public void mouseEntered(MouseEvent ev) {}
public void mouseExited(MouseEvent ev) {}
public static void main(String[] args) {
exo2 f=new exo2();
[Link](true);
//ex3 partie 1 M1
import [Link].*;
import [Link].*;
public class exo3 extends JFrame implements MouseListener{
private int num;
private static int nbFen=0;
public exo3(){
nbFen++;
num=nbFen;
setTitle("fenetre "+num);
setBounds(10,20,300,200);
addMouseListener(this);
public void mouseClicked(MouseEvent ev){}
public void mousePressed(MouseEvent ev) {
[Link]("appui en "+ [Link]()+" "+[Link]());}
public void mouseReleased(MouseEvent ev) {
[Link]("relachement "+[Link]()+" "+[Link]());}
public void mouseEntered(MouseEvent ev) {}
public void mouseExited(MouseEvent ev) {}
public static void main(String[] args) {
for(int i=1;i<5;i++){
exo3 f=new exo3();
[Link](true);}}}
//exo3 p2
import [Link].*;
import [Link].*;
public class exo3q2 extends JFrame {
int i;
public exo3q2(int i){
this.i=i;
setTitle("fenetre "+i);
setBounds(10,20,300,200);
addMouseListener(new ecout(i));}
class ecout extends MouseAdapter{
int i;
public ecout(int i){
this.i=i;}
public void mousePressed(MouseEvent ev){
[Link]("appui en "+[Link]()+" "+[Link]());
public void mouseReleased(MouseEvent ev){
[Link]("relachement "+[Link]()+" "+[Link]());
}}
//exo4
import [Link].*;
import [Link].*;
import [Link].*;
public class fen2boutons extends JFrame implements ActionListener{
public fen2boutons(){
setTitle("Avec deux boutons");
setSize(200,100);
monBouton1 = new JButton("B1");
monBouton2=new JButton("B2");
monBouton3=new JButton("B3");
Container contenu = getContentPane();
[Link](new FlowLayout());//decposition des composentes dans la fenetre
[Link](monBouton1);
[Link](monBouton2);
[Link](monBouton3);
[Link](this);
[Link](this);
[Link](this);}
public void actionPerformed (ActionEvent ev) {
if([Link]()==monBouton1)
[Link]("action sur bouton B1 ");
if([Link]()==monBouton2)
[Link]("action sur bouton B2");
if([Link]()==monBouton3)
[Link]("action sur bouton B3 ");
public static void main(String[] args) {
fen2boutons fen=new fen2boutons();
[Link](true);
private JButton monBouton1, monBouton2 ,monBouton3; }
//exo5
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
public class tirage extends JFrame implements ActionListener {
private JTextField t1,t2,t3,g;
private JButton t;
private JLabel r;
private JPanel p1,p2;
public tirage(){
setTitle("Jeu 3 nbrs");
setSize(350,150);
Container cn=getContentPane();
[Link](new FlowLayout());
//Creation du textF
p1=new JPanel();
[Link](p1);
t1=new JTextField(6);
[Link](t1);
[Link](this);
t2=new JTextField(6);
[Link](t2);
[Link](this);
t3=new JTextField(6);
[Link](t3);
[Link](this);
// button et res
p2=new JPanel();
[Link](p2);
t=new JButton("Tirage");
[Link](t);
[Link](this);
r=new JLabel("Resultat");
[Link](r);
g=new JTextField(10);
[Link](g);
[Link](this);
public void actionPerformed(ActionEvent ev){
Object source=[Link]();
if(source==t){
Random rn=new Random();
int n1=[Link](10);
int n2=[Link](10);
int n3=[Link](10);
//2eme methode pour le Random
/*int n1=(int) ([Link]()*10);
int n2=(int) ([Link]()*10);
int n3=(int) ([Link]()*10);*/
[Link]([Link](n1)); // pour afficher le nbr choisi par random dans le textF
[Link]([Link](n2));
[Link]([Link](n3));
int s=n1+n2+n3;
if(s>15) [Link]("GAGNE");
else [Link]("PERDU");
public static void main(String[] args) {
tirage ti=new tirage();
[Link](true);
TD N7:
//ex1
import [Link].* ;
import [Link].* ;
import [Link].* ;
public class ex1 extends JFrame implements ActionListener, ItemListener{
public ex1(){
setTitle ("Cases a cocher") ;
setSize (300, 140) ;
Container contenu = getContentPane () ;
// les deux boutons
boutRaz = new JButton ("RAZ") ;
[Link] (this) ;
[Link] (boutRaz, "North") ;
boutEtat = new JButton ("Etat") ;
[Link] (this) ;
[Link] (boutEtat, "South") ;
// les cases a cocher dans un panneau
pan = new JPanel () ;
[Link] (pan) ;
cercle = new JCheckBox ("Cercle") ;
[Link] (cercle) ;
[Link] (this) ;
[Link] (this) ;
rectangle = new JCheckBox ("Rectangle") ;
[Link] (rectangle) ;
[Link] (this) ;
[Link] (this) ;
triangle = new JCheckBox ("Triangle") ;
[Link] (triangle) ;
[Link] (this) ;
[Link] (this) ;}
public void actionPerformed (ActionEvent e){
Object source = [Link]() ;
if (source == boutRaz){
[Link] (false) ;
[Link] (false) ;
[Link] (false) ;
if (source == boutEtat){
[Link] ("Cases selectionnees : ") ;
if ([Link]()) [Link] (" cercle ") ;
if ([Link]()) [Link] (" rectangle ") ;
if ([Link]()) [Link] (" triangle ") ;
[Link]() ;
if (source == cercle) [Link] ("Action case cercle") ;
if (source == rectangle) [Link] ("Action case rectangle") ;
if (source == triangle) [Link] ("Action case triangle") ;}
public void itemStateChanged (ItemEvent e)
{ Object source = [Link]() ;
if (source == cercle) [Link] ("Item case cercle") ;
if (source == rectangle) [Link] ("Item case rectangle") ;
if (source == triangle) [Link] ("Item case triangle") ;
private JButton boutRaz, boutEtat ;
private JPanel pan ;
private JCheckBox cercle, rectangle, triangle ;
public static void main (String args[]){
ex1 fen = new ex1() ;
[Link](true) ;
}}
//ex2
import [Link].* ;
import [Link].* ;
import [Link].* ;
class ex2 extends JFrame implements ActionListener{
public ex2 (String[] libelles){
setTitle ("Boutons radio") ;
setSize (400, 160) ;
Container contenu = getContentPane () ;
boutEtat = new JButton ("Etat") ;
[Link] (this) ;
[Link] (boutEtat, "South") ;
pan = new JPanel () ;
[Link] (pan) ;
[Link] = libelles ;
nbBoutons = [Link] ;
ButtonGroup groupe = new ButtonGroup() ;
boutons = new JRadioButton [nbBoutons] ;
for (int i=0 ; i<nbBoutons ; i++)
{ boutons[i] = new JRadioButton (libelles[i]) ;
[Link] (boutons[i]) ;
[Link] (boutons[i]) ;
boutons[i].addActionListener (this) ;
if (nbBoutons > 0) boutons[0].setSelected(true) ;}
public void actionPerformed (ActionEvent e)
{ Object source = [Link]() ;
if (source == boutEtat)
{ [Link] ("Bouton selectionne = ") ;
for (int i=0 ; i<nbBoutons ; i++)
if (boutons[i].isSelected()) [Link] (libelles[i]+ " ") ;
[Link]() ;
for (int i=0 ; i<nbBoutons ; i++)
if (source == boutons[i])
[Link] ("Action bouton " + libelles[i]) ;
private JButton boutEtat ;
private JPanel pan ;
private JRadioButton boutons[] ;
private String libelles[] ;
private int nbBoutons ;
public static void main (String args[]){
String libelles[] = {"Cercle", "Rectangle", "Triangle", "Pentagone",
"Ellipse", "Carre"} ;
ex2 fen = new ex2(libelles) ;
[Link](true) ;}}
//ex3
import [Link].* ;
import [Link].* ;
import [Link].* ;
public class ex3 extends JFrame implements ActionListener {
public ex3 () {
setTitle ("Carres") ;
setSize (400, 100) ;
Container contenu = getContentPane() ;
[Link] (new FlowLayout() ) ;
labNombre = new JLabel (etiqNombre) ;
[Link](labNombre) ;
nombre = new JTextField (10) ;
[Link](nombre) ;
boutonCalcul = new JButton ("CALCUL") ;
[Link](boutonCalcul) ;
[Link](this) ;
labCarre = new JLabel (etiqCarre) ;
[Link](labCarre) ;
public void actionPerformed (ActionEvent e)
{ if ([Link]() == boutonCalcul)
try
{ String texte = [Link]() ;
int n = [Link](texte) ;
long carre = (long)n*(long)n ;
[Link] (etiqCarre + carre) ;
}
catch (NumberFormatException ex)
{ [Link] ("") ;
[Link] (etiqCarre) ;
private JLabel labNombre, labCarre ;
private JTextField nombre ;
static private String etiqNombre = "Nombre : ", etiqCarre = "Carre : " ;
private JButton boutonCalcul ;
public static void main (String args[])
{ ex3 fen = new ex3() ;
[Link](true) ;
TD N8:
//ex1
import [Link];
public class ex1 {
public static void main(String[] args) {
int nombre = 2;
int reponse;
do {
double racineCarree = [Link](nombre);
[Link](null, "La racine carrée de " + nombre + " est : " + racineCarree);
reponse = [Link](null, "Voulez-vous continuer ?", "Continuer ?",
JOptionPane.YES_NO_OPTION);
// Passer au prochain nombre pair
nombre += 2; }
while (reponse == JOptionPane.YES_OPTION);
}}
//ex2
import [Link].*;
public class exo2 {
public static void main(String[] args) {
double note;
int n=1;
boolean correct;
double somme=0;
double moy;
int reponse;
do{
n++;
do{
correct=false;
String txt=[Link](null,"Entrer la note"+n,"
notes",JOptionPane.INFORMATION_MESSAGE);
try{
note=[Link](txt);
correct=true;
somme+=note;
}
catch(NumberFormatException e){
[Link](null, "Note incorrecte");}
}while (!correct);
reponse=[Link](null,"Autre
notes","Confirmation",JOptionPane.YES_NO_OPTION);
}while(reponse==JOptionPane.YES_OPTION);
moy=somme/n;
[Link](null, "La moyenne des "+n+" notes="+moy);
}}
//ex3
import [Link].*;
public class ex3 {
public static void main(String[] args) {
try {
int som=0;
FileReader fileReader = new FileReader("[Link]");
BufferedReader reader = new BufferedReader(fileReader);
while ([Link]()) {
String[] line = [Link]().split(" ");
for (String s : line) {
[Link](s+" ");
som+=[Link](s);
[Link]();
}
[Link]("somme= "+som);
[Link]();
[Link]();
} catch (FileNotFoundException e) {
[Link]();
} catch (IOException e) {
[Link]();
//ex4
import [Link];
import [Link];
import [Link];
public class ex4 {
public static void main(String[] args) {
try{
FileWriter writer1 = new FileWriter("[Link]");
FileWriter writer2 = new FileWriter("[Link]");
int s;
for(int i=0;i<5;i++) {
int som=0;
for(int j=0;j<5;j++) {
s=(int)([Link]()*10+20);
som+=s;
[Link](s + " ");}
[Link]("\n");
[Link]("ligne "+i+": "+som);
[Link]("\n");}
[Link]();
[Link]();}
catch(FileNotFoundException e){
[Link]();
catch(IOException e){
[Link]();
}}
//ex5.6
import [Link].* ;
import [Link].* ;
public class ex6 extends JFrame implements ActionListener{
public ex6 (){
setTitle ("Exemple de menus") ;
setSize (300, 130) ;
/* creation barre des menus */
barreMenus = new JMenuBar() ;
setJMenuBar(barreMenus) ;
/* creation menu Fichier et ses options */
fichier = new JMenu ("Fichier") ;
[Link](fichier) ;
ouvrir = new JMenuItem ("Ouvrir") ;
[Link] (ouvrir) ;
[Link] (this) ;
sauvegarder = new JMenuItem ("Sauvegarder") ;
[Link] (sauvegarder) ;
[Link] (this) ;
fermer = new JMenuItem ("Fermer") ;
[Link] (fermer) ;
[Link] (this) ;
/* creation menu Edition et ses options*/
edition = new JMenu ("Edition") ;
[Link] (edition) ;
copier = new JMenuItem ("Copier") ;
[Link] (copier) ;
[Link] (this) ;
coller = new JMenuItem ("Coller") ;
[Link] (coller) ;
[Link] (this) ;
/* etat initial : pas de fichier ouvert, pas d'info copiee */
fichierOuvert = false ; infoCopiee = false ;
nomFichier = null ;
public void actionPerformed (ActionEvent e){
Object source = [Link]() ;
if (source == ouvrir){
String nom = [Link] (this, "nom fichier a ouvrir") ;
if ((nom == null) || ([Link](""))) return ;
if (fichierOuvert) [Link] ("On ferme " + nomFichier) ;
nomFichier = nom ; fichierOuvert = true ;
[Link] ("On ouvre " + nomFichier) ;}
if (source == fermer){
if (fichierOuvert) [Link] ("On ferme " + nomFichier) ;
else [Link] ("pas de fichier ouvert") ;
fichierOuvert = false ;}
if (source == sauvegarder){
if (fichierOuvert) [Link] ("on sauvegarde " + nomFichier) ;
else [Link] ("Pas de fichier ouvert a sauvegarder") ;}
if (source == copier){
[Link] ("copie d'information") ;
infoCopiee = true ;}
if (source == coller){
if (infoCopiee) [Link] ("collage d'information") ;
else [Link] ("Rien a coller") ;
infoCopiee = false ;}
private JMenuBar barreMenus ;
private JMenu fichier, edition ;
private JMenuItem ouvrir, sauvegarder, fermer, copier, coller ;
private boolean fichierOuvert, infoCopiee ;
private String nomFichier ;
public static void main (String args[]){
ex6 fen = new ex6() ;
[Link](true) ;}
وربي يوقفنا أجمعين، ادعولي معاكم فضال