Repository: TheAlgorithms/Java
Pull request #5814 (2024-10-14 13:26:20)

feat: Add `SlackTimeScheduling` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 78ff31bf..804320a2 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -539,6 +539,7 @@
             * [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
             * [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
             * [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
+            * [SlackTimeScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SlackTimeScheduling.java)
             * [SRTFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java)
           * searches
             * [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/BinarySearch.java)
@@ -1134,6 +1135,7 @@
             * [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
             * [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
             * [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
+            * [SlackTimeSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java)
             * [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
           * searches
             * [BinarySearch2dArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java)
diff --git a/src/main/java/com/thealgorithms/scheduling/SlackTimeScheduling.java b/src/main/java/com/thealgorithms/scheduling/SlackTimeScheduling.java
new file mode 100644
index 00000000..bbfd36f0
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/SlackTimeScheduling.java
@@ -0,0 +1,64 @@
+package com.thealgorithms.scheduling;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * SlackTimeScheduling is an algorithm that prioritizes tasks based on their
+ * slack time, which is defined as the difference between the task's deadline
+ * and the time required to execute it. Tasks with less slack time are prioritized.
+ *
+ * Use Case: Real-time systems with hard deadlines, such as robotics or embedded systems.
+ *
+ * @author Hardvan
+ */
+public class SlackTimeScheduling {
+
+    static class Task {
+        String name;
+        int executionTime;
+        int deadline;
+
+        Task(String name, int executionTime, int deadline) {
+            this.name = name;
+            this.executionTime = executionTime;
+            this.deadline = deadline;
+        }
+
+        int getSlackTime() {
+            return deadline - executionTime;
+        }
+    }
+
+    private final List<Task> tasks;
+
+    public SlackTimeScheduling() {
+        tasks = new ArrayList<>();
+    }
+
+    /**
+     * Adds a task to the scheduler.
+     *
+     * @param name          the name of the task
+     * @param executionTime the time required to execute the task
+     * @param deadline      the deadline by which the task must be completed
+     */
+    public void addTask(String name, int executionTime, int deadline) {
+        tasks.add(new Task(name, executionTime, deadline));
+    }
+
+    /**
+     * Schedules the tasks based on their slack time.
+     *
+     * @return the order in which the tasks should be executed
+     */
+    public List<String> scheduleTasks() {
+        tasks.sort(Comparator.comparingInt(Task::getSlackTime));
+        List<String> scheduledOrder = new ArrayList<>();
+        for (Task task : tasks) {
+            scheduledOrder.add(task.name);
+        }
+        return scheduledOrder;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java
new file mode 100644
index 00000000..555a0941
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java
@@ -0,0 +1,48 @@
+package com.thealgorithms.scheduling;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.List;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class SlackTimeSchedulingTest {
+
+    private SlackTimeScheduling scheduler;
+
+    @BeforeEach
+    public void setup() {
+        scheduler = new SlackTimeScheduling();
+    }
+
+    @Test
+    public void testAddAndScheduleSingleTask() {
+        scheduler.addTask("Task1", 2, 5);
+        List<String> expected = List.of("Task1");
+        assertEquals(expected, scheduler.scheduleTasks());
+    }
+
+    @Test
+    public void testScheduleMultipleTasks() {
+        scheduler.addTask("Task1", 2, 5);
+        scheduler.addTask("Task2", 1, 4);
+        scheduler.addTask("Task3", 3, 7);
+        List<String> expected = List.of("Task1", "Task2", "Task3");
+        assertEquals(expected, scheduler.scheduleTasks());
+    }
+
+    @Test
+    public void testScheduleTasksWithSameSlackTime() {
+        scheduler.addTask("Task1", 2, 5);
+        scheduler.addTask("Task2", 3, 6);
+        scheduler.addTask("Task3", 1, 4);
+        List<String> expected = List.of("Task1", "Task2", "Task3");
+        assertEquals(expected, scheduler.scheduleTasks());
+    }
+
+    @Test
+    public void testEmptyScheduler() {
+        List<String> expected = List.of();
+        assertEquals(expected, scheduler.scheduleTasks());
+    }
+}