From bc4dfb6ef000b5c13102ec986fbd1da869661405 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 17 Dec 2024 13:49:14 +0100 Subject: [PATCH] added ex P --- src/exercises/ex_p/Drum.java | 13 +++++++++++ src/exercises/ex_p/Partition.java | 38 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/exercises/ex_p/Drum.java create mode 100644 src/exercises/ex_p/Partition.java diff --git a/src/exercises/ex_p/Drum.java b/src/exercises/ex_p/Drum.java new file mode 100644 index 0000000..19f257f --- /dev/null +++ b/src/exercises/ex_p/Drum.java @@ -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); + } +} diff --git a/src/exercises/ex_p/Partition.java b/src/exercises/ex_p/Partition.java new file mode 100644 index 0000000..9ef53b6 --- /dev/null +++ b/src/exercises/ex_p/Partition.java @@ -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); + } + } +}