Compare commits
10 Commits
8caae17dd4
...
fa5b8bf520
Author | SHA1 | Date | |
---|---|---|---|
fa5b8bf520 | |||
b964385405 | |||
ca9d0ec062 | |||
6eee9df1fe | |||
23829388cb | |||
64f7f56ffe | |||
51f5d352c6 | |||
7c614b0c5c | |||
3743b47887 | |||
94df35881e |
29
src/lab12_singleton/ex1/CarSensorSingletonLauncher.java
Normal file
29
src/lab12_singleton/ex1/CarSensorSingletonLauncher.java
Normal file
@ -0,0 +1,29 @@
|
||||
package lab12_singleton.ex1;
|
||||
|
||||
import lab12_singleton.ex1.sensors.*;
|
||||
import lab12_singleton.ex1.managers.*;
|
||||
|
||||
public class CarSensorSingletonLauncher {
|
||||
public CarSensorSingletonLauncher() {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CarSensorSingletonLauncher launcher = new CarSensorSingletonLauncher();
|
||||
launcher.launch();
|
||||
}
|
||||
|
||||
public void launch() {
|
||||
this.test(SimpleSingletonSensorsManager.getInstance());
|
||||
this.test(SynchSingletonSensorsManager.getInstance());
|
||||
this.test(EagerSingletonSensorsManager.getInstance());
|
||||
this.test(DbleCheckedSingletonSensorsManager.getInstance());
|
||||
}
|
||||
|
||||
private void test(SingletonSensorManager manager) {
|
||||
manager.addSensor(new TemperatureSensor());
|
||||
manager.addSensor(new SpeedSensor());
|
||||
manager.addSensor(new RoadConditionSensor());
|
||||
manager.printState();
|
||||
}
|
||||
}
|
15
src/lab12_singleton/ex1/Sensor.java
Normal file
15
src/lab12_singleton/ex1/Sensor.java
Normal file
@ -0,0 +1,15 @@
|
||||
package lab12_singleton.ex1;
|
||||
|
||||
public abstract class Sensor {
|
||||
private boolean ok = true;
|
||||
|
||||
public abstract String getName();
|
||||
public void setOk(boolean ok) {
|
||||
this.ok = ok;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName() + ": " + (ok ? "Ok" : "Warning");
|
||||
}
|
||||
}
|
17
src/lab12_singleton/ex1/SingletonSensorManager.java
Normal file
17
src/lab12_singleton/ex1/SingletonSensorManager.java
Normal file
@ -0,0 +1,17 @@
|
||||
package lab12_singleton.ex1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class SingletonSensorManager {
|
||||
private final ArrayList<Sensor> sensors = new ArrayList<>();
|
||||
|
||||
protected SingletonSensorManager() {}
|
||||
|
||||
public void addSensor(Sensor sensor) {
|
||||
sensors.add(sensor);
|
||||
}
|
||||
|
||||
public void printState() {
|
||||
System.out.println("<" + this.getClass().getSimpleName() + ": " + sensors.toString() + ">");
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package lab12_singleton.ex1.managers;
|
||||
|
||||
import lab12_singleton.ex1.SingletonSensorManager;
|
||||
|
||||
public class DbleCheckedSingletonSensorsManager extends SingletonSensorManager {
|
||||
private static DbleCheckedSingletonSensorsManager instance;
|
||||
|
||||
public static DbleCheckedSingletonSensorsManager getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (DbleCheckedSingletonSensorsManager.class) {
|
||||
if (instance == null) {
|
||||
instance = new DbleCheckedSingletonSensorsManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package lab12_singleton.ex1.managers;
|
||||
|
||||
import lab12_singleton.ex1.SingletonSensorManager;
|
||||
|
||||
public class EagerSingletonSensorsManager extends SingletonSensorManager {
|
||||
private static final EagerSingletonSensorsManager instance = new EagerSingletonSensorsManager();
|
||||
|
||||
public static EagerSingletonSensorsManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package lab12_singleton.ex1.managers;
|
||||
|
||||
import lab12_singleton.ex1.SingletonSensorManager;
|
||||
|
||||
public class SimpleSingletonSensorsManager extends SingletonSensorManager {
|
||||
private static SimpleSingletonSensorsManager instance;
|
||||
|
||||
public static SingletonSensorManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new SimpleSingletonSensorsManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package lab12_singleton.ex1.managers;
|
||||
|
||||
import lab12_singleton.ex1.SingletonSensorManager;
|
||||
|
||||
public class SynchSingletonSensorsManager extends SingletonSensorManager {
|
||||
private static SynchSingletonSensorsManager instance;
|
||||
|
||||
public static synchronized SynchSingletonSensorsManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new SynchSingletonSensorsManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
10
src/lab12_singleton/ex1/sensors/RoadConditionSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/RoadConditionSensor.java
Normal file
@ -0,0 +1,10 @@
|
||||
package lab12_singleton.ex1.sensors;
|
||||
|
||||
import lab12_singleton.ex1.Sensor;
|
||||
|
||||
public class RoadConditionSensor extends Sensor {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Road Condition";
|
||||
}
|
||||
}
|
10
src/lab12_singleton/ex1/sensors/SpeedSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/SpeedSensor.java
Normal file
@ -0,0 +1,10 @@
|
||||
package lab12_singleton.ex1.sensors;
|
||||
|
||||
import lab12_singleton.ex1.Sensor;
|
||||
|
||||
public class SpeedSensor extends Sensor {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Speed";
|
||||
}
|
||||
}
|
10
src/lab12_singleton/ex1/sensors/TemperatureSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/TemperatureSensor.java
Normal file
@ -0,0 +1,10 @@
|
||||
package lab12_singleton.ex1.sensors;
|
||||
|
||||
import lab12_singleton.ex1.Sensor;
|
||||
|
||||
public class TemperatureSensor extends Sensor {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Temperature";
|
||||
}
|
||||
}
|
27
src/lab13_proxy/ex1/Image.java
Normal file
27
src/lab13_proxy/ex1/Image.java
Normal file
@ -0,0 +1,27 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
public class Image {
|
||||
protected final String path;
|
||||
protected final String resolution;
|
||||
protected boolean loaded = false;
|
||||
|
||||
public Image(String path, String resolution) {
|
||||
this.path = path;
|
||||
this.resolution = resolution;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
if (!loaded) {
|
||||
System.out.println("Image " + path + " is loaded in " + resolution + " resolution");
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLoaded() {
|
||||
return loaded;
|
||||
}
|
||||
|
||||
public void showImage(User user) {
|
||||
System.out.println("Image " + path + " is shown in " + resolution + " resolution for user " + user.getName());
|
||||
}
|
||||
}
|
24
src/lab13_proxy/ex1/ImageProxy.java
Normal file
24
src/lab13_proxy/ex1/ImageProxy.java
Normal file
@ -0,0 +1,24 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
public class ImageProxy extends Image {
|
||||
private final Image lowResImage;
|
||||
private final Image highResImage;
|
||||
|
||||
public ImageProxy(String path) {
|
||||
super(path, "high");
|
||||
lowResImage = new Image(path, "low");
|
||||
highResImage = new Image(path, "high");
|
||||
}
|
||||
|
||||
public void showImage(User user) {
|
||||
System.out.println(user.getName() + " selects preview image " + path + " and wants to see its full resolution.");
|
||||
if (RegistrationService.isRegistered(user)) {
|
||||
highResImage.load();
|
||||
highResImage.showImage(user);
|
||||
} else {
|
||||
System.out.println("User " + user.getName() + " is not registered. Showing preview image in low resolution");
|
||||
lowResImage.load();
|
||||
lowResImage.showImage(user);
|
||||
}
|
||||
}
|
||||
}
|
19
src/lab13_proxy/ex1/Main.java
Normal file
19
src/lab13_proxy/ex1/Main.java
Normal file
@ -0,0 +1,19 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
User jean = new User("Jean");
|
||||
User paul = new User("Paul");
|
||||
User pierre = new User("Pierre");
|
||||
|
||||
RegistrationService.register(paul);
|
||||
|
||||
Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg");
|
||||
Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg");
|
||||
Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg");
|
||||
|
||||
highResolutionImage1.showImage(jean);
|
||||
highResolutionImage2.showImage(paul);
|
||||
highResolutionImage3.showImage(pierre);
|
||||
}
|
||||
}
|
15
src/lab13_proxy/ex1/RegistrationService.java
Normal file
15
src/lab13_proxy/ex1/RegistrationService.java
Normal file
@ -0,0 +1,15 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RegistrationService {
|
||||
private static final ArrayList<User> users = new ArrayList<>();
|
||||
public static void register(User user) {
|
||||
System.out.println(user.getName() + " is now registered");
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
public static boolean isRegistered(User user) {
|
||||
return users.contains(user);
|
||||
}
|
||||
}
|
13
src/lab13_proxy/ex1/User.java
Normal file
13
src/lab13_proxy/ex1/User.java
Normal file
@ -0,0 +1,13 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
public class User {
|
||||
private final String name;
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
36
src/lab13_proxy/ex2/Account.java
Normal file
36
src/lab13_proxy/ex2/Account.java
Normal file
@ -0,0 +1,36 @@
|
||||
package lab13_proxy.ex2;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Account {
|
||||
private int balance;
|
||||
private int accountNumber;
|
||||
private String owner;
|
||||
|
||||
public Account(String owner, int startBalance) {
|
||||
this.owner = owner;
|
||||
this.balance = startBalance;
|
||||
this.accountNumber = new Random().hashCode();
|
||||
}
|
||||
|
||||
public void deposit(int amount) {
|
||||
balance += amount;
|
||||
}
|
||||
|
||||
public void withdraw(int amount) {
|
||||
balance -= amount;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "account " + this.accountNumber + " belonging to " + this.owner;
|
||||
}
|
||||
}
|
52
src/lab13_proxy/ex2/Bank.java
Normal file
52
src/lab13_proxy/ex2/Bank.java
Normal file
@ -0,0 +1,52 @@
|
||||
package lab13_proxy.ex2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Bank {
|
||||
private final ArrayList<String> blacklistedClients = new ArrayList<>();
|
||||
|
||||
public void blacklist(String client) {
|
||||
blacklistedClients.add(client);
|
||||
}
|
||||
public boolean isBlacklisted(String client) {
|
||||
return blacklistedClients.contains(client);
|
||||
}
|
||||
|
||||
public void deposit(String client, Account account, int amount) {
|
||||
if (amount < 0) {
|
||||
System.out.println("Cannot deposit a negative amount");
|
||||
return;
|
||||
}
|
||||
if (isBlacklisted(client)) {
|
||||
System.out.println(client + " is on a blacklist and does not have the right to DEPOSIT money into " + account);
|
||||
return;
|
||||
}
|
||||
|
||||
account.deposit(amount);
|
||||
System.out.println(client + " has deposited " + amount + " on " + account + ". New balance is " + account.getBalance());
|
||||
}
|
||||
|
||||
public void withdraw(String client, Account account, int amount) {
|
||||
if (amount < 0) {
|
||||
System.out.println("Cannot withdraw a negative amount");
|
||||
return;
|
||||
}
|
||||
if (isBlacklisted(client)) {
|
||||
System.out.println(client + " is on a blacklist and does not have the right to WITHDRAW money from " + account);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!account.getOwner().equals(client)) {
|
||||
System.out.println(client + " cannot WITHDRAW money from " + account + " because they are not the owner");
|
||||
return;
|
||||
}
|
||||
|
||||
if (account.getBalance() < amount) {
|
||||
System.out.println(client + " cannot WITHDRAW money from " + account + " because there is not enough money on the account.");
|
||||
return;
|
||||
}
|
||||
|
||||
account.withdraw(amount);
|
||||
System.out.println(client + " has withdrawn " + amount + " from " + account + ". New balance is " + account.getBalance());
|
||||
}
|
||||
}
|
18
src/lab13_proxy/ex2/Main.java
Normal file
18
src/lab13_proxy/ex2/Main.java
Normal file
@ -0,0 +1,18 @@
|
||||
package lab13_proxy.ex2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Account account = new Account("Pascale", 16000);
|
||||
|
||||
Bank bank = new Bank();
|
||||
bank.blacklist("Jean");
|
||||
bank.blacklist("Pierre");
|
||||
|
||||
bank.deposit("Marcel", account, 1000);
|
||||
bank.deposit("Jean", account, 1000);
|
||||
bank.withdraw("Marcel", account, 1000);
|
||||
bank.withdraw("Pascale", account, 1000000);
|
||||
bank.withdraw("Pascale", account, 1000);
|
||||
bank.withdraw("Pierre", account, 1000);
|
||||
}
|
||||
}
|
8
src/lab14_strategy/ex1/AxeBehavior.java
Normal file
8
src/lab14_strategy/ex1/AxeBehavior.java
Normal file
@ -0,0 +1,8 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
public class AxeBehavior implements WeaponBehavior {
|
||||
@Override
|
||||
public String useWeapon() {
|
||||
return "chops with an axe";
|
||||
}
|
||||
}
|
19
src/lab14_strategy/ex1/Character.java
Normal file
19
src/lab14_strategy/ex1/Character.java
Normal file
@ -0,0 +1,19 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
public abstract class Character {
|
||||
private WeaponBehavior weapon;
|
||||
|
||||
public Character(WeaponBehavior weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
|
||||
public void setWeapon(WeaponBehavior weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public void fight() {
|
||||
System.out.println(getName() + " " + weapon.useWeapon());
|
||||
}
|
||||
}
|
25
src/lab14_strategy/ex1/Game.java
Normal file
25
src/lab14_strategy/ex1/Game.java
Normal file
@ -0,0 +1,25 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
import lab14_strategy.ex1.characters.King;
|
||||
import lab14_strategy.ex1.characters.Knight;
|
||||
import lab14_strategy.ex1.characters.Troll;
|
||||
import lab14_strategy.ex1.characters.Queen;
|
||||
import lab14_strategy.ex1.weapons.BowAndArrowBehavior;
|
||||
import lab14_strategy.ex1.weapons.KnifeBehavior;
|
||||
|
||||
public class Game {
|
||||
public static void main(String[] args) {
|
||||
Character king = new King(new KnifeBehavior());
|
||||
Character queen = new Queen(new BowAndArrowBehavior());
|
||||
Character knight = new Knight(new SwordBehavior());
|
||||
Character troll = new Troll(new AxeBehavior());
|
||||
|
||||
king.fight();
|
||||
queen.fight();
|
||||
knight.fight();
|
||||
troll.fight();
|
||||
|
||||
king.setWeapon(new SwordBehavior());
|
||||
king.fight();
|
||||
}
|
||||
}
|
8
src/lab14_strategy/ex1/SwordBehavior.java
Normal file
8
src/lab14_strategy/ex1/SwordBehavior.java
Normal file
@ -0,0 +1,8 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
public class SwordBehavior implements WeaponBehavior {
|
||||
@Override
|
||||
public String useWeapon() {
|
||||
return "swings a sword";
|
||||
}
|
||||
}
|
5
src/lab14_strategy/ex1/WeaponBehavior.java
Normal file
5
src/lab14_strategy/ex1/WeaponBehavior.java
Normal file
@ -0,0 +1,5 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
public interface WeaponBehavior {
|
||||
String useWeapon();
|
||||
}
|
15
src/lab14_strategy/ex1/characters/King.java
Normal file
15
src/lab14_strategy/ex1/characters/King.java
Normal file
@ -0,0 +1,15 @@
|
||||
package lab14_strategy.ex1.characters;
|
||||
|
||||
import lab14_strategy.ex1.Character;
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class King extends Character {
|
||||
public King(WeaponBehavior weapon) {
|
||||
super(weapon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "King";
|
||||
}
|
||||
}
|
15
src/lab14_strategy/ex1/characters/Knight.java
Normal file
15
src/lab14_strategy/ex1/characters/Knight.java
Normal file
@ -0,0 +1,15 @@
|
||||
package lab14_strategy.ex1.characters;
|
||||
|
||||
import lab14_strategy.ex1.Character;
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class Knight extends Character {
|
||||
public Knight(WeaponBehavior weapon) {
|
||||
super(weapon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Knight";
|
||||
}
|
||||
}
|
15
src/lab14_strategy/ex1/characters/Queen.java
Normal file
15
src/lab14_strategy/ex1/characters/Queen.java
Normal file
@ -0,0 +1,15 @@
|
||||
package lab14_strategy.ex1.characters;
|
||||
|
||||
import lab14_strategy.ex1.Character;
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class Queen extends Character {
|
||||
public Queen(WeaponBehavior weapon) {
|
||||
super(weapon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Queen";
|
||||
}
|
||||
}
|
15
src/lab14_strategy/ex1/characters/Troll.java
Normal file
15
src/lab14_strategy/ex1/characters/Troll.java
Normal file
@ -0,0 +1,15 @@
|
||||
package lab14_strategy.ex1.characters;
|
||||
|
||||
import lab14_strategy.ex1.Character;
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class Troll extends Character {
|
||||
public Troll(WeaponBehavior weapon) {
|
||||
super(weapon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Troll";
|
||||
}
|
||||
}
|
10
src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java
Normal file
10
src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java
Normal file
@ -0,0 +1,10 @@
|
||||
package lab14_strategy.ex1.weapons;
|
||||
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class BowAndArrowBehavior implements WeaponBehavior {
|
||||
@Override
|
||||
public String useWeapon() {
|
||||
return "shoots an arrow with a bow";
|
||||
}
|
||||
}
|
10
src/lab14_strategy/ex1/weapons/KnifeBehavior.java
Normal file
10
src/lab14_strategy/ex1/weapons/KnifeBehavior.java
Normal file
@ -0,0 +1,10 @@
|
||||
package lab14_strategy.ex1.weapons;
|
||||
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class KnifeBehavior implements WeaponBehavior {
|
||||
@Override
|
||||
public String useWeapon() {
|
||||
return "cuts with a knife";
|
||||
}
|
||||
}
|
5
src/lab14_strategy/ex2/SortAlgorithm.java
Normal file
5
src/lab14_strategy/ex2/SortAlgorithm.java
Normal file
@ -0,0 +1,5 @@
|
||||
package lab14_strategy.ex2;
|
||||
|
||||
public interface SortAlgorithm {
|
||||
void sort(int[] vector);
|
||||
}
|
21
src/lab14_strategy/ex2/Sorter.java
Normal file
21
src/lab14_strategy/ex2/Sorter.java
Normal file
@ -0,0 +1,21 @@
|
||||
package lab14_strategy.ex2;
|
||||
|
||||
public class Sorter {
|
||||
private SortAlgorithm algorithm;
|
||||
public void sort(int[] vector) {
|
||||
algorithm.sort(vector);
|
||||
}
|
||||
|
||||
public void setAlgorithm(SortAlgorithm algorithm) {
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
|
||||
public void showVectorData(int[] vector) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int j : vector) {
|
||||
sb.append(j).append("\t");
|
||||
}
|
||||
System.out.println(sb);
|
||||
}
|
||||
}
|
35
src/lab14_strategy/ex2/StrategySortLauncher.java
Normal file
35
src/lab14_strategy/ex2/StrategySortLauncher.java
Normal file
@ -0,0 +1,35 @@
|
||||
package lab14_strategy.ex2;
|
||||
|
||||
import lab14_strategy.ex2.algorithms.BubbleSort;
|
||||
import lab14_strategy.ex2.algorithms.InsertionSort;
|
||||
import lab14_strategy.ex2.algorithms.SelectionSort;
|
||||
|
||||
public class StrategySortLauncher {
|
||||
public static void main(String[] args) {
|
||||
StrategySortLauncher launcher = new StrategySortLauncher();
|
||||
launcher.test();
|
||||
}
|
||||
|
||||
public void test() {
|
||||
int[] tab1 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
|
||||
int[] tab2 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
|
||||
int[] tab3 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
|
||||
|
||||
Sorter sorter = new Sorter();
|
||||
|
||||
System.out.println("test bubble sort");
|
||||
sorter.setAlgorithm(new BubbleSort());
|
||||
sorter.sort(tab1);
|
||||
sorter.showVectorData(tab1);
|
||||
|
||||
System.out.println("test insert sort");
|
||||
sorter.setAlgorithm(new InsertionSort());
|
||||
sorter.sort(tab2);
|
||||
sorter.showVectorData(tab2);
|
||||
|
||||
System.out.println("test selection sort");
|
||||
sorter.setAlgorithm(new SelectionSort());
|
||||
sorter.sort(tab3);
|
||||
sorter.showVectorData(tab3);
|
||||
}
|
||||
}
|
23
src/lab14_strategy/ex2/algorithms/BubbleSort.java
Normal file
23
src/lab14_strategy/ex2/algorithms/BubbleSort.java
Normal file
@ -0,0 +1,23 @@
|
||||
package lab14_strategy.ex2.algorithms;
|
||||
|
||||
import lab14_strategy.ex2.SortAlgorithm;
|
||||
|
||||
public class BubbleSort implements SortAlgorithm {
|
||||
@Override
|
||||
public void sort(int[] vector) {
|
||||
int temp;
|
||||
int nbrePermutation = -1;
|
||||
int nbreIteration = 0;
|
||||
while (nbrePermutation != 0) {
|
||||
nbrePermutation = 0;
|
||||
for (int i=0; i<vector.length - nbreIteration - 1; i++) {
|
||||
if (vector[i] > vector[i + 1]) {
|
||||
nbrePermutation++;
|
||||
temp = vector[i + 1];
|
||||
vector[i + 1] = vector[i];
|
||||
vector[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
22
src/lab14_strategy/ex2/algorithms/InsertionSort.java
Normal file
22
src/lab14_strategy/ex2/algorithms/InsertionSort.java
Normal file
@ -0,0 +1,22 @@
|
||||
package lab14_strategy.ex2.algorithms;
|
||||
|
||||
import lab14_strategy.ex2.SortAlgorithm;
|
||||
|
||||
public class InsertionSort implements SortAlgorithm {
|
||||
@Override
|
||||
public void sort(int[] vector) {
|
||||
int temp;
|
||||
for (int i=1; i<vector.length; i++) {
|
||||
temp = vector[i];
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
if (vector[j] > temp) {
|
||||
vector[j + 1] = vector[j];
|
||||
vector[j] = temp;
|
||||
} else {
|
||||
vector[j + 1] = temp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
22
src/lab14_strategy/ex2/algorithms/SelectionSort.java
Normal file
22
src/lab14_strategy/ex2/algorithms/SelectionSort.java
Normal file
@ -0,0 +1,22 @@
|
||||
package lab14_strategy.ex2.algorithms;
|
||||
|
||||
import lab14_strategy.ex2.SortAlgorithm;
|
||||
|
||||
public class SelectionSort implements SortAlgorithm {
|
||||
@Override
|
||||
public void sort(int[] vector) {
|
||||
int temp, cursor = 0;
|
||||
for (int i=0; i<vector.length; i++) {
|
||||
temp = vector[i];
|
||||
cursor = i;
|
||||
for (int j = i; j < vector.length; j++) {
|
||||
if (vector[j] < temp) {
|
||||
temp = vector[j];
|
||||
cursor = j;
|
||||
}
|
||||
}
|
||||
vector[cursor] = vector[i];
|
||||
vector[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
24
src/lab15_observer/ex1/Main.java
Normal file
24
src/lab15_observer/ex1/Main.java
Normal file
@ -0,0 +1,24 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
PatientMonitoring pm = new PatientMonitoring(1, 140, 85);
|
||||
|
||||
MedicalEmployee jean = new MedicalEmployee("Jean", pm);
|
||||
MedicalEmployee pauline = new MedicalEmployee("Pauline", pm);
|
||||
MedicalEmployee matthieu = new MedicalEmployee("Matthieu", pm);
|
||||
MedicalEmployee symeon = new MedicalEmployee("Symeon", pm);
|
||||
|
||||
pm.setBloodPressure(110);
|
||||
pm.setPosition(3);
|
||||
|
||||
pm.setPulseOximetry(90);
|
||||
pm.setPulseOximetry(70);
|
||||
pm.setPosition(7);
|
||||
pm.setBloodPressure(150);
|
||||
|
||||
pm.removeObserver(matthieu);
|
||||
pm.setBloodPressure(145);
|
||||
pm.setPosition(9);
|
||||
}
|
||||
}
|
19
src/lab15_observer/ex1/MedicalEmployee.java
Normal file
19
src/lab15_observer/ex1/MedicalEmployee.java
Normal file
@ -0,0 +1,19 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
public class MedicalEmployee implements PatientObserver {
|
||||
private final String name;
|
||||
|
||||
public MedicalEmployee(String name, PatientMonitoring pm) {
|
||||
this.name = name;
|
||||
pm.registerObserver(this);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Problem problem, PatientMonitoring pm) {
|
||||
System.out.println(getName() + " has been notified of " + problem + " for patient " + pm);
|
||||
}
|
||||
}
|
70
src/lab15_observer/ex1/PatientMonitoring.java
Normal file
70
src/lab15_observer/ex1/PatientMonitoring.java
Normal file
@ -0,0 +1,70 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PatientMonitoring {
|
||||
private final List<PatientObserver> observers = new ArrayList<>();
|
||||
|
||||
private int position;
|
||||
private int bloodPressure;
|
||||
private int pulseOximetry;
|
||||
|
||||
public PatientMonitoring(int position, int bloodPressure, int pulseOximetry) {
|
||||
this.position = position;
|
||||
this.bloodPressure = bloodPressure;
|
||||
this.pulseOximetry = pulseOximetry;
|
||||
}
|
||||
|
||||
public void registerObserver(PatientObserver o) {
|
||||
observers.add(o);
|
||||
}
|
||||
|
||||
public void removeObserver(PatientObserver o) {
|
||||
observers.remove(o);
|
||||
}
|
||||
|
||||
private void notifyObservers(Problem problem) {
|
||||
for (PatientObserver o : observers) {
|
||||
o.update(problem, this);
|
||||
}
|
||||
}
|
||||
|
||||
public int getPosition() {
|
||||
return position;
|
||||
}
|
||||
public int getBloodPressure() {
|
||||
return bloodPressure;
|
||||
}
|
||||
public int getPulseOximetry() {
|
||||
return pulseOximetry;
|
||||
}
|
||||
|
||||
public void setPosition(int position) {
|
||||
this.position = position;
|
||||
notifyObservers(Problem.NO_PROBLEM);
|
||||
}
|
||||
|
||||
public void setBloodPressure(int bloodPressure) {
|
||||
this.bloodPressure = bloodPressure;
|
||||
if (getBloodPressure() > 145) {
|
||||
notifyObservers(Problem.BLOOD_PRESSURE);
|
||||
} else {
|
||||
notifyObservers(Problem.NO_PROBLEM);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPulseOximetry(int pulseOximetry) {
|
||||
this.pulseOximetry = pulseOximetry;
|
||||
if (getPulseOximetry() > 85) {
|
||||
notifyObservers(Problem.OXIMETRY);
|
||||
} else {
|
||||
notifyObservers(Problem.NO_PROBLEM);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<Patient Position: " + position + ", BP: " + bloodPressure + ", PO: " + pulseOximetry + ">";
|
||||
}
|
||||
}
|
5
src/lab15_observer/ex1/PatientObserver.java
Normal file
5
src/lab15_observer/ex1/PatientObserver.java
Normal file
@ -0,0 +1,5 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
public interface PatientObserver {
|
||||
void update(Problem problem, PatientMonitoring pm);
|
||||
}
|
7
src/lab15_observer/ex1/Problem.java
Normal file
7
src/lab15_observer/ex1/Problem.java
Normal file
@ -0,0 +1,7 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
public enum Problem {
|
||||
NO_PROBLEM,
|
||||
BLOOD_PRESSURE,
|
||||
OXIMETRY
|
||||
}
|
103
src/lab15_observer/ex2/AnalogTimer.java
Normal file
103
src/lab15_observer/ex2/AnalogTimer.java
Normal file
@ -0,0 +1,103 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public class AnalogTimer implements TimerObserver {
|
||||
private final MyTimer timer;
|
||||
private static final String[][] digits = {
|
||||
{
|
||||
"┌─┐",
|
||||
"│ │",
|
||||
"└─┘"
|
||||
},
|
||||
{
|
||||
" ╷",
|
||||
" │",
|
||||
" ╵"
|
||||
},
|
||||
{
|
||||
"╶─┐",
|
||||
"┌─┘",
|
||||
"└─╴"
|
||||
},
|
||||
{
|
||||
"╶─┐",
|
||||
" ─┤",
|
||||
"╶─┘"
|
||||
},
|
||||
{
|
||||
"╷ ╷",
|
||||
"└─┤",
|
||||
" ╵"
|
||||
},
|
||||
{
|
||||
"┌─╴",
|
||||
"└─┐",
|
||||
"╶─┘"
|
||||
},
|
||||
{
|
||||
"┌─╴",
|
||||
"├─┐",
|
||||
"└─┘"
|
||||
},
|
||||
{
|
||||
"╶─┐",
|
||||
" │",
|
||||
" ╵"
|
||||
},
|
||||
{
|
||||
"┌─┐",
|
||||
"├─┤",
|
||||
"└─┘"
|
||||
},
|
||||
{
|
||||
"┌─┐",
|
||||
"└─┤",
|
||||
"╶─┘"
|
||||
},
|
||||
};
|
||||
|
||||
public AnalogTimer(MyTimer timer) {
|
||||
this.timer = timer;
|
||||
timer.registerObserver(this);
|
||||
}
|
||||
|
||||
public void update(int hours, int minutes, int seconds) {
|
||||
String[] lines = {"", "", ""};
|
||||
|
||||
addDigit(lines, hours / 10);
|
||||
addSpace(lines);
|
||||
addDigit(lines, hours % 10);
|
||||
|
||||
addSep(lines);
|
||||
|
||||
addDigit(lines, minutes / 10);
|
||||
addSpace(lines);
|
||||
addDigit(lines, minutes % 10);
|
||||
|
||||
addSep(lines);
|
||||
|
||||
addDigit(lines, seconds / 10);
|
||||
addSpace(lines);
|
||||
addDigit(lines, seconds % 10);
|
||||
|
||||
System.out.println(lines[0]);
|
||||
System.out.println(lines[1]);
|
||||
System.out.println(lines[2]);
|
||||
}
|
||||
|
||||
private void addDigit(String[] lines, int digit) {
|
||||
String[] parts = digits[digit];
|
||||
for (int i = 0; i < 3; i++) {
|
||||
lines[i] += parts[i];
|
||||
}
|
||||
}
|
||||
private void addSpace(String[] lines) {
|
||||
lines[0] += " ";
|
||||
lines[1] += " ";
|
||||
lines[2] += " ";
|
||||
}
|
||||
private void addSep(String[] lines) {
|
||||
lines[0] += " o ";
|
||||
lines[1] += " ";
|
||||
lines[2] += " o ";
|
||||
}
|
||||
}
|
8
src/lab15_observer/ex2/ClockTimer.java
Normal file
8
src/lab15_observer/ex2/ClockTimer.java
Normal file
@ -0,0 +1,8 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public interface ClockTimer {
|
||||
int getHour();
|
||||
int getMinute();
|
||||
int getSecond();
|
||||
void tick();
|
||||
}
|
15
src/lab15_observer/ex2/DigitalTimer.java
Normal file
15
src/lab15_observer/ex2/DigitalTimer.java
Normal file
@ -0,0 +1,15 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public class DigitalTimer implements TimerObserver {
|
||||
private final MyTimer timer;
|
||||
|
||||
public DigitalTimer(MyTimer timer) {
|
||||
this.timer = timer;
|
||||
timer.registerObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(int hours, int minutes, int seconds) {
|
||||
System.out.printf("Time: %02d:%02d:%02d%n", hours, minutes, seconds);
|
||||
}
|
||||
}
|
10
src/lab15_observer/ex2/Main.java
Normal file
10
src/lab15_observer/ex2/Main.java
Normal file
@ -0,0 +1,10 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
MyTimer clockTimer = new MyTimer();
|
||||
|
||||
new AnalogTimer(clockTimer);
|
||||
new DigitalTimer(clockTimer);
|
||||
}
|
||||
}
|
53
src/lab15_observer/ex2/MyTimer.java
Normal file
53
src/lab15_observer/ex2/MyTimer.java
Normal file
@ -0,0 +1,53 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class MyTimer implements ClockTimer {
|
||||
private final List<TimerObserver> observers = new ArrayList<>();
|
||||
private Calendar cal;
|
||||
private Timer timer;
|
||||
|
||||
public MyTimer() {
|
||||
cal = Calendar.getInstance();
|
||||
timer = new Timer();
|
||||
timer.schedule(new TimerAction(), 0, 1000);
|
||||
}
|
||||
|
||||
class TimerAction extends TimerTask {
|
||||
@Override
|
||||
public void run() {
|
||||
cal = Calendar.getInstance();
|
||||
tick();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHour() {
|
||||
return cal.get(Calendar.HOUR_OF_DAY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinute() {
|
||||
return cal.get(Calendar.MINUTE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSecond() {
|
||||
return cal.get(Calendar.SECOND);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
for (TimerObserver o : observers) {
|
||||
o.update(getHour(), getMinute(), getSecond());
|
||||
}
|
||||
}
|
||||
|
||||
public void registerObserver(TimerObserver o) {
|
||||
observers.add(o);
|
||||
}
|
||||
|
||||
public void removeObserver(TimerObserver o) {
|
||||
observers.remove(o);
|
||||
}
|
||||
}
|
5
src/lab15_observer/ex2/TimerObserver.java
Normal file
5
src/lab15_observer/ex2/TimerObserver.java
Normal file
@ -0,0 +1,5 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public interface TimerObserver{
|
||||
void update(int hours, int minutes, int seconds);
|
||||
}
|
15
src/learn/simple_observer/ConcreteObserver.java
Normal file
15
src/learn/simple_observer/ConcreteObserver.java
Normal file
@ -0,0 +1,15 @@
|
||||
package learn.simple_observer;
|
||||
|
||||
public class ConcreteObserver implements Observer {
|
||||
private ConcreteSubject subject;
|
||||
|
||||
public ConcreteObserver(ConcreteSubject subject) {
|
||||
this.subject = subject;
|
||||
subject.registerObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
System.out.println("Observer was notified: subject has new state: " + subject.getState());
|
||||
}
|
||||
}
|
37
src/learn/simple_observer/ConcreteSubject.java
Normal file
37
src/learn/simple_observer/ConcreteSubject.java
Normal file
@ -0,0 +1,37 @@
|
||||
package learn.simple_observer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ConcreteSubject implements Subject {
|
||||
private List<Observer> observers = new ArrayList<>();
|
||||
int state;
|
||||
|
||||
@Override
|
||||
public void registerObserver(Observer o) {
|
||||
observers.add(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeObserver(Observer o) {
|
||||
observers.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyObservers() {
|
||||
for (Observer o : observers) {
|
||||
o.update();
|
||||
}
|
||||
}
|
||||
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
if (state > 50) {
|
||||
notifyObservers();
|
||||
}
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
12
src/learn/simple_observer/Main.java
Normal file
12
src/learn/simple_observer/Main.java
Normal file
@ -0,0 +1,12 @@
|
||||
package learn.simple_observer;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
ConcreteSubject subject = new ConcreteSubject();
|
||||
Observer observer = new ConcreteObserver(subject);
|
||||
subject.setState(40);
|
||||
subject.setState(100);
|
||||
Observer observer2 = new ConcreteObserver(subject);
|
||||
subject.setState(200);
|
||||
}
|
||||
}
|
5
src/learn/simple_observer/Observer.java
Normal file
5
src/learn/simple_observer/Observer.java
Normal file
@ -0,0 +1,5 @@
|
||||
package learn.simple_observer;
|
||||
|
||||
public interface Observer {
|
||||
void update();
|
||||
}
|
7
src/learn/simple_observer/Subject.java
Normal file
7
src/learn/simple_observer/Subject.java
Normal file
@ -0,0 +1,7 @@
|
||||
package learn.simple_observer;
|
||||
|
||||
public interface Subject {
|
||||
void registerObserver(Observer o);
|
||||
void removeObserver(Observer o);
|
||||
void notifyObservers();
|
||||
}
|
8
src/learn/simple_proxy/Main.java
Normal file
8
src/learn/simple_proxy/Main.java
Normal file
@ -0,0 +1,8 @@
|
||||
package learn.simple_proxy;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Subject subject = new Proxy();
|
||||
subject.request();
|
||||
}
|
||||
}
|
11
src/learn/simple_proxy/Proxy.java
Normal file
11
src/learn/simple_proxy/Proxy.java
Normal file
@ -0,0 +1,11 @@
|
||||
package learn.simple_proxy;
|
||||
|
||||
public class Proxy implements Subject {
|
||||
private RealSubject realSubject = new RealSubject();
|
||||
|
||||
@Override
|
||||
public void request() {
|
||||
System.out.println("Proxy does some preliminary job and checks");
|
||||
realSubject.request();
|
||||
}
|
||||
}
|
8
src/learn/simple_proxy/RealSubject.java
Normal file
8
src/learn/simple_proxy/RealSubject.java
Normal file
@ -0,0 +1,8 @@
|
||||
package learn.simple_proxy;
|
||||
|
||||
public class RealSubject implements Subject {
|
||||
@Override
|
||||
public void request() {
|
||||
System.out.println("Real subject action");
|
||||
}
|
||||
}
|
5
src/learn/simple_proxy/Subject.java
Normal file
5
src/learn/simple_proxy/Subject.java
Normal file
@ -0,0 +1,5 @@
|
||||
package learn.simple_proxy;
|
||||
|
||||
public interface Subject {
|
||||
public void request();
|
||||
}
|
8
src/learn/simple_strategy/ConcreteStrategyA.java
Normal file
8
src/learn/simple_strategy/ConcreteStrategyA.java
Normal file
@ -0,0 +1,8 @@
|
||||
package learn.simple_strategy;
|
||||
|
||||
public class ConcreteStrategyA implements Strategy {
|
||||
@Override
|
||||
public void algorithm() {
|
||||
System.out.println("nice strategy A");
|
||||
}
|
||||
}
|
8
src/learn/simple_strategy/ConcreteStrategyB.java
Normal file
8
src/learn/simple_strategy/ConcreteStrategyB.java
Normal file
@ -0,0 +1,8 @@
|
||||
package learn.simple_strategy;
|
||||
|
||||
public class ConcreteStrategyB implements Strategy {
|
||||
@Override
|
||||
public void algorithm() {
|
||||
System.out.println("nice strategy B");
|
||||
}
|
||||
}
|
17
src/learn/simple_strategy/Context.java
Normal file
17
src/learn/simple_strategy/Context.java
Normal file
@ -0,0 +1,17 @@
|
||||
package learn.simple_strategy;
|
||||
|
||||
public class Context {
|
||||
private Strategy currentStrategy;
|
||||
|
||||
public Context(Strategy currentStrategy) {
|
||||
this.currentStrategy = currentStrategy;
|
||||
}
|
||||
|
||||
public void setCurrentStrategy(Strategy currentStrategy) {
|
||||
this.currentStrategy = currentStrategy;
|
||||
}
|
||||
|
||||
public void doSomeJob() {
|
||||
currentStrategy.algorithm();
|
||||
}
|
||||
}
|
11
src/learn/simple_strategy/Main.java
Normal file
11
src/learn/simple_strategy/Main.java
Normal file
@ -0,0 +1,11 @@
|
||||
package learn.simple_strategy;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Context context = new Context(new ConcreteStrategyB());
|
||||
|
||||
context.doSomeJob();
|
||||
context.setCurrentStrategy(new ConcreteStrategyA());
|
||||
context.doSomeJob();
|
||||
}
|
||||
}
|
5
src/learn/simple_strategy/Strategy.java
Normal file
5
src/learn/simple_strategy/Strategy.java
Normal file
@ -0,0 +1,5 @@
|
||||
package learn.simple_strategy;
|
||||
|
||||
public interface Strategy {
|
||||
void algorithm();
|
||||
}
|
Loading…
Reference in New Issue
Block a user