Java

Télécharger cours Java

1- Notion de class : exemple class etudiant

//Class etudiant

public class etudiant {

public int age;

public int poids;

public String nom;

public void affichage(){

}

//constructeur paramatré

public etudiant(int age,int poids,String nom){

this.age=age;

this.poids=poids;

this.nom=nom;

}

//constructeur par defaut

public etudiant(){

this.age=100;

}

//constructeur de recopie

public etudiant(etudiant c)

{      this.age=c.age;

this.poids=c.poids;

this.nom=c.nom;

}

}

//--------------------------------------------------------------------------------------------------------------

 

public class main {

public static void main(String[] Args){

etudiant p1=new etudiant();

p1.age=20;

p1.poids=40;

p1.nom="nour";

int a=p1.age;

System.out.println("age= "+a);

System.out.println("poids ="+p1.poids);

System.out.println("nom ="+p1.nom);

}

}

2- Notion de class : exemple class personne

//Class personne

public class personne {

public int age;

private int poids;

public String nom;

public String prenom;

public String cin;

public String sexe;

public static int compteur=0;

public int nombre;

public int getPoids() {

return this.poids;

}

 

public void setPoids(int poids) {

this.poids = poids;

}

public String affichage(){

return "age "+this.age+" nom ="+this.nom+" prenom= "+this.prenom+" cin="+this.cin+" sex= "+this.sexe;

}

public personne(int age,int poids,String nom,String prenom,String cin,String sexe){

this.age=age;

this.poids=poids;

this.nom=nom;

this.prenom=prenom;

this.cin=cin;

this.sexe=sexe;

this.nombre=compteur++;

}

public personne(){

this.nombre=compteur++;

}

public personne(personne c)

{

this.age=c.age;

this.poids=c.poids;

this.nom=c.nom;

this.prenom=c.prenom;

this.cin=c.cin;

this.nombre=compteur++;

}

}

}

//---------------------------------------------------------main-------------------------------------------

public class main {

public static void main(String[] Args){

personne p1=new personne(20,40,"jagh","nour","h1111","M") ;

personne p2=new personne(p1);

personne p3=new personne();

p1.affichage();

p1.setPoids(100);//       p1.poids=100;

p1.affichage();

p2.setPoids(100);

System.out.println("age="+p1.age+"  poids ="+p1.getPoids()+"  nom ="+"  "+"  "+p1.nom+ " nombre= "+p1.nombre);

p2.nom="brahim";

System.out.println("age="+p2.age+"  poids ="+p2.getPoids()+"  nom ="+"  "+"  "+p2.nom+ " nombre= "+p2.nombre);

System.out.println("nombre="+p3.nombre);

//on accede au variable static par le nom de la class

System.out.println("nombre total d'objet instancier ="+(personne.compteur));

System.out.println(p1.affichage());

System.out.println(p2.affichage());

}

}

3-Notion d'héritage en java:exemple 1

//Class personne

public class personne {

protected String nom;

protected String prenom;

protected String cin;

protected String sexe;

protected int age;

public int nombre;

public static int compteur=1;

public String affichage(){

return this.nom+" : "+this.prenom+" : "+this.cin+" : "+this.sexe+" : "+this.age;

}

public personne(String nom,String prenom,String cin,String sexe,int age){

this.nom=nom;

this.prenom=prenom;

this.cin=cin;

this.sexe=sexe;

this.age=age;

this.nombre=compteur++;

}

public personne(){

this.nombre=compteur++;

}

public personne(personne c)

{

this.nom=c.nom;

this.prenom=c.prenom;

this.cin=c.cin;

this.sexe=c.sexe;

this.age=c.age;

this.nombre=compteur++;

}

}

//--------------------------------employe---------------------------------

 

public class employe extends personne {

//extends car elle herite de la class personne

public String service;

public String code;

public float salaire;

public employe(){

 

}

public employe(String nom,String prenom,String cin,String sexe,int age,String service,String code,float salaire){

super(nom,prenom,cin,sexe,age);

this.service=service;

this.code=code;

this.salaire=salaire;

}

public String affichage2(){

return this.nom+" : "+this.prenom+" : "+this.cin+" : "+this.sexe+" : "+this.age+" : "+service+" : "+salaire;

}

@Override

public String affichage(){

return super.affichage()+" : " +service+" : " +code+" : "+salaire;

}

}

//---------------------------------------------------------main-------------------------------------------

public class main {

public static void main(String[] Args){

 

personne p1=new personne("jagh","nour","h662","M",20) ;

personne p2;

p2 = new personne(p1);

personne p3=new personne();

employe e1=new employe("alaoui","amina","H222","F",22,"Admin","X120",2000);

 

System.out.println("age="+p1.age+"  nom ="+"  "+"  "+p1.nom+ " nombre= "+p1.nombre);

p2.nom="brahim";

System.out.println("age="+p2.age+"  nom ="+"  "+"  "+p2.nom+ " nombre= "+p2.nombre);

System.out.println("nombre="+p3.nombre);

//on accede au variable static par le nom de la class

System.out.println("nombre total d'objet instancier ="+(personne.compteur-1));

System.out.println(p1.affichage());

System.out.println(p2.affichage());

System.out.println(e1.affichage());

System.out.println(e1.affichage2());

System.out.println(e1.affichage2());

 

 

}

}

4-Notion d'heritage:exemple 2

package heritage2;

class Calculation {
int z;

public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}

public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}

public class heritage2 extends Calculation {

public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}

public static void main(String args[]) {
int a = 20, b = 10;
heritage2 demo = new heritage2();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}

5-Suite heritage : class vehicule , avion ,voityre

public class vehicule {
protected String marque;
protected int date_achat;
protected int prix_achat;
protected int prix_courant;
public vehicule(String marque,int date_achat, int prix_achat, int prix_courant){
this.marque=marque;
this.date_achat=date_achat;
this.prix_achat=prix_achat;
this.prix_courant=prix_courant;
}
public void afficher(){
System.out.println(this.marque+"-"+this.date_achat+"-"+this.prix_achat+"-"+this.prix_courant);
}

public void calcul_prix(){
double p;
p=(1-(2017-this.date_achat)*0.01)*this.prix_achat;
System.out.println(p);
}

}

//////////////////////////:

public class avion extends vehicule{
public String type;
public int nbheure;
public avion(String marque,int date_achat, int prix_achat, int prix_courant,String type,int nbheure){
super(marque,date_achat,prix_achat,prix_courant) ;
this.type=type;
this.nbheure=nbheure;
}
@Override
public void afficher(){
super.afficher();
System.out.println(this.type+"-"+this.nbheure);
}
@Override
public void calcul_prix(){
double p;
p=prix_achat;
int k1,k2;
k1=this.nbheure%1000;
p=p-0.1*k1;
if (this.type.equals("reaction") ){
p=p-0.1*k1;
}
k2=this.nbheure%100;
if (this.type.equals("helice") ){
p=p-0.1*k2;
}
if (p<0){
p=0;
}
System.out.println(p);

}
}

/////////////////////////

public class voiture extends vehicule{
public int cylindre;
public int nbporte;
public int puiss;
public int kilom;
public voiture (String marque,int date_achat, int prix_achat, int prix_courant,int cylindre,int nbporte,int puiss,int kilom){
super(marque,date_achat,prix_achat,prix_courant) ;
this.cylindre=cylindre;
this.nbporte=nbporte;
this.kilom=kilom;
this.puiss=puiss;
}
@Override
public void afficher(){
super.afficher();
System.out.println(this.cylindre+"-"+this.nbporte+"-"+this.kilom+"-"+this.puiss);
}
@Override
public void calcul_prix(){
double p;
int k;
p=(1-(2017-this.date_achat)*0.02)*this.prix_achat;
k=this.kilom%10000;
p=p-k*k*0.05;
if (this.marque.equals("Renaut") || this.marque.equals("Fiat")){
p=p-p*0.1;
}
if (this.marque.equals("Ferrari") ){
p=p-p*0.2;
}
if (p<0){
p=0;
}
System.out.println(p);
}
}

/////////////////////:

public static void main(String[] args) {
vehicule v4=new vehicule("Peugeot",1998,2000,100);
voiture v1=new voiture("Peugeot",1998,2000,100,50,600,20,30);
voiture v2=new voiture("Porshe",1998,2000,100,50,600,20,30);
voiture v3=new voiture("Renaut",1998,2000,100,50,600,20,30000);
v4.afficher();
v1.afficher();
v1.calcul_prix();

}

6-abstract class forme

public abstract class forme {
protected String name;
protected abstract void afficher();
protected abstract double surface();

}

///////////////////////

public class rectangle extends forme{
private double L,l;
public rectangle(String name,double L,double l){
this.name=name;
this.L=L;
this.l=l;
}
public void afficher(){
System.out.println(this.name+"-"+this.L+"-"+this.l);
}
public double surface(){
return this.L*this.l;
}
public double getL(){
return this.L;
}
public double getl(){
return this.l;
}
public void setL(double L){
this.L=L;
}
public void setl(double l){
this.l=l;
}
}

/////////////////////////////

public class triangle extends forme{
double h,b;
public triangle(String name,double h,double b){
this.name=name;
this.h=h;
this.b=b;

}
@Override
public void afficher(){
System.out.println(this.name+"-"+this.h+"-"+this.b);
}
@Override
public double surface(){
return this.h*this.b/2;
}

}

7-Application Héritage : class direction

class Direction
{
public static void main(String[] args) {
Ecole epfl = new Ecole(5);
epfl.add(new EtudiantRegulier("Gaston Peutimide", 2008, "SSC", 6.0));
epfl.add(new EtudiantRegulier("Yvan Rattrapeur", 2009, "SSC", 2.5));
epfl.add(new EtudiantEchange("Bjorn Borgue", 2010, "Informatique", "KTH"));
epfl.add(new Enseignant("Mathieu Matheu", 1998, "LMEP", 10000, "Physique"));
epfl.add(new Secretaire("Sophie Scribona", 1995, "LMT", 5000));
epfl.afficherStatistiques();
epfl.afficherEpfliens();
}

}

class Ecole {
private int nbEtudiants;
private int nbTotal;
private Epflien [] gens;

public final static int ANNEE = 2012;

public Ecole(int nbPersonnes) {
gens = new Epflien[nbPersonnes];
nbEtudiants = 0;
nbTotal = 0;
}

public void add(Epflien personne)
{
if (nbTotal < gens.length)
{
gens[nbTotal] = personne;
++nbTotal;
if (personne.estEtudiant())
{
++nbEtudiants;
}
}
}

public void afficherStatistiques() {
int nbAnneesTotal = 0;
for (int i = 0; i < gens.length; i++) {
nbAnneesTotal = nbAnneesTotal + (ANNEE - gens[i].getAnnee());
}
System.out.println("Parmi les " + gens.length + " EPFLiens, " +
nbEtudiants + " sont des etudiants.");
double moyen = (double) nbAnneesTotal / gens.length;
System.out.println("Ils sont ici depuis en moyenne " + moyen + " ans");
}

public void afficherEpfliens() {
System.out.println("Liste des EPFLiens: ");
for (int i = 0; i < gens.length; i++)
gens[i].afficher();
}
}

class Epflien {
private String nom;
private int annee;

public Epflien(String nom, int annee) {
this.nom = nom;
this.annee = annee;
}

public void afficher() {
System.out.println(" Nom : " + getNom());
System.out.println(" Annee : " + getAnnee());
}

public String getNom() {
return nom;
}

public int getAnnee() {
return annee;
}

public boolean estEtudiant ()
{
return false;
}

}

class Etudiant extends Epflien {
private String section;

public Etudiant(String nom, int annee, String section) {
super(nom, annee);
this.section = section;
}

public void afficher() {
super.afficher();
System.out.println(" Section : " + getSection());
}

public String getSection() {
return section;
}

public boolean estEtudiant()
{
return true;
}

}

class EtudiantRegulier extends Etudiant {
private double prope1;

public EtudiantRegulier(String nom, int annee, String section, double prope1) {
super(nom, annee, section);
this.prope1 = prope1;
}

public void afficher() {
System.out.println("Etudiant regulier:");
super.afficher();
System.out.println(" Prope I : " + getPrope1());
}

public double getPrope1() {
return prope1;
}
}

class EtudiantEchange extends Etudiant {
private String uniOrigine;

public EtudiantEchange(String nom, int annee, String section, String uniOrigine) {
super(nom, annee, section);
this.uniOrigine = uniOrigine;
}

public void afficher() {
System.out.println("Etudiant d'echange:");
super.afficher();
System.out.println(" Uni d'origine : " + getUniOrigine());
}

public String getUniOrigine() {
return uniOrigine;
}
}

class Personnel extends Epflien {
private String labo;
private int salaire;

public Personnel(String nom, int annee, String labo, int salaire) {
super(nom, annee);
this.labo = labo;
this.salaire = salaire;
}

public void afficher() {
super.afficher();
System.out.println(" Laboratoire : " + getLabo());
System.out.println(" Salaire : " + getSalaire());
}

public String getLabo() {
return labo;
}

public int getSalaire() {
return salaire;
}
}

class Enseignant extends Personnel {
private String section;

public Enseignant(String nom, int annee, String labo, int salaire, String section) {
super(nom, annee, labo, salaire);
this.section = section;
}

public void afficher() {
System.out.println("Enseignant:");
super.afficher();
System.out.println(" Section d'enseignement : " + getSection());
}

public String getSection() {
return section;
}
}

class Secretaire extends Personnel {
public Secretaire(String nom, int annee, String labo, int salaire) {
super(nom, annee, labo, salaire);
}

public void afficher() {
System.out.println("Secretaire:");
super.afficher();
}
}

8-class point

public class point {
private int x,y;
public point(){

}
public point(int x,int y){
this.x=x;
this.y=y;
}
public void affiche()
{
System.out.println(this.getX()+" "+this.getY());
}

/**
* @return the x
*/
public int getX() {
return x;
}

/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}

/**
* @return the y
*/
public int getY() {
return y;
}

/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}
public static point somme(point p1,point p2) {
point p3=new point();
p3.x=p1.x+p2.x;
p3.y=p1.y+p2.y;
return p3;
}
public point somme1(point p2) {
point p3=new point();
p3.x=this.x+p2.x;
p3.y=this.y+p2.y;
return p3;
}

}

/////////////////////////main/////////////////////////////

public class JavaApplication30 {

public static void main(String[] args) {
point p1=new point(2,3);
point p2=new point(4,5);
point p3=new point();
point p4=new point();
System.out.println("----------------");

p3=point.somme(p1, p2);
p3.affiche();
System.out.println("----------------");
p4=p1.somme1( p2);
p4.affiche();
}

}

 

9-class gestion véhicules

class GestionVehicules {

private static int ANNEE_ACTUELLE = 2012;

public static void main(String[] args) {
Vehicule[] vehicules = new Vehicule [5];

vehicules[0] = new Voiture("Peugeot", 2005, 147325.79, 2.5, 5, 180.0, 12000);
vehicules[1] = new Voiture("Porsche", 1999, 250000.00, 6.5, 2, 280.0, 81320);
vehicules[2] = new Voiture("Fiat", 2001, 7327.30, 1.6, 3, 65.0, 3000);

vehicules[3] = new Avion("Cessna", 1982, 1230673.90, "HELICES", 250);
vehicules[4] = new Avion("Nain Connu", 1993, 4321098.00, "REACTION", 1300);

for (int i = 0; i < vehicules.length; i++) {
vehicules[i].calculePrix(ANNEE_ACTUELLE);
vehicules[i].affiche();
}
}
}

class Vehicule {

private String marque;
private int dateAchat;
private double prixAchat;
private double prixCourant;

public Vehicule(String marque, int date, double prix) {
this.marque = marque;
dateAchat = date;
prixAchat = prix;
prixCourant = prix;
}

public void calculePrix(int anneActuelle) {
double decote = (anneActuelle - dateAchat) * 0.01;
prixCourant = Math.max(0.0, (1.0 - decote) * prixAchat);
}

public void affiche() {
System.out.print("marque : " + marque +
", date d'achat : " + dateAchat +
", prix actuel : " + prixCourant);
System.out.println();
}
public int getDateAchat(){
return dateAchat;
}
public String getMarque(){
return marque;
}
public double getPrixAchat(){
return prixAchat;
}
public void setPrixCourant(double prix){
prixCourant = prix;
}

}

class Voiture extends Vehicule {

private double cylindree;
private int nbPortes;
private double puissance;
private double kilometrage;

public Voiture(String marque, int date, double prix,
double cylindree, int portes, double cv, double km) {
super(marque, date, prix);
this.cylindree = cylindree;
nbPortes = portes;
puissance = cv;
kilometrage = km;
}

public void calculePrix(int anneActuelle) {
double decote = (anneActuelle - getDateAchat()) * .02;
// On force le type en int de manière à arrondir le résultat
// On verra dans quelques semaines une manière plus élégante de faire
// ce genre de choses...
decote += 0.05 * (int)(kilometrage / 10000);
if ((getMarque() == "Fiat") || (getMarque() == "Renault")) {
decote += 0.1;
} else if ((getMarque() == "Ferrari") || (getMarque() == "Porsche")) {
decote -= 0.2;
}

setPrixCourant(Math.max(0.0, (1.0 - decote) * getPrixAchat()));
}

public void affiche() {
System.out.println(" ---- Voiture ----");
super.affiche();
System.out.println(cylindree + " litres, "
+ nbPortes + " portes, "
+ puissance + " CV, "
+ kilometrage + " km.");
}
}

class Avion extends Vehicule {

private String moteur;
private int heuresVol;

public Avion(String marque, int date, double prix, String moteur, int heures) {
super(marque, date, prix);
this.moteur = moteur;
heuresVol = heures;
}

public void calculePrix(int anneActuelle) {
double decote;
if (moteur == "HELICES") {
decote = 0.1 * heuresVol / 100.0;
} else {
decote = 0.1 * heuresVol / 1000.0;
}

setPrixCourant(Math.max(0.0, (1.0 - decote) * getPrixAchat()));
}

public void affiche() {
System.out.println(" ---- Avion à " + moteur + "----");
super.affiche();
System.out.println(heuresVol + " heures de vol.");
}

}

10-class Scanner

import java.util.Scanner;
public class ScannerDemo {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String nom;
double age;
String genre;
String marital_status;
int telephone_number;
Scanner one = new Scanner(System.in);
System.out.println("Enter Name:" );
nom = one.next();
System.out.println("Name accepte" + nom);
Scanner two = new Scanner(System.in);
System.out.println("Enter Age:" );
age = two.nextDouble();
System.out.println("Age accepte " + age);
Scanner three = new Scanner(System.in);
System.out.println("Enter Gender:" );
genre = three.next();
System.out.println("Genre accepte " + genre);
Scanner four = new Scanner(System.in);
System.out.println("Enter Marital status:" );
marital_status = four.next();
System.out.println("Marital status accepte " + marital_status);
Scanner five = new Scanner(System.in);
System.out.println("Enter num Telephone :" );
telephone_number = five.nextInt();
System.out.println("Telephone number accepte " + telephone_number);
}
}

11-OR - NAND en Java

public class Andlogic {

public static void main(String[] args) {
int a = 168;
int b = 255;

int c = a & b;

int d = a | b;

System.out.println(c);
System.out.println(d);
}

}

1-Graphique en Java

package pkg1_1form;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class fenetre extends JFrame {//herite de la classe JFrame
public fenetre(){ //constructeur par defaut
super("BTS SRI");//appelle du constructeur d'initialisation de la class JFrame pour donner un titre à la fenetre
// Container c = getContentPane();
JPanel pn=new JPanel();
pn.setBackground(Color.blue);
JButton bouton1=new JButton();
JButton bouton2=new JButton("envoyer");
JLabel label=new JLabel();
JTextField tf=new JTextField();
JComboBox cb=new JComboBox();
tf.setSize(200,180);
tf.setText(" safi ");
label.setText("clique sur bouton");
cb.addItem("safi ");
cb.addItem("casa ");
cb.addItem("marraa ");
cb.addItem("sale ");
cb.addItem("rabat ");
cb.setSelectedIndex(3);
bouton1.setText("OK");
bouton1.setSize(50, 80);
//bouton2.setText("envoyer");
bouton2.setSize(50, 80);
// c.add(bouton1,"Center");

pn.add(label);
pn.add(bouton1);
pn.add(bouton2);
pn.add(tf);
pn.add(cb);
setContentPane(pn);
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

//////////////

import javax.swing.*;
public class Main {
public static void main(String[] args) {//programme general
JFrame f=new fenetre();
}
}

2-Graphique en Java; les événements

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class form2 extends JFrame implements ActionListener{
private JButton bouton1= new JButton();
private JButton bouton2= new JButton("bouton2");
private JButton btn= new JButton();
private JTextField txt=new JTextField("text");
// private JPanel panel1=new JPanel();
public form2(){
setSize(600, 200);setTitle("ENSAS1");setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container conteneur = getContentPane();
FlowLayout disposition = new FlowLayout();
conteneur.setLayout(disposition);

conteneur.add(bouton1);bouton1.setText("bouton1");bouton1.setSize(100, 30);
conteneur.add(bouton2);
txt.setBounds(5, 5, 100, 100);
txt.setPreferredSize(new Dimension(80,20));
conteneur.add(txt);
// panel1.add(bouton1);panel1.add(bouton2);setContentPane(panel1);
bouton1.addActionListener(this);
bouton2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e){

Object source = e.getSource();

if (source == bouton1) {
System.out.println("un");
JOptionPane.showMessageDialog(null, "un");
}

else if (source == bouton2) {
System.out.println("deux");
txt.setText("deux");
}

}

/////////////////:

package pkg1_ensas;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
/* JFrame f;
f=new JFrame();
f.setTitle("ENSAS1");
f.setSize(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
*/
form2 f;
f= new form2();
}
}

}

Exercice: Adressage IP

//Projet VLSM
//BTS Safi : 2016/2017
//Réalisé par ELJAAFARI & ETTAJANI
//Encadré par Jaghlal.N
package adressageip;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AdressageIp extends JFrame implements ActionListener{
//
JLabel m=new JLabel("Sous reseau", SwingConstants.CENTER);
JLabel c=new JLabel(" @ Sous reseau", SwingConstants.CENTER);
JLabel g=new JLabel("premiere @", SwingConstants.CENTER);
JLabel j=new JLabel("Derniere @", SwingConstants.CENTER);
JLabel k=new JLabel("@ diffusion", SwingConstants.CENTER);

JLabel m1=new JLabel("XXXX", SwingConstants.CENTER);
JLabel c1=new JLabel(" YYY", SwingConstants.CENTER);
JLabel g1=new JLabel("ZZZZ", SwingConstants.CENTER);
JLabel j1=new JLabel("WWWWW", SwingConstants.CENTER);
JLabel k1=new JLabel("bts@gmail.com", SwingConstants.CENTER);
JButton af=new JButton("Calculer");
JButton ef=new JButton("Effacer");
JButton exit=new JButton("Exit");
JButton[] array;
JComboBox<String> comboBox = new JComboBox<String>();
//
private String texte;
ImageIcon image = new ImageIcon("image.png");
JLabel titre1=new JLabel("saisir @ IP", SwingConstants.CENTER);
JLabel titre2=new JLabel("masque", SwingConstants.CENTER);
JLabel titre3=new JLabel("Operation", SwingConstants.CENTER);
private JPanel panel,pane1,pane;
JTextField t1 = new JTextField("192");

JTextField t2 = new JTextField("168");
JTextField t3 = new JTextField("1");
JTextField t4 = new JTextField("1");

JTextField t11 = new JTextField(10);
JTextField t22 = new JTextField(10);
JTextField t33 = new JTextField(10);
JTextField t44 = new JTextField(10);
JScrollPane scrollBar;
int x=0,inc=0,comp=0,comp1=1,msk=0;
public AdressageIp(){
super("BTS Safi (2016/2017): ELJAAFARI & ETTAJANI");
setSize(260,260);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
////////////////////////
JLabel imagelabel = new JLabel(image);
pane=new JPanel();
GridLayout family=new GridLayout(3,5,5,5);
pane.setLayout(family);
t1.setHorizontalAlignment(JLabel.CENTER);
t2.setHorizontalAlignment(JLabel.CENTER);
t3.setHorizontalAlignment(JLabel.CENTER);
t4.setHorizontalAlignment(JLabel.CENTER);

t11.setHorizontalAlignment(JLabel.CENTER);
t22.setHorizontalAlignment(JLabel.CENTER);
t33.setHorizontalAlignment(JLabel.CENTER);
t44.setHorizontalAlignment(JLabel.CENTER);
t1.setPreferredSize(new Dimension(80,20));
t2.setPreferredSize(new Dimension(80,20));
t3.setPreferredSize(new Dimension(80,20));
t4.setPreferredSize(new Dimension(80,20));
t11.setPreferredSize(new Dimension(80,20));
t22.setPreferredSize(new Dimension(80,20));
t33.setPreferredSize(new Dimension(80,20));
t44.setPreferredSize(new Dimension(80,20));

pane.add(titre1); pane.add(t1); pane.add(t2); pane.add(t3); pane.add(t4);
pane.add(titre2); pane.add(t11); pane.add(t22); pane.add(t33); pane.add(t44);
pane.add(titre3); pane.add(comboBox);pane.add(af); pane.add(ef);; pane.add(exit);
comboBox.addItem("1"); comboBox.addItem("2");
comboBox.addItem("4"); comboBox.addItem("8");
comboBox.addItem("16"); comboBox.addItem("32");

//
pane1=new JPanel();
pane1.setPreferredSize(new Dimension(800, 200));
GridLayout family1=new GridLayout(2,1,5,5);
pane1.setLayout(family1);
pane1.add(imagelabel); pane1.add(pane);
//
af.addActionListener(this);
ef.addActionListener(this);
exit.addActionListener(this);

this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(pane1, BorderLayout.PAGE_START);

setVisible(true);
this.pack();
}
public void actionPerformed(ActionEvent e){

if(e.getSource()==af)
{
inc=0;comp=0;comp1=1;
///////////////////////////////////////////////////////////////////////////
if (x!=0){
for (int i = 0; i < array.length; i++) {
panel.remove(array[i]);

}
this.remove(scrollBar);this.repaint(); this.validate();

}
//////////////////////////////////////////////////////////////////////////
String value=comboBox.getSelectedItem().toString();
x = Integer.parseInt(value);
array = new JButton[x*5];
panel = new JPanel(new GridLayout(x+2,5));
panel.add(m1);panel.add(c1); panel.add(g1); panel.add(j1); panel.add(k1);
panel.add(m);panel.add(c); panel.add(g); panel.add(j); panel.add(k);
////////////////////////////////////////////////////////////////////////
String s=null,St=null;
if (x==1){ inc=0;
msk=0;
}
if (x==2){ inc=128;
msk=128;
}
if (x==4) {inc=64;
msk=192;
}
if (x==8) {inc=32;
msk=224;
}
if (x==16){ inc=16;
msk=240;
}
if (x==32){ inc=8;
msk=248;
}
int classe=Integer.parseInt(t1.getText());
for (int i = 0; i < array.length; i++) {
///////////////////////////
if ((i%5)==0){
s=""+(comp1);

}
if (((i-1)%5)==0){
if ((classe>0)&& (classe<128)) {
t11.setText("255");t22.setText(""+msk);t33.setText("0");t44.setText("0");
m1.setText("Classe A");
g1.setText("Nombre sous réseaux:"+x);
j1.setText("Increment:"+inc);
if(x==1) c1.setText("Nombre d'Host : "+(int)(Math.pow(2, 24)-2));
if(x==2) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 23)-2));
if(x==4) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 22)-2));
if(x==8) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 21)-2));
if(x==16) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 20)-2));
if(x==32) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 19)-2));
St=(""+comp);
s=t1.getText()+"."+St+".0.0";
}
if ((classe>127)&& (classe<192)) {
t11.setText("255");t22.setText("255");t33.setText(""+msk);t44.setText("0");
m1.setText("Classe B");
g1.setText("Nombre sous réseaux:"+x);
j1.setText("Increment:"+inc);
if(x==1) c1.setText("Nombre d'Host : "+(int)(Math.pow(2, 16)-2));
if(x==2) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 15)-2));
if(x==4) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 14)-2));
if(x==8) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 13)-2));
if(x==16) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 12)-2));
if(x==32) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 11)-2));
St=(""+comp);
s=t1.getText()+"."+t2.getText()+"."+St+".0";
}
if ((classe>191)&& (classe<224)) {
t11.setText("255");t22.setText("255");t33.setText("255");t44.setText(""+msk);
m1.setText("Classe C");
g1.setText("Nombre sous réseaux:"+x);
j1.setText("Increment:"+inc);
if(x==1) c1.setText("Nombre d'Host : "+(int)(Math.pow(2, 8)-2));
if(x==2) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 7)-2));
if(x==4) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 6)-2));
if(x==8) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 5)-2));
if(x==16) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 4)-2));
if(x==32) c1.setText("Nbre host/sous reseau : "+(int)(Math.pow(2, 3)-2));
St=(""+comp);
s=t1.getText()+"."+t2.getText()+"."+t3.getText()+"."+St;
}

}
if (((i-2)%5)==0){
if ((classe>0)&& (classe<128)) {
St=(""+comp);
s=t1.getText()+"."+St+".0.1";
}
if ((classe>127)&& (classe<192)) {
St=(""+comp);
s=t1.getText()+"."+t2.getText()+"."+St+".1";
}
if ((classe>191)&& (classe<224)) {
St=(""+(comp+1));
s=t1.getText()+"."+t2.getText()+"."+t3.getText()+"."+St;
}
}
if (((i-3)%5)==0){

if ((classe>0)&& (classe<128)) {
if(x!=1) St=(""+(comp+inc-2));
else St="255";
s=t1.getText()+"."+St+".255.254";
}
if ((classe>127)&& (classe<192)) {
if(x!=1) St=(""+(comp+inc-2));
else St="255";
s=t1.getText()+"."+t2.getText()+"."+St+".254";
}
if ((classe>191)&& (classe<224)) {
if(x!=1) St=(""+(comp+inc-2));
else St="254";
s=t1.getText()+"."+t2.getText()+"."+t3.getText()+"."+St;
}
}
if (((i-4)%5)==0){
if ((classe>0)&& (classe<128)) {
if(x!=1) St=(""+(comp+inc-1));
else St="255";
s=t1.getText()+"."+St+".255.255";
}
if ((classe>127)&& (classe<192)) {
if(x!=1) St=(""+(comp+inc-1));
else St="255";
s=t1.getText()+"."+t2.getText()+"."+St+".255";
}
if ((classe>191)&& (classe<224)) {
if(x!=1) St=(""+(comp+inc-1));
else St="255";
s=t1.getText()+"."+t2.getText()+"."+t3.getText()+"."+St;
}
comp+=inc;comp1++;
}
/////////////////////

// array[i] = new JButton(String.valueOf(i));
array[i] = new JButton(s);
// array[i].addActionListener(listener);
panel.add(array[i]);

}

//////////////////////////////////////////////////////////////////
scrollBar = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.getContentPane().add(scrollBar, BorderLayout.CENTER);
this.pack();
//System.out.println(comboBox.getSelectedItem());
this.repaint();
this.validate();
/////////////////////////////////////////////////////////////////////
}

else if(e.getSource()==ef)

{

for (int i = 0; i < array.length; i++) {
panel.add(array[i]); panel.remove(array[i]);

}

pane1.remove(panel);this.repaint(); this.validate();
}
else if(e.getSource()==exit)
{
System.exit(0);
}

}

public static void main(String[] args) {
new AdressageIp();
}
}

class banque

public class Banque {
public static void main(String[] args) {

// TODO Auto-generated method stub
Client c1=new Client("ahmed" , "casa", 0.01 , 1000 , 0.02 , 2000,true);
Client c2=new Client("nour" , "safi" ,0.01 , 3000 , 0.02 , 4000,false);

System.out.println("Avant le Bouclement n n");
Test t=new Test();
t.ajouter(c1);
t.ajouter(c2);
t.afficher();
System.out.println("Aprés le Bouclement n n");
t.bc();
t.afficher();

}

}

/////////////////////

public class Client {

private String name;
private String ville;
private Compte compte1;
private Compte compte2;
boolean masculin;

//constructeur

public Client(String name , String ville , double taux1 , double solde1 , double taux2 , double solde2,boolean m)
{
this.name=name;
this.ville=ville;
compte1=new Compte(taux1,solde1);
compte2=new Compte(taux2,solde2);
this.masculin=m;

}

//methods

void afficherClient()
{
if(masculin)
{
System.out.println("client "+name+" de "+ville);
System.out.println("compte prive :"+compte1.getSolde()+"francs");
System.out.println("compte d'epargne :"+compte2.getSolde()+"francs");

}else
{
System.out.println("cliente "+name+" de "+ville);
System.out.println("compte prive : "+compte1.getSolde()+" francs");
System.out.println("compte d'epargne : "+compte2.getSolde()+" francs");
}
}

public void boocler()
{
compte1.bouclerLaSolde();
compte2.bouclerLaSolde(); }
}

////////////////////

public class Compte {

//attributs

private double taux;
private double solde;

//constructeur ;

public Compte(double taux , double solde)
{
this.taux=taux;
this.solde=solde;
}
public Compte() {}

//methods

public double getTaux()
{
return taux;
}

public double getSolde()
{
return solde;
}

public double bouclerLaSolde()
{
return solde+=solde*taux;
}

}

////////////////

import java.util.*;

public class Test
{
ArrayList<Client> t;
Test()
{
t=new ArrayList<Client>();
}
void ajouter(Client c)
{
t.add(c);
}
void afficher()
{
for(int i=0;i<t.size();i++)
{
t.get(i).afficherClient();
}
}
void bc()
{
for(int i=0;i<t.size();i++)
{
t.get(i).boocler();
}

}
}

class composé

public class Point {

private int x, y;
public Point()
{
this.x = 0;this.y = 0;
}public Point(int x, int y)
{this.x = x;this.y = y;
}
public int getX() {
return this.x;
}
public void setX(int x)
{this.x = x;
}
public int getY()
{return this.y;
}
public void setY(int y)
{this.y = y;
}
public String toString() {return "(" + this.x + "," + this.y + ")";}
public int[] getXY()
{
int[] results = new int[2];
results[0] = this.x;
results[1] = this.y;return results;
}
public void setXY(int x, int y) {this.x = x;this.y = y;}
public double distance(int x, int y) {int xDiff = this.x - x;int yDiff = this.y - y;return Math.sqrt(xDiff*xDiff + yDiff*yDiff);}

public double distance(Point another) {int xDiff = this.x - another.x;int yDiff = this.y - another.y;return Math.sqrt(xDiff*xDiff + yDiff*yDiff);}
public double distance() {return Math.sqrt(this.x*this.x + this.y*this.y);}
}

////////////////

public class Line {
// The private instance variables
Point begin, end; // Object members - instances of the Point class

// Constructors
public Line(int x1, int y1, int x2, int y2) {
begin = new Point(x1, y1); // Construct the instances declared
end = new Point(x2, y2);
}
public Line(Point begin, Point end) {
this.begin = begin; // The caller constructed the instances
this.end = end;
}

// The public getter and setter for the private instance variables
public Point getBegin() {
return begin;
}
public Point getEnd() {
return end;
}
public void setBegin(Point begin) {
this.begin = begin;
}
public void setEnd(Point end) {
this.end = end;
}

public int getBeginX() {
return begin.getX(); // Point's getX()
}
public void setBeginX(int x) {
begin.setX(x); // Point's setX()
}
public int getBeginY() {
return begin.getY(); // Point's getY()
}
public void setBeginY(int y) {
begin.setY(y); // Point's setY()
}
public int[] getBeginXY() {
return begin.getXY(); // Point's getXY()
}
public void setBeginXY(int x, int y) {
begin.setXY(x, y); // Point's setXY()
}
public int getEndX() {
return end.getX(); // Point's getX()
}
public void setEndX(int x) {
end.setX(x); // Point's setX()
}
public int getEndY() {
return end.getY(); // Point's getY()
}
public void setEndY(int y) {
end.setY(y); // Point's setY()
}
public int[] getEndXY() {
return end.getXY(); // Point's getXY()
}
public void setEndXY(int x, int y) {
end.setXY(x, y); // Point's setXY()
}

// The toString() describe itself
public String toString() {
return "Line[begin=" + begin + ",end=" + end + "]";
// Invoke begin.toString() and end.toString()
}

public double getLength() {
return begin.distance(end); // Point's distance()
}
}

////////////////

public class TestLine {
public static void main(String[] args) {
// Test constructor and toString()
Line l1 = new Line(1, 2, 3, 4);
System.out.println(l1); // Line's toString()
Line l2 = new Line(new Point(5,6), new Point(7,8)); // anonymous Point's instances
System.out.println(l2); // Line's toString()

// Test Setters and Getters
l1.setBegin(new Point(11, 12));
l1.setEnd(new Point(13, 14));
System.out.println(l1); // Line's toString()
System.out.println("begin is: " + l1.getBegin()); // Point's toString()
System.out.println("end is: " + l1.getEnd()); // Point's toString()

l1.setBeginX(21);
l1.setBeginY(22);
l1.setEndX(23);
l1.setEndY(24);
System.out.println(l1); // Line's toString()
System.out.println("begin's x is: " + l1.getBeginX());
System.out.println("begin's y is: " + l1.getBeginY());
System.out.println("end's x is: " + l1.getEndX());
System.out.println("end's y is: " + l1.getEndY());

l1.setBeginXY(31, 32);
l1.setEndXY(33, 34);
System.out.println(l1); // Line's toString()
System.out.println("begin's x is: " + l1.getBeginXY()[0]);
System.out.println("begin's y is: " + l1.getBeginXY()[1]);
System.out.println("end's x is: " + l1.getEndXY()[0]);
System.out.println("end's y is: " + l1.getEndXY()[1]);

// Test getLength()
System.out.printf("length is: %.2f%n", l1.getLength());
}
}

événement en java

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Interface {

private JLabel label;
private JLabel label2;
private JLabel label3;
private JTextField textfield;
private JTextField textfield2;
private JTextField textfield3;
private JButton button;
private JButton button2;
private JButton button3;
private JPanel panel;

public static void main(String[] args) {
new Interface();
}

public Interface() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);

JPanel buttonPane = new JPanel();
label = new JLabel("Button 1");
label2 = new JLabel("Button 2");
label3 = new JLabel("Button 3");

textfield = new JTextField(5);

JButton button = new JButton("circle");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.removeAll();
panel.add(label);
panel.add(textfield);
panel.revalidate();
panel.repaint();
String text = textfield.getText();

// Van de String 'text' een double maken
double diameter = Double.parseDouble(text);

// cirkel C1 = new cirkel();
// C1.setDiam(diameter);
label.setText("Le diamètre = " + 1 + " cm nn");
label.setText("Le rayon = " + 2 + " cm");
label.setText("La surface du cercle = " + 3 + " cm2");
}
});

JButton button2 = new JButton("triangle");
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.removeAll();
panel.add(label);
panel.add(textfield);
panel.revalidate();
panel.repaint();
// driehoek
}
});

JButton button3 = new JButton("quadrilatère");
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.removeAll();
panel.add(label);
panel.add(textfield);
panel.revalidate();
panel.repaint();
// vierhoek
}
});

buttonPane.add(button);
buttonPane.add(button2);
buttonPane.add(button3);

panel = new JPanel();

frame.add(buttonPane, BorderLayout.NORTH);
frame.add(panel);
frame.setVisible(true);
}
}

projet :utilisation des Thread

Télécharger code source: https://www.4shared.com/rar/EEeGQappca/ani1.html?