###### tags: `Algorithmes`
# résumer examen
## méthode pour échanger deux places dans un tableau
```
private static void swap(int[] tab, int pos1, int pos2) {
int temp;
temp = tab[pos1];
tab[pos1] = tab[pos2];
tab[pos2] = temp;
}
```
```
public static void main(String[] args) {
int[] tab = new int[4];
tab[1] = 1;
tab[2] = 3;
MyArrayUtils.swap(tab, 1, 2);
System.out.println(Arrays.toString(tab));
}
```
## méthode pour générer un array avec random numbers
```
public static int[] genrateRandomArray(int size){
int[] rdArr = new int[size];
int i = size;
while(i-->0)
{
rdArr[i] = (int) (size * Math.random());
}
return rdArr;
}
```
```
public static void main(String[] args) {
System.out.println(Arrays.toString(genrateRandomArray(100)));
}
```
## 4 moyens de faire la même chose avec JavaFX
### All in One
```
public class AllInOne extends Application implements EventHandler{
private Button button = new Button("Start");
private CheckBox checkBox = new CheckBox("ok");
@Override
public void start(Stage stage) throws Exception {
//FlowPane est déjà a déjà un layout définit
Pane pane = new FlowPane();
/* **ajouter chaque élément**
pane.getChildren().add(button);
pane.getChildren().add(checkBox);*/
// **ajouter tous les éléments sur une ligne**
pane.getChildren().addAll(checkBox, button);
// **il est possible de tous faire en une seule ligne mais c'est de la merde donc fais le pas**
//stage.setScene(new Scene(new FlowPane().getChildren().addAll(button, checkBox)));
//fait l'acction quand on presse sur le bouton
button.setOnAction(this);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
@Override
public void handle(Event event) {
//ça affiche hello quand on presse sur le bouton
//System.out.println("Hello");
// coche ou décoche le bouton quand on presse sur start
boolean ok=checkBox.isSelected();
checkBox.setSelected( !ok);
}
public static void main(String[] args) {
launch();
}
}
```
### Anonymous
```
public class Anonymous extends Application {
private Button button = new Button("Start");
private CheckBox checkBox = new CheckBox("ok");
@Override
public void start(Stage stage) throws Exception {
// déclarer une scene
//Pane pane = new Pane();
//FlowPane est déjà a déjà un layout définit
Pane pane = new FlowPane();
// ajouter tous les éléments sur une ligne
pane.getChildren().addAll(checkBox, button);
// **classe anonyme**
button.setOnAction(new EventHandler(){
@Override
public void handle(Event event) {
boolean ok=checkBox.isSelected();
checkBox.setSelected( !ok);
}
});
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
```
### Lambda
```
public class Lambda1 extends Application {
private Button button = new Button("Start");
private CheckBox checkBox = new CheckBox("ok");
@Override
public void start(Stage stage) throws Exception {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//FlowPane est déjà a déjà un layout définit
Pane pane = new FlowPane();
// ajouter tous les éléments sur une ligne
pane.getChildren().addAll(checkBox, button);
// calsse lamda
button.setOnAction((Event event) -> {
boolean ok=checkBox.isSelected();
checkBox.setSelected( !ok);
});
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
```
### Outer Inner Class
```
public class OuterInnerClass extends Application {
private Button button = new Button("Start");
private CheckBox checkBox = new CheckBox("ok");
@Override
public void start(Stage stage) throws Exception {
//FlowPane est déjà a déjà un layout définit
Pane pane = new FlowPane();
// ajouter tous les éléments sur une ligne
pane.getChildren().addAll(checkBox, button);
// parce qu'on a enlevé l'interface
EventHandler myEventHandler= new MyEventHandler();
button.setOnAction(myEventHandler);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public class MyEventHandler implements EventHandler
{
@Override
public void handle(Event event) {
boolean ok=checkBox.isSelected();
checkBox.setSelected( !ok);
}
/***** @Override
public void handle(Event event) {
//ça affiche hello quand on presse sur le bouton
//System.out.println("Hello");
// coche ou décoche le bouton quand on presse sur start
boolean ok=checkBox.isSelected();
checkBox.setSelected( !ok);
}*/
}
public static void main(String[] args) {
launch();
}
}
```
## Dice qui donne la valeur chaque fois qu'il y a une nouvelle valeur
### n°1 Java

### n°2 JavaFX

## être sur de pouvoir faire une seule instanciation (singletone)
### Option n°1
```
public class Singleton
{
private static Singleton instance;
private Singleton() // Private constructor
{ // prevents extern instant-
} // iation with new
public static Singleton getInstance()
{
if(instance==null)
{
instance=new Singleton();
}
return instance;
}
}
```
### Option n°2
```
public class Singleton
{
private static final Singleton INSTANCE = new Singleton();
private Singleton() // Private constructor
{ // prevents extern instant-
} // iation with new
public static Singleton getINSTANCE()
{
return INSTANCE;
}
}
```
## LottoMat
```
public class MyLottomat {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
LottoMat lottoMat = new LottoMat(20);
System.out.println("----- Lottomat ------");
System.out.println(lottoMat.getNumberList());
System.out.println(lottoMat.getList());
for (int i = 0; i < 6; i++) {
System.out.print("Return for next number");
in.nextLine();
System.out.println(lottoMat.getNextRandomBall());
}
System.out.print("Lotto Numbers:");
System.out.println(lottoMat.getLottoNumbers());
}
}
```
```
public class LottoMat {
int size;
int numberPicked;
int x=0;
final int MAX = 100, MIN = 1;
Set<Integer> tab = new HashSet<>();
List<Integer> tab2 = new ArrayList<Integer>();
List<Integer> list;
Stack<Integer> stack = new Stack();
public LottoMat(int size) {
this.size = size;
}
public Set<Integer> getNumberList() {
for (int i = this.size; i > 0; i--) {
tab.add((int) (Math.random() * (MAX - MIN + 1) + MIN));
}
return tab;
}
public List<Integer> getList() { //List<Integer>
list = new ArrayList<Integer>(tab);
Collections.shuffle(list);
return list;
}
public int getNextRandomBall() {
stack.push(list.get(x));
tab2.add(stack.peek());
numberPicked = stack.peek();
stack.pop();
x++;
return numberPicked;
}
public String getLottoNumbers() {
return tab2.toString();
}
}
```
## Java Collection Framework


### Stack
The stack is a linear data structure that is used to store the collection of objects. It is based on Last-In-First-Out (LIFO).

### Set
Basically, Set is a type of collection that does not allow duplicate elements. That means an element can only exist once in a Set. It models the set abstraction in mathematics.The following picture illustrates three sets of numbers in mathematics:

### Maps
A Map is an object that maps keys to values, or is a collection of attribute-value pairs. It models the function abstraction in mathematics. The following picture illustrates a map:

### Queue
Basically, a queue has a head and a tail. New elements are added to the tail, and to-be-processed elements are picked from the head. The following picture illustrates a typical queue:
