Solution TD Java 4-8
Solution TD Java 4-8
//exo1
this.Ncompte=Ncompte;
this.solde=solde;
this.CIN=CIN;
solde+=somme;
if(solde>=somme){
solde=solde-somme;
else {
System.out.println(compte.avoirInf());
System.out.println(compte.avoirSolde());
compte.deposer(500);
System.out.println(compte.avoirInf());
compte.retirer(200);
System.out.println(compte.avoirInf());
System.out.println(compte.avoirSolde());
//exo3
xa = xxa;
ya = yya;
xb = xxb;
yb = yyb;
l1 =Math.abs(xb-xa);
l2 =Math.abs(xb-xa);}
double per=l1*2+l2*2;
double surf=l1*l2;
rct.calcul_perimetre();
rct.calcul_surface();
rct.affiche(); }}
//exo5
nom=n;
specialite=s;
age=a;
indice=i;
prime=age*valeur[indice];
}}
//exo5
double[]valeur={33.2,13.2,5.8};
p1.calcul_prime();
p1.affiche();}}
TD N5:
//ex1:
try{
int x=10/0;
System.out.println("x="+x);}
catch(Exception e){
System.out.println("Exception"+e);
e.printStackTrace();
int x=10/1;
System.out.println("x="+x);
System.out.println("fin de main");}
}}
//exo2 1
private int n;
this.n=n;
return n;
}}
// ex2 2
//exo2 3
try{
System.out.println("n="+n1.getN());
System.out.println("n="+n2.getN());
catch(ErrConst e){
//exo3 1
nomVille=pNom;
nbrHabitants=pNbr;
nomPays=pPays;
this.setCategorie();
nbrHabitants=nbre;
this.setCategorie();
int borneSup[]={0,1000,10000,100000,500000,1000000,5000000,10000000};
char categorie[]={'?','A','B','C','D','E','F','G','H'};
int i=0;
i++;
this.categorie = categorie[i];}
ErrCons(String msg)
{super(msg);}
v1.setNombreHabitants(-2000);
System.out.println(e.getMessage()); }}}
//exo4
try{
catch(Except e){
return;
finally{
f(1);
f(2);}}
//exo 4
TD N6:
//exo1
import javax.swing.*;
import java.awt.event.*;
public MaFenetre(){
setTitle("Gestion de clics");
setBounds(20,20,400,200);
addMouseListener(this);
fen.setVisible(true);
}
//ex2
import javax.swing.*;
import java.awt.event.*;
public exo2() {
setTitle("Gestion de clics");
setBounds(10,20,300,200);
addMouseListener(this);
f.setVisible(true);
}}
//exo 2 partie 2
import javax.swing.*;
import java.awt.event.*;
public exo22() {
setTitle("Gestion de clics");
setBounds(10,20,300,200);
addMouseListener(new Ecouteur());
}}
f.setVisible(true);
//ex3 partie 1 M1
import javax.swing.*;
import java.awt.event.*;
public class exo3 extends JFrame implements MouseListener{
public exo3(){
nbFen++;
num=nbFen;
setTitle("fenetre "+num);
setBounds(10,20,300,200);
addMouseListener(this);
for(int i=1;i<5;i++){
f.setVisible(true);}}}
//exo3 p2
import javax.swing.*;
import java.awt.event.*;
this.i=i;
setTitle("fenetre "+i);
setBounds(10,20,300,200);
addMouseListener(new ecout(i));}
int i;
this.i=i;}
}}
//exo4
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public fen2boutons(){
setSize(200,100);
monBouton1 = new JButton("B1");
monBouton2=new JButton("B2");
monBouton3=new JButton("B3");
contenu.add(monBouton1);
contenu.add(monBouton2);
contenu.add(monBouton3);
monBouton1.addActionListener(this);
monBouton2.addActionListener(this);
monBouton3.addActionListener(this);}
if(ev.getSource()==monBouton1)
if(ev.getSource()==monBouton2)
if(ev.getSource()==monBouton3)
fen.setVisible(true);
//exo5
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.*;
private JButton t;
private JLabel r;
public tirage(){
setTitle("Jeu 3 nbrs");
setSize(350,150);
Container cn=getContentPane();
cn.setLayout(new FlowLayout());
//Creation du textF
p1=new JPanel();
cn.add(p1);
t1=new JTextField(6);
p1.add(t1);
t1.addActionListener(this);
t2=new JTextField(6);
p1.add(t2);
t2.addActionListener(this);
t3=new JTextField(6);
p1.add(t3);
t3.addActionListener(this);
// button et res
p2=new JPanel();
cn.add(p2);
t=new JButton("Tirage");
p2.add(t);
t.addActionListener(this);
r=new JLabel("Resultat");
p2.add(r);
g=new JTextField(10);
p2.add(g);
g.addActionListener(this);
Object source=ev.getSource();
if(source==t){
int n1=rn.nextInt(10);
int n2=rn.nextInt(10);
int n3=rn.nextInt(10);
t2.setText(Integer.toString(n2));
t3.setText(Integer.toString(n3));
int s=n1+n2+n3;
if(s>15) g.setText("GAGNE");
else g.setText("PERDU");
ti.setVisible(true);
TD N7:
//ex1
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
public ex1(){
boutEtat.addActionListener (this) ;
contenu.add (pan) ;
pan.add (cercle) ;
cercle.addActionListener (this) ;
cercle.addItemListener (this) ;
pan.add (rectangle) ;
rectangle.addActionListener (this) ;
rectangle.addItemListener (this) ;
pan.add (triangle) ;
triangle.addActionListener (this) ;
triangle.addItemListener (this) ;}
if (source == boutRaz){
cercle.setSelected (false) ;
rectangle.setSelected (false) ;
triangle.setSelected (false) ;
if (source == boutEtat){
System.out.println() ;
fen.setVisible(true) ;
}}
//ex2
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
boutEtat.addActionListener (this) ;
contenu.add (pan) ;
this.libelles = libelles ;
nbBoutons = libelles.length ;
pan.add (boutons[i]) ;
groupe.add (boutons[i]) ;
boutons[i].addActionListener (this) ;
if (source == boutEtat)
System.out.println() ;
if (source == boutons[i])
"Ellipse", "Carre"} ;
fen.setVisible(true) ;}}
//ex3
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
public ex3 () {
setTitle ("Carres") ;
contenu.add(labNombre) ;
contenu.add(nombre) ;
contenu.add(boutonCalcul) ;
boutonCalcul.addActionListener(this) ;
contenu.add(labCarre) ;
{ if (e.getSource() == boutonCalcul)
try
int n = Integer.parseInt(texte) ;
}
catch (NumberFormatException ex)
{ nombre.setText ("") ;
labCarre.setText (etiqCarre) ;
fen.setVisible(true) ;
TD N8:
//ex1
import javax.swing.JOptionPane;
int nombre = 2;
int reponse;
do {
JOptionPane.showMessageDialog(null, "La racine carrée de " + nombre + " est : " + racineCarree);
nombre += 2; }
}}
//ex2
import javax.swing.*;
double note;
int n=1;
boolean correct;
double somme=0;
double moy;
int reponse;
do{
n++;
do{
correct=false;
try{
note=Double.parseDouble(txt);
correct=true;
somme+=note;
}
catch(NumberFormatException e){
}while (!correct);
reponse=JOptionPane.showConfirmDialog(null,"Autre
notes","Confirmation",JOptionPane.YES_NO_OPTION);
}while(reponse==JOptionPane.YES_OPTION);
moy=somme/n;
}}
//ex3
import java.io.*;
try {
int som=0;
while (reader.ready()) {
System.out.print(s+" ");
som+=Integer.parseInt(s);
System.out.println();
}
System.out.println("somme= "+som);
reader.close();
fileReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
//ex4
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
try{
FileWriter writer1 = new FileWriter("Matrice1.txt");
int s;
for(int i=0;i<5;i++) {
int som=0;
for(int j=0;j<5;j++) {
s=(int)(Math.random()*10+20);
som+=s;
writer1.write("\n");
writer2.write("\n");}
writer1.close();
writer2.close();}
catch(FileNotFoundException e){
e.printStackTrace();
catch(IOException e){
e.printStackTrace();
}}
//ex5.6
import java.awt.event.* ;
import javax.swing.* ;
setJMenuBar(barreMenus) ;
barreMenus.add(fichier) ;
fichier.add (ouvrir) ;
ouvrir.addActionListener (this) ;
fichier.add (sauvegarder) ;
sauvegarder.addActionListener (this) ;
fichier.add (fermer) ;
fermer.addActionListener (this) ;
barreMenus.add (edition) ;
edition.add (copier) ;
copier.addActionListener (this) ;
edition.add (coller) ;
coller.addActionListener (this) ;
nomFichier = null ;
if (source == ouvrir){
if (source == fermer){
if (source == sauvegarder){
if (source == copier){
infoCopiee = true ;}
if (source == coller){
infoCopiee = false ;}
fen.setVisible(true) ;}