Skip to content

Commit 22310de

Browse files
Cleaned up code for some packages (#5094)
* Cleaned up code of some packages --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent 40cd4d8 commit 22310de

File tree

12 files changed

+23
-29
lines changed

12 files changed

+23
-29
lines changed

src/main/java/com/thealgorithms/conversions/IntegerToRoman.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public class IntegerToRoman {
1111

12-
private static int[] allArabianRomanNumbers = new int[] {
12+
private static final int[] allArabianRomanNumbers = new int[] {
1313
1000,
1414
900,
1515
500,
@@ -24,7 +24,7 @@ public class IntegerToRoman {
2424
4,
2525
1,
2626
};
27-
private static String[] allRomanNumbers = new String[] {
27+
private static final String[] allRomanNumbers = new String[] {
2828
"M",
2929
"CM",
3030
"D",

src/main/java/com/thealgorithms/conversions/RomanToInteger.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
public class RomanToInteger {
66

7-
private static Map<Character, Integer> map = new HashMap<Character, Integer>() {
8-
/**
9-
* */
7+
private static final Map<Character, Integer> map = new HashMap<>() {
108
private static final long serialVersionUID = 87605733047260530L;
119

1210
{

src/main/java/com/thealgorithms/conversions/TurkishToLatinConversion.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static String convertTurkishToLatin(String param) {
5858
'G',
5959
};
6060
for (int i = 0; i < turkishChars.length; i++) {
61-
param = param.replaceAll(new String(new char[] {turkishChars[i]}), new String(new char[] {latinChars[i]}));
61+
param = param.replaceAll(String.valueOf(turkishChars[i]), String.valueOf(latinChars[i]));
6262
}
6363
return param;
6464
}

src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public class Fibonacci {
1111

12-
private static Map<Integer, Integer> map = new HashMap<>();
12+
private static final Map<Integer, Integer> map = new HashMap<>();
1313

1414
public static void main(String[] args) {
1515
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
@@ -106,7 +106,6 @@ public static int fibOptimized(int n) {
106106
public static int fibBinet(int n) {
107107
double squareRootOf5 = Math.sqrt(5);
108108
double phi = (1 + squareRootOf5) / 2;
109-
int nthTerm = (int) ((Math.pow(phi, n) - Math.pow(-phi, -n)) / squareRootOf5);
110-
return nthTerm;
109+
return (int) ((Math.pow(phi, n) - Math.pow(-phi, -n)) / squareRootOf5);
111110
}
112111
}

src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
public class MatrixChainMultiplication {
88

9-
private static Scanner scan = new Scanner(System.in);
10-
private static ArrayList<Matrix> mArray = new ArrayList<>();
9+
private static final Scanner scan = new Scanner(System.in);
10+
private static final ArrayList<Matrix> mArray = new ArrayList<>();
1111
private static int size;
1212
private static int[][] m;
1313
private static int[][] s;
@@ -115,9 +115,9 @@ private static String[] input(String string) {
115115

116116
class Matrix {
117117

118-
private int count;
119-
private int col;
120-
private int row;
118+
private final int count;
119+
private final int col;
120+
private final int row;
121121

122122
Matrix(int count, int col, int row) {
123123
this.count = count;

src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
public class ActivitySelection {
1010
// Function to perform activity selection
11-
public static ArrayList<Integer> activitySelection(int startTimes[], int endTimes[]) {
11+
public static ArrayList<Integer> activitySelection(int[] startTimes, int[] endTimes) {
1212
int n = startTimes.length;
13-
int activities[][] = new int[n][3];
13+
int[][] activities = new int[n][3];
1414

1515
// Create a 2D array to store activities and their start/end times.
1616
// Each row: [activity index, start time, end time]

src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ public class CoinChange {
1010
// Function to solve the coin change problem
1111
public static ArrayList<Integer> coinChangeProblem(int amount) {
1212
// Define an array of coin denominations in descending order
13-
Integer coins[] = {1, 2, 5, 10, 20, 50, 100, 500, 2000};
13+
Integer[] coins = {1, 2, 5, 10, 20, 50, 100, 500, 2000};
1414

1515
// Sort the coin denominations in descending order
1616
Arrays.sort(coins, Comparator.reverseOrder());
1717

18-
int count = 0; // Variable to keep track of the total number of coins used
1918
ArrayList<Integer> ans = new ArrayList<>(); // List to store selected coins
2019

2120
// Iterate through the coin denominations
@@ -24,7 +23,6 @@ public static ArrayList<Integer> coinChangeProblem(int amount) {
2423
if (coins[i] <= amount) {
2524
// Repeatedly subtract the coin denomination from the remaining amount
2625
while (coins[i] <= amount) {
27-
count++; // Increment the count of coins used
2826
ans.add(coins[i]); // Add the coin to the list of selected coins
2927
amount -= coins[i]; // Update the remaining amount
3028
}

src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
public class FractionalKnapsack {
99
// Function to perform fractional knapsack
10-
public static int fractionalKnapsack(int weight[], int value[], int capacity) {
10+
public static int fractionalKnapsack(int[] weight, int[] value, int capacity) {
1111
// Create a 2D array to store item indices and their value-to-weight ratios.
12-
double ratio[][] = new double[weight.length][2];
12+
double[][] ratio = new double[weight.length][2];
1313

1414
// Populate the ratio array with item indices and their value-to-weight ratios.
1515
for (int i = 0; i < weight.length; i++) {

src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static String findJobSequence(ArrayList<Job> jobs, int size) {
3131
Boolean[] slots = new Boolean[size];
3232
Arrays.fill(slots, false);
3333

34-
int result[] = new int[size];
34+
int[] result = new int[size];
3535

3636
// Iterate through jobs to find the optimal job sequence
3737
for (int i = 0; i < size; i++) {

src/main/java/com/thealgorithms/searches/UnionFind.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
public class UnionFind {
66

7-
private int[] p;
8-
private int[] r;
7+
private final int[] p;
8+
private final int[] r;
99

1010
public UnionFind(int n) {
1111
p = new int[n];

src/main/java/com/thealgorithms/sorts/DNFSort.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class DNFSort {
77
static void sort012(int[] a, int arr_size) {
88
int low = 0;
99
int high = arr_size - 1;
10-
int mid = 0, temp = 0;
10+
int mid = 0, temp;
1111
while (mid <= high) {
1212
switch (a[mid]) {
1313
case 0: {
@@ -37,7 +37,7 @@ static void printArray(int[] arr, int arr_size) {
3737
for (int i = 0; i < arr_size; i++) {
3838
System.out.print(arr[i] + " ");
3939
}
40-
System.out.println("");
40+
System.out.println();
4141
}
4242

4343
/*Driver function to check for above functions*/

src/main/java/com/thealgorithms/strings/Pangram.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ public static void main(String[] args) {
2525
*/
2626
// alternative approach using Java Collection Framework
2727
public static boolean isPangramUsingSet(String s) {
28-
HashSet<Character> alpha = new HashSet<Character>();
28+
HashSet<Character> alpha = new HashSet<>();
2929
s = s.trim().toLowerCase();
3030
for (int i = 0; i < s.length(); i++)
3131
if (s.charAt(i) != ' ') alpha.add(s.charAt(i));
32-
if (alpha.size() == 26) return true;
33-
return false;
32+
return alpha.size() == 26;
3433
}
3534

3635
/**

0 commit comments

Comments
 (0)