Skip to content

Commit 05435c6

Browse files
Removed Main method and adjusted the test file
1 parent 3dc5f8e commit 05435c6

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5-
import java.util.Scanner;
65

76
/**
87
* TowerOfHanoi - Solves the classic Tower of Hanoi puzzle
@@ -17,12 +16,12 @@
1716
* Example: If n = 3, Source = 'A', Destination = 'C', Auxiliary = 'B'
1817
* Resulting moves will guide disks from A to C using B.
1918
*
20-
* @author justanothercoder
19+
* @author justanothercoder-hub
2120
* @see <a href="https://en.wikipedia.org/wiki/Tower_of_Hanoi">Tower of Hanoi</a>
2221
*/
23-
public final class TowerofHanoi {
22+
public final class TowerOfHanoi {
2423

25-
private TowerofHanoi() {
24+
private TowerOfHanoi() {
2625

2726
// Utility class
2827
}
@@ -38,7 +37,7 @@ private TowerofHanoi() {
3837
*/
3938
public static List<String> solveTowerOfHanoi(int n, char source, char destination, char auxiliary) {
4039
List<String> moves = new ArrayList<>();
41-
if(n < 0) {
40+
if (n < 0) {
4241
throw new IllegalArgumentException("Number of disks cannot be negative");
4342
}
4443
moveDisks(n, source, destination, auxiliary, moves);
@@ -63,15 +62,4 @@ private static void moveDisks(int n, char source, char destination, char auxilia
6362
moves.add("Move disk " + n + " from rod " + source + " to rod " + destination);
6463
moveDisks(n - 1, auxiliary, destination, source, moves);
6564
}
66-
67-
public static void main(String[] args) {
68-
Scanner scanner = new Scanner(System.in);
69-
System.out.print("Enter the number of disks: ");
70-
int n = scanner.nextInt();
71-
List<String> result = solveTowerOfHanoi(n, 'A', 'C', 'B');
72-
73-
for(String move : result){
74-
System.out.println(move);
75-
}
76-
}
77-
}
65+
}
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.thealgorithms.recursion;
22

3-
43
import static org.junit.jupiter.api.Assertions.assertEquals;
54
import static org.junit.jupiter.api.Assertions.assertThrows;
65

@@ -12,16 +11,15 @@ public class TowerOfHanoiTest {
1211

1312
/**
1413
* Test Case 1: Base Case (n = 1)
15-
* Requirement: Explicitly requested.
1614
* Logic: With only 1 disk, it should move directly from Source (A) to Destination (C).
1715
*/
1816
@Test
1917
public void testBaseCase() {
2018
List<String> result = TowerOfHanoi.solveTowerOfHanoi(1, 'A', 'C', 'B');
21-
19+
2220
// Assertion 1: Check size (2^1 - 1 = 1 move)
2321
assertEquals(1, result.size(), "Should have exactly 1 move for 1 disk");
24-
22+
2523
// Assertion 2: Verify the exact string
2624
assertEquals("Move disk 1 from rod A to rod C", result.get(0));
2725
}
@@ -33,16 +31,16 @@ public void testBaseCase() {
3331
@Test
3432
public void testSmallRecursion() {
3533
List<String> result = TowerOfHanoi.solveTowerOfHanoi(2, 'A', 'C', 'B');
36-
34+
3735
// Assertion 1: Check size (2^2 - 1 = 3 moves)
3836
assertEquals(3, result.size());
39-
37+
4038
// Assertion 2: Verify the exact sequence of moves
4139
List<String> expected = Arrays.asList("Move disk 1 from rod A to rod B", // Small disk to Aux
4240
"Move disk 2 from rod A to rod C", // Big disk to Dest
4341
"Move disk 1 from rod B to rod C" // Small disk to Dest
4442
);
45-
43+
4644
assertEquals(expected, result, "Sequence of moves for 2 disks is incorrect");
4745
}
4846

@@ -53,22 +51,21 @@ public void testSmallRecursion() {
5351
@Test
5452
public void testStandardCase() {
5553
List<String> result = TowerOfHanoi.solveTowerOfHanoi(3, 'A', 'C', 'B');
56-
54+
5755
// Assertion 1: Check size (2^3 - 1 = 7 moves)
5856
assertEquals(7, result.size());
59-
57+
6058
// Assertion 2: Verify start and end moves specifically
6159
assertEquals("Move disk 1 from rod A to rod C", result.get(0)); // First move
6260
assertEquals("Move disk 1 from rod A to rod C", result.get(6)); // Last move
6361
}
6462

6563
/**
66-
* Extra Test Case: Negative Input
67-
* Logic: Ensures your "Defensive Programming" check works.
64+
* Illegal Input Case: Negative Input
65+
* Logic: Ensures the exception is thrown when n is negative.
6866
*/
6967
@Test
7068
public void testNegativeInput() {
71-
assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); },
72-
"Should throw exception for negative disks");
69+
assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, "Should throw exception for negative disks");
7370
}
7471
}

0 commit comments

Comments
 (0)