Articles

Affichage des articles du novembre, 2017

multiplicationmatrice

public class Matrices { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double mat1[][]= new double [2][2]; double mat2[][]= new double [2][2]; mat1[0][0]=1; mat1[0][1]=2; mat1[1][0]=3; mat1[1][1]=5; mat2[0][0]=4; mat2[0][1]=6; mat2[1][0]=4; mat2[1][1]=7; double[][] prod =     new double[mat1.length][mat2[0].length];   for (int row = 0; row < mat1.length; row++) {       for (int col = 0; col < mat2[0].length; col++) {           prod[row][col] = 0.0;           for (int i = 0; i < mat2.length; i++) {               prod[row][col] += mat1[row][i] *                 mat2[i][col];           }       }               }   for (int i=0;i<2;i++)   {   for...

testthread

class RunnableDemo implements Runnable {    private Thread t;    private String threadName;      RunnableDemo( String name) {       threadName = name;       System.out.println("Creating " +  threadName );    }      public void run() {       System.out.println("Running " +  threadName );       try {          for(int i = 4; i > 0; i--) {             System.out.println("Thread: " + threadName + ", " + i);             // Let the thread sleep for a while.             Thread.sleep(50);          }       } catch (InterruptedException e) {          System.out.println("Thread " +  threadName + " interrupted.");       }       System.ou...

tri avec multithread

/**  * Tri d'un tableau d'entiers multi-thread.  * Utilisation de wait() et notifyAll() au lieu de join()  */ public class Trieur extends Thread {   private int[] t; // tableau a trier   private int debut, fin; // tranche de ce tableau qu'il faut trier   private Trieur parent;  // tread Trieur qui a lance ce (this) Trieur   private int nbNotify = 0; // Nombre de notifys envoyes a ce (this) Trieur   public Trieur(int[] t) {     this(null, t, 0, t.length - 1);   }     private Trieur(Trieur parent, int[] t, int debut, int fin) {     this.parent = parent;     this.t = t;     this.debut = debut;     this.fin = fin;     start();   }   public synchronized void notifier() {     // Notifie tous les thread en attente sur "ce" (this) moniteur     // Attention, quand le message sera envoye au parent (dans run()),   ...

lecture d'un fichier optimale

import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.FileReader; import java.io.LineNumberReader; import java.io.FileNotFoundException; import java.io.IOException; public class test1 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String titleAndBodyContainer; titleAndBodyContainer ="le travail est fait." + "fsdjhskdhjdk?"; //découpage de la phrase. String[] sentenceHolder = titleAndBodyContainer.split("[.?!][^A-Z0-9]"); for(int i=0;i< sentenceHolder.length;i++ ) { System.out.println(sentenceHolder[i]); } int i=0; try{ FileReader fr = null ; // ouverture du flux de lecture // peut jeter une FileNotFoundException   fr =  new FileReader("c://test.txt") ;   // ouverture d'un flux sur le flux de lecture du ...

Palindrome

//Ceci importe la classe Scanner du package java.util import java.util.Scanner; public class Palindrome { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.println("Veuillez saisir un mot :"); String pal = sc.nextLine(); System.out.println("Vous avez saisi : " + pal); //String pal ="RADAR"; int i = 0; boolean test = true; while ( i<pal.length()/2 && test) { if(!pal.substring(i,i+1).equalsIgnoreCase(pal.substring(pal.length()-i-1,pal.length()-i-1+1))) { test= false; } i++; //if(pal.substring(i,1).equals( )) //} } if(test) { System.out.println("palindrome"); } else{ System.out.println("non palindrome"); } } }

lecture fichier

import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class FileInput {   public static void main(String[] args) {     File file = new File("C:\\Test.txt");     FileInputStream fis = null;     BufferedInputStream bis = null;     DataInputStream dis = null;     try {       fis = new FileInputStream(file);        // Here BufferedInputStream is added for fast reading.       bis = new BufferedInputStream(fis);       dis = new DataInputStream(bis);  int i = 0;  int j=0;  String [][] Tab ;       // dis.available() returns 0 if the file does not have more lines.           int nbmax=0;       String[] TEST = new String[20];     ...

factorielle

public class Factorielle { /** * @param args */ public static int Calc_Fact(int nb){ int total=1; for(int i=1;i<=nb;i++) { total = total * i; } return total; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(Calc_Fact(5)); } }

Multi threading

Classe de tri public class Tri extends Thread { public int []t; public int ind; public int min ; public int ret=0; public int indret; public Tri(int[]tab,int Indice,int min) { this.t=tab; this.ind=Indice; this.min=min; this.ret=0; start(); }    public void run() {        for(int i =this.ind;i<this.t.length ;i++)      {     if(min > t[i])     {     min = t[i];     indret =i;       ret = -1;     }      }      } } ---- Classe de remplacement : public class Remplace extends Thread { public int []tab; public int indice1; public  int indice2; int var; public Remplace(int[] t ,int nb1,int nb2 ) { this.tab=t; this.indice1=nb1; this.indice2=nb2; start(); }    public void run() {           var=...