---
# System prepended metadata

title: Java - Librairie de classes utiles
tags: [java, gobelins, POO]

---

# Java - Librairie de classes utiles

###### tags: `java` `POO` `gobelins`

## Calcul de Pi
```
public class Pi {
	public static void main(String[] args) {
		double pi = Math.sqrt(2);
		int iteration = 100;
		for (int i = 0; i < iteration; i++) {
			double pow = Math.pow(0.5 * pi,2);
			pi = Math.sqrt(pow + Math.pow(1-Math.sqrt(1-pow),2));
		}
		System.out.println(Math.pow(2,iteration +1)*pi);
	}
}
```

## Minimum d'un tableau
```
public class Minimum {
        static int MiniumTableau(int[] tableau) {
                if(tableau.length == 0){
                        throw new RuntimeException("Le tableau est vide");
                }
                int minimum = tableau[0];
                for(int i = 0; i < tableau.length; i++)
                {
                        if(tableau[i] < minimum){minimum = tableau[i];}
                }
                System.out.println(minimum);
                return minimum;
        }

        public static void main(String[] args) {
                int tableauEntier[] = {};
                MiniumTableau(tableauEntier);
        }
}
```

## Moindre carré
```
public class LeastSquare {
    public static void main(String[] args) {
        double tabx[] = {1,2,3};
        double taby[] = {1,1.5,1.78};

        int N = tabx.length;

        // Conditions
        if(tabx.length != taby.length){throw new RuntimeException("les tableaux ne sont pas de la même taille");}
        if(N == 0){throw new RuntimeException("Un des tableaux est vide");}

        // Calculate a and b
        double sumXperY = 0;
        double sumX = 0;
        double sumY = 0;
        double sumXsquare = 0;
        double squaresumX = 0;
        for(int i = 0; i < N; i++)
        {
            sumX += tabx[i];
            sumXsquare += Math.pow(tabx[i],2);
            sumY += taby[i];
            sumXperY += tabx[i] * taby[i];
        }
        squaresumX = Math.pow(sumX,2);
        double numerator = 2*sumXperY-(2.0/N)*sumX*sumY;
        double denominator = 2*sumXsquare-(2.0/N)*squaresumX;

        // Conditions
        if(denominator < 1e-6){throw new RuntimeException("Le carré n'est pas possible");}

        double a = numerator / denominator;
        double b = (1.0/N) * (sumY-a*sumX);
        System.out.println("a = "+a);
        System.out.println("b = "+b);

        // Calculate error
        double error = 0;
        for(int i = 0; i < N; i++)
        {
            error += Math.pow(a*tabx[i]+b-taby[i],2);
        }
        System.out.println("error = "+error);
    }
}
```
Les conditions sont testées pour assurer le bon fonctionnement du programme.

## Tri d'un tableau
```
import java.util.ArrayList;
public class SortingArray {
    public static void main(String[] args) {
        ArrayList<Integer> table = new ArrayList<Integer>();
        table.add(6);
        table.add(3);
        table.add(8);
        table.add(1);
        table.add(5);
        Sorting(table);
    }
    static void Sorting(ArrayList<Integer> array) {
        int length = array.size();
        if(length == 0){
            throw new RuntimeException("Le tableau est vide");
        }

        ArrayList<Integer> sortedTable = new ArrayList<Integer>();
        for(int i = 0; i < length; i++)
        {
            // Solution 1
            int val = array.get(getMinIndex(array,0));
            sortedTable.add(val);
            int index = searchIndex(array,val);
            array.remove(index);

            // Solution 2 (cassé)
            int indexMin = getMinIndex(theArray,i);
            int minimum = theArray.get(indexMin);
            swap(theArray,indexMin,minimum,i);

            System.out.println(sortedTable.get(i));
        }
    }
    static int getMinIndex(ArrayList<Integer> array, int n) {
        if(array.size() == 0){
            throw new RuntimeException("Le tableau est vide");
        }
        int minimum = array.get(n);
        int index = n;
        for(int i = n; i < array.size(); i++)
        {
            if(array.get(i) < minimum){
                minimum = array.get(i);
                index = i;
            }
        }
        return index;
    }
    static int searchIndex(ArrayList<Integer> array, int value){
        if(array.size() == 0){
            throw new RuntimeException("Le tableau est vide");
        }
        int index = -1;
        for(int i = 0; i < array.size(); i++)
        {
            if(array.get(i) == value){
                index = i;
            }
        }
        return index;
    }
    
    static void swap(ArrayList<Integer> array, int indexMin, int minimum, int position){
        array.set(indexMin,array.get(position));
        array.set(position,minimum);
    }
}
```
