###### tags: `Algorithmes` # cours résumé ## cours 16.03.2022 ### tips: - utilise ctrl+space pour trouver les variables/paramètres - **alt+shift+f** --> formatage du texte - toujours imporater les librairies Javafx - rename un project **ctrl+r** - run **f6** ### théorie - comme mqtt plusieurs peuvent écouter sur les événements. - pour communiquer entre les objects nous devons faire la communication avec des méthodes --> le récpeteur doit avoir la même méthode que celui qui envoie. - utiliser des interfaces pou rendres ces méthodes obligatoire ### what we done - new project sur netbeans - javafx #### ex1 ``` /* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package intro; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.layout.FlowPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; /** * * @author manuel.gerber */ 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 { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. // déclarer une scene //Pane pane = new Pane(); //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(); } } ``` 2 eme programm <b>OuterInnerClass.java</b> ``` /* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package intro; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.layout.FlowPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; /** * * @author manuel.gerber */ 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 { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. // déclarer une scene //Pane pane = new Pane(); //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))); // 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(); } } ``` ## cours 22.03.2022 ### class anonyme on fais une calsse anonyme dans l'action d'un bouton ``` button.setOnAction(new EventHandler(){ @Override public void handle(Event event) { boolean ok=checkBox.isSelected(); checkBox.setSelected( !ok); } }); ``` ### lampda expression ``` button.setOnAction((Event event) -> { boolean ok=checkBox.isSelected(); checkBox.setSelected( !ok); }); ``` ### anonymous program ``` /* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package intro; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.layout.FlowPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; /** * * @author manuel.gerber */ 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 { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. // déclarer une scene //Pane pane = new Pane(); //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))); // calsse 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(); } } ``` ### swap method MyArrayUtils.swap(a,2,3) a= array 2, position swaped 3, position swaped ## cours 23.03 ### tips pour introduire des méthodes (get,set,toString) clic droit **insert code** puis choisir ### random array solution ``` import java.util.Arrays; public class ArrayTools { public static int[] generateRandomArray(int size) { int a[] = new int[size]; int i = size; while (i-- > 0) { a[i] = (int) (size * Math.random()); } return a; } public static int[] generateRandomArrayWithoutDoubles(int size) { int a[] = new int[size]; for (int i = size - 1; i > 0; i--) { a[i] = i; } for (int i = size - 1; i-- > 0; i--) { int j = (int) (i * Math.random()); swap(a, i, j); } return a; } public static void swap(int[] list, int pos1, int pos2) { int mem; mem = list[pos1]; list[pos1] = list[pos2]; list[pos2] = mem; } public static void main(String[] args) { int[] arr = { 5, 2, 88, 33, 11 }; System.out.println(Arrays.toString(arr)); ArrayTools.swap(arr, 2, 3); System.out.println(Arrays.toString(arr)); Arrays.sort(arr); Arrays.fill(arr, 255); System.out.println(Arrays.toString(arr)); } } ``` ### stack - tableau linéaire de données ``` // Java code for stack implementation import java.io.*; import java.util.*; class Test { // Pushing element on the top of the stack static void stack_push(Stack<Integer> stack) { for(int i = 0; i < 5; i++) { stack.push(i); } } // Popping element from the top of the stack static void stack_pop(Stack<Integer> stack) { System.out.println("Pop Operation:"); for(int i = 0; i < 5; i++) { Integer y = (Integer) stack.pop(); System.out.println(y); } } // Displaying element on the top of the stack static void stack_peek(Stack<Integer> stack) { Integer element = (Integer) stack.peek(); System.out.println("Element on stack top: " + element); } // Searching element in the stack static void stack_search(Stack<Integer> stack, int element) { Integer pos = (Integer) stack.search(element); if(pos == -1) System.out.println("Element not found"); else System.out.println("Element is found at position: " + pos); } public static void main (String[] args) { Stack<Integer> stack = new Stack<Integer>(); stack_push(stack); stack_pop(stack); stack_push(stack); stack_peek(stack); stack_search(stack, 2); stack_search(stack, 6); } } ``` ``` Output: Pop Operation: 4 3 2 1 0 Element on stack top: 4 Element is found at position: 3 Element not found ``` ### MAP - Map est très utile pour les bases de données. - Permet d'enregistrer des données, comme pour un tableau. ![](https://i.imgur.com/MAIStb2.jpg) ### SET - il a bcp d'élément - une seule fois la même structure (dans une liste on peut avoir plusieur fois la même structure) ``` // Java program Illustrating Set Interface // Importing utility classes import java.util.*; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Demonstrating Set using HashSet // Declaring object of type String Set<String> hash_Set = new HashSet<String>(); // Adding elements to the Set // using add() method hash_Set.add("Geeks"); hash_Set.add("For"); hash_Set.add("Geeks"); hash_Set.add("Example"); hash_Set.add("Set"); // Printing elements of HashSet object System.out.println(hash_Set); } } ``` ``` Output [Set, Example, Geeks, For] ``` ## cours 06.04 ### méthode push/pull ![](https://i.imgur.com/cBd1P5a.png)