Commit a2a4de5
committed
feat: Add ReverseANumber with overloaded methods to reverse integers and arrays
WHAT the code does:
Defines a ReverseANumber class with two overloaded reverse methods:
- reverse(int n): reverses the digits of a single integer using modulus and division.
- reverse(int[] A): creates a new array with elements in reverse order.
In main():
- Demonstrates reversing an integer (569 → 965).
- Demonstrates reversing an array {2, 4, 6, 7} → {7, 6, 4, 2}.
WHY this matters:
Shows method overloading with different parameter types (int vs int[]).
Demonstrates number reversal using arithmetic operations, reinforcing digit manipulation.
Illustrates array reversal using index-based traversal.
Provides an educational example bridging algorithmic logic with object-oriented design.
HOW it works:
For reverse(int n):
- Initialize rev = 0.
- While n != 0, repeatedly extract last digit (n % 10), append to rev, and reduce n by dividing by 10.
- Returns reversed number.
For reverse(int[] A):
- Creates a new array B of same length.
- Iterates from end of A to beginning, copying values into B in reverse order.
main() prints both original and reversed results for clarity.
Tips and gotchas:
The number reversal does not handle negatives; logic could be extended for sign handling.
Array reversal allocates a new array; in-place reversal could be more memory-efficient.
Method overloading improves readability but requires clear signatures to avoid confusion.
For production, prefer descriptive names like reverseNumber and reverseArray for clarity.
Use-cases:
Educational demonstration of method overloading in Java.
Utility functions for digit-based problems (palindrome checks, numeric transformations).
Reversing arrays for algorithmic needs (e.g., stack simulation, data reordering).
Foundation for extending into generic reversal utilities.
Short key: class-reverseanumber overloaded-reverse int-and-array.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent dc5bde9 commit a2a4de5
1 file changed
+6
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
13 | | - | |
| 11 | + | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
20 | 18 | | |
21 | | - | |
22 | | - | |
23 | | - | |
| 19 | + | |
24 | 20 | | |
25 | | - | |
26 | 21 | | |
27 | 22 | | |
28 | 23 | | |
29 | 24 | | |
30 | 25 | | |
31 | | - | |
32 | 26 | | |
33 | 27 | | |
34 | 28 | | |
35 | 29 | | |
36 | | - | |
37 | 30 | | |
38 | | - | |
39 | | - | |
40 | | - | |
| 31 | + | |
41 | 32 | | |
42 | 33 | | |
43 | 34 | | |
44 | 35 | | |
45 | | - | |
46 | 36 | | |
47 | | - | |
48 | | - | |
49 | | - | |
| 37 | + | |
50 | 38 | | |
51 | 39 | | |
52 | 40 | | |
53 | | - | |
| 41 | + | |
0 commit comments