added ex P

This commit is contained in:
Louis Heredero 2024-12-17 13:49:14 +01:00
parent 1255f304fc
commit bc4dfb6ef0
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package exercises.ex_p;
public class Drum {
private final String name;
public Drum(String name) {
this.name = name;
}
public void play() {
System.out.println("Playing " + name);
}
}

View File

@ -0,0 +1,38 @@
package exercises.ex_p;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Partition {
public static void main(String[] args) {
Drum bassDrum = new Drum("Bass Drum");
Drum floortom = new Drum("Floor Tom");
Drum snareDrum = new Drum("Snare Drum");
Drum rackTom = new Drum("Rack Tom");
Drum hiHat = new Drum("Hi-Hat");
Drum crashCymbal = new Drum("Crash Cymbal");
Drum rideCymbal = new Drum("Ride Cymbal");
Drum splashCymbal = new Drum("Splash Cymbal");
Drum chinaCymbal = new Drum("China Cymbal");
ScheduledExecutorService executor = Executors.newScheduledThreadPool(9);
int initialDelay = 1000;
int quaver = 500; // croche
int crotchet = 1000; // noire
int minim = 2000; // blanche
TimeUnit unit = TimeUnit.MILLISECONDS;
Drum[] drums = {
bassDrum, floortom, snareDrum,
rackTom, hiHat, crashCymbal,
rideCymbal, splashCymbal, chinaCymbal
};
int[] notes = {quaver, crotchet, minim};
for (int i=0; i < 9; i++) {
Drum drum = drums[i];
int rhythm = notes[(int) (Math.random() * 3)];
executor.scheduleAtFixedRate(drum::play, initialDelay, rhythm, unit);
}
}
}