diff --git a/checkstyle.xml b/checkstyle.xml index d16549b6..78ca487a 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -108,7 +108,7 @@ - + diff --git a/src/main/java/com/thealgorithms/backtracking/KnightsTour.java b/src/main/java/com/thealgorithms/backtracking/KnightsTour.java index 27b7f66c..6b3ec78a 100644 --- a/src/main/java/com/thealgorithms/backtracking/KnightsTour.java +++ b/src/main/java/com/thealgorithms/backtracking/KnightsTour.java @@ -26,8 +26,8 @@ import java.util.*; */ public class KnightsTour { - private static final int base = 12; - private static final int[][] moves = { + private static final int BASE = 12; + private static final int[][] MOVES = { {1, -2}, {2, -1}, {2, 1}, @@ -41,19 +41,19 @@ public class KnightsTour { private static int total; // total squares in chess public static void main(String[] args) { - grid = new int[base][base]; - total = (base - 4) * (base - 4); + grid = new int[BASE][BASE]; + total = (BASE - 4) * (BASE - 4); - for (int r = 0; r < base; r++) { - for (int c = 0; c < base; c++) { - if (r < 2 || r > base - 3 || c < 2 || c > base - 3) { + for (int r = 0; r < BASE; r++) { + for (int c = 0; c < BASE; c++) { + if (r < 2 || r > BASE - 3 || c < 2 || c > BASE - 3) { grid[r][c] = -1; } } } - int row = 2 + (int) (Math.random() * (base - 4)); - int col = 2 + (int) (Math.random() * (base - 4)); + int row = 2 + (int) (Math.random() * (BASE - 4)); + int col = 2 + (int) (Math.random() * (BASE - 4)); grid[row][col] = 1; @@ -95,7 +95,7 @@ public class KnightsTour { private static List neighbors(int row, int column) { List neighbour = new ArrayList<>(); - for (int[] m : moves) { + for (int[] m : MOVES) { int x = m[0]; int y = m[1]; if (grid[row + y][column + x] == 0) { @@ -109,7 +109,7 @@ public class KnightsTour { // Returns the total count of neighbors private static int countNeighbors(int row, int column) { int num = 0; - for (int[] m : moves) { + for (int[] m : MOVES) { if (grid[row + m[1]][column + m[0]] == 0) { num++; } diff --git a/src/main/java/com/thealgorithms/ciphers/Polybius.java b/src/main/java/com/thealgorithms/ciphers/Polybius.java index 7b5a2780..b28cac8a 100644 --- a/src/main/java/com/thealgorithms/ciphers/Polybius.java +++ b/src/main/java/com/thealgorithms/ciphers/Polybius.java @@ -15,7 +15,7 @@ package com.thealgorithms.ciphers; */ public class Polybius { - private static final char[][] key = { + private static final char[][] KEY = { // 0 1 2 3 4 /* 0 */ {'A', 'B', 'C', 'D', 'E'}, /* 1 */ {'F', 'G', 'H', 'I', 'J'}, @@ -26,9 +26,9 @@ public class Polybius { private static String findLocationByCharacter(final char character) { final StringBuilder location = new StringBuilder(); - for (int i = 0; i < key.length; i++) { - for (int j = 0; j < key[i].length; j++) { - if (character == key[i][j]) { + for (int i = 0; i < KEY.length; i++) { + for (int j = 0; j < KEY[i].length; j++) { + if (character == KEY[i][j]) { location.append(i).append(j); break; } @@ -53,7 +53,7 @@ public class Polybius { for (int i = 0; i < chars.length; i += 2) { int pozitionX = Character.getNumericValue(chars[i]); int pozitionY = Character.getNumericValue(chars[i + 1]); - plaintext.append(key[pozitionX][pozitionY]); + plaintext.append(KEY[pozitionX][pozitionY]); } return plaintext.toString(); } diff --git a/src/main/java/com/thealgorithms/conversions/DecimalToHexaDecimal.java b/src/main/java/com/thealgorithms/conversions/DecimalToHexaDecimal.java index 3564acbe..1435c7f8 100644 --- a/src/main/java/com/thealgorithms/conversions/DecimalToHexaDecimal.java +++ b/src/main/java/com/thealgorithms/conversions/DecimalToHexaDecimal.java @@ -3,10 +3,10 @@ package com.thealgorithms.conversions; // hex = [0 - 9] -> [A - F] class DecimalToHexaDecimal { - private static final int sizeOfIntInHalfBytes = 8; - private static final int numberOfBitsInAHalfByte = 4; - private static final int halfByte = 0x0F; - private static final char[] hexDigits = { + private static final int SIZE_OF_INT_IN_HALF_BYTES = 8; + private static final int NUMBER_OF_BITS_IN_HALF_BYTE = 4; + private static final int HALF_BYTE = 0x0F; + private static final char[] HEX_DIGITS = { '0', '1', '2', @@ -27,12 +27,12 @@ class DecimalToHexaDecimal { // Returns the hex value of the dec entered in the parameter. public static String decToHex(int dec) { - StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes); - hexBuilder.setLength(sizeOfIntInHalfBytes); - for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i) { - int j = dec & halfByte; - hexBuilder.setCharAt(i, hexDigits[j]); - dec >>= numberOfBitsInAHalfByte; + StringBuilder hexBuilder = new StringBuilder(SIZE_OF_INT_IN_HALF_BYTES); + hexBuilder.setLength(SIZE_OF_INT_IN_HALF_BYTES); + for (int i = SIZE_OF_INT_IN_HALF_BYTES - 1; i >= 0; --i) { + int j = dec & HALF_BYTE; + hexBuilder.setCharAt(i, HEX_DIGITS[j]); + dec >>= NUMBER_OF_BITS_IN_HALF_BYTE; } return hexBuilder.toString().toLowerCase(); } diff --git a/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java b/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java index aae0b3b2..5507ab11 100644 --- a/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java +++ b/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java @@ -9,7 +9,7 @@ package com.thealgorithms.conversions; */ public class IntegerToRoman { - private static final int[] allArabianRomanNumbers = new int[] { + private static final int[] ALL_ROMAN_NUMBERS_IN_ARABIC = new int[] { 1000, 900, 500, @@ -24,7 +24,7 @@ public class IntegerToRoman { 4, 1, }; - private static final String[] allRomanNumbers = new String[] { + private static final String[] ALL_ROMAN_NUMBERS = new String[] { "M", "CM", "D", @@ -48,13 +48,13 @@ public class IntegerToRoman { StringBuilder builder = new StringBuilder(); - for (int a = 0; a < allArabianRomanNumbers.length; a++) { - int times = num / allArabianRomanNumbers[a]; + for (int a = 0; a < ALL_ROMAN_NUMBERS_IN_ARABIC.length; a++) { + int times = num / ALL_ROMAN_NUMBERS_IN_ARABIC[a]; for (int b = 0; b < times; b++) { - builder.append(allRomanNumbers[a]); + builder.append(ALL_ROMAN_NUMBERS[a]); } - num -= times * allArabianRomanNumbers[a]; + num -= times * ALL_ROMAN_NUMBERS_IN_ARABIC[a]; } return builder.toString(); diff --git a/src/main/java/com/thealgorithms/conversions/RomanToInteger.java b/src/main/java/com/thealgorithms/conversions/RomanToInteger.java index d5dc7987..19535df7 100644 --- a/src/main/java/com/thealgorithms/conversions/RomanToInteger.java +++ b/src/main/java/com/thealgorithms/conversions/RomanToInteger.java @@ -4,9 +4,7 @@ import java.util.*; public class RomanToInteger { - private static final Map map = new HashMap<>() { - private static final long serialVersionUID = 87605733047260530L; - + private static final Map ROMAN_TO_INT = new HashMap<>() { { put('I', 1); put('V', 5); @@ -38,10 +36,10 @@ public class RomanToInteger { if (prev != ' ') { // checking current Number greater then previous or not - newPrev = map.get(prev) > newPrev ? map.get(prev) : newPrev; + newPrev = ROMAN_TO_INT.get(prev) > newPrev ? ROMAN_TO_INT.get(prev) : newPrev; } - int currentNum = map.get(c); + int currentNum = ROMAN_TO_INT.get(c); // if current number greater then prev max previous then add if (currentNum >= newPrev) { diff --git a/src/main/java/com/thealgorithms/datastructures/trees/LCA.java b/src/main/java/com/thealgorithms/datastructures/trees/LCA.java index d45f0d4f..c8bac470 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/LCA.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/LCA.java @@ -5,14 +5,14 @@ import java.util.Scanner; public class LCA { - private static final Scanner scanner = new Scanner(System.in); + private static final Scanner SCANNER = new Scanner(System.in); public static void main(String[] args) { // The adjacency list representation of a tree: ArrayList> adj = new ArrayList<>(); // v is the number of vertices and e is the number of edges - int v = scanner.nextInt(), e = v - 1; + int v = SCANNER.nextInt(), e = v - 1; for (int i = 0; i < v; i++) { adj.add(new ArrayList()); @@ -21,8 +21,8 @@ public class LCA { // Storing the given tree as an adjacency list int to, from; for (int i = 0; i < e; i++) { - to = scanner.nextInt(); - from = scanner.nextInt(); + to = SCANNER.nextInt(); + from = SCANNER.nextInt(); adj.get(to).add(from); adj.get(from).add(to); @@ -38,7 +38,7 @@ public class LCA { dfs(adj, 0, -1, parent, depth); // Inputting the two vertices whose LCA is to be calculated - int v1 = scanner.nextInt(), v2 = scanner.nextInt(); + int v1 = SCANNER.nextInt(), v2 = SCANNER.nextInt(); // Outputting the LCA System.out.println(getLCA(v1, v2, depth, parent)); diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java b/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java index c2e921c3..81dd504e 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java @@ -9,7 +9,7 @@ import java.util.Scanner; */ public class Fibonacci { - private static final Map map = new HashMap<>(); + private static final Map CACHE = new HashMap<>(); public static void main(String[] args) { // Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...] @@ -30,8 +30,8 @@ public class Fibonacci { * Outputs the nth fibonacci number */ public static int fibMemo(int n) { - if (map.containsKey(n)) { - return map.get(n); + if (CACHE.containsKey(n)) { + return CACHE.get(n); } int f; @@ -40,7 +40,7 @@ public class Fibonacci { f = n; } else { f = fibMemo(n - 1) + fibMemo(n - 2); - map.put(n, f); + CACHE.put(n, f); } return f; } diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java b/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java index af0447b9..3a4b687a 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java @@ -6,8 +6,8 @@ import java.util.Scanner; public class MatrixChainMultiplication { - private static final Scanner scan = new Scanner(System.in); - private static final ArrayList mArray = new ArrayList<>(); + private static final Scanner SCANNER = new Scanner(System.in); + private static final ArrayList MATRICES = new ArrayList<>(); private static int size; private static int[][] m; private static int[][] s; @@ -24,14 +24,14 @@ public class MatrixChainMultiplication { int row = Integer.parseInt(mSize[1]); Matrix matrix = new Matrix(count, col, row); - mArray.add(matrix); + MATRICES.add(matrix); count++; } - for (Matrix m : mArray) { + for (Matrix m : MATRICES) { System.out.format("A(%d) = %2d x %2d%n", m.count(), m.col(), m.row()); } - size = mArray.size(); + size = MATRICES.size(); m = new int[size + 1][size + 1]; s = new int[size + 1][size + 1]; p = new int[size + 1]; @@ -42,7 +42,7 @@ public class MatrixChainMultiplication { } for (int i = 0; i < p.length; i++) { - p[i] = i == 0 ? mArray.get(i).col() : mArray.get(i - 1).row(); + p[i] = i == 0 ? MATRICES.get(i).col() : MATRICES.get(i - 1).row(); } matrixChainOrder(); @@ -109,7 +109,7 @@ public class MatrixChainMultiplication { private static String[] input(String string) { System.out.print(string); - return (scan.nextLine().split(" ")); + return (SCANNER.nextLine().split(" ")); } } diff --git a/src/main/java/com/thealgorithms/maths/FindKthNumber.java b/src/main/java/com/thealgorithms/maths/FindKthNumber.java index bcb83b5e..0608cd0f 100644 --- a/src/main/java/com/thealgorithms/maths/FindKthNumber.java +++ b/src/main/java/com/thealgorithms/maths/FindKthNumber.java @@ -8,7 +8,7 @@ import java.util.Random; */ public class FindKthNumber { - private static final Random random = new Random(); + private static final Random RANDOM = new Random(); public static void main(String[] args) { /* generate an array with random size and random elements */ @@ -29,11 +29,11 @@ public class FindKthNumber { } private static int[] generateArray(int capacity) { - int size = random.nextInt(capacity) + 1; + int size = RANDOM.nextInt(capacity) + 1; int[] array = new int[size]; for (int i = 0; i < size; i++) { - array[i] = random.nextInt() % 100; + array[i] = RANDOM.nextInt() % 100; } return array; } diff --git a/src/main/java/com/thealgorithms/matrixexponentiation/Fibonacci.java b/src/main/java/com/thealgorithms/matrixexponentiation/Fibonacci.java index aa255832..b2e4576b 100644 --- a/src/main/java/com/thealgorithms/matrixexponentiation/Fibonacci.java +++ b/src/main/java/com/thealgorithms/matrixexponentiation/Fibonacci.java @@ -10,10 +10,10 @@ import java.util.Scanner; public class Fibonacci { // Exponentiation matrix for Fibonacci sequence - private static final int[][] fibMatrix = {{1, 1}, {1, 0}}; - private static final int[][] identityMatrix = {{1, 0}, {0, 1}}; + private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}}; + private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}}; // First 2 fibonacci numbers - private static final int[][] baseFibNumbers = {{1}, {0}}; + private static final int[][] BASE_FIB_NUMBERS = {{1}, {0}}; /** * Performs multiplication of 2 matrices @@ -53,14 +53,14 @@ public class Fibonacci { */ public static int[][] fib(int n) { if (n == 0) { - return Fibonacci.identityMatrix; + return Fibonacci.IDENTITY_MATRIX; } else { int[][] cachedResult = fib(n / 2); int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult); if (n % 2 == 0) { return matrixExpResult; } else { - return matrixMultiplication(Fibonacci.fibMatrix, matrixExpResult); + return matrixMultiplication(Fibonacci.FIB_MATRIX, matrixExpResult); } } } @@ -69,7 +69,7 @@ public class Fibonacci { // Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ] Scanner sc = new Scanner(System.in); int n = sc.nextInt(); - int[][] result = matrixMultiplication(fib(n), baseFibNumbers); + int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS); System.out.println("Fib(" + n + ") = " + result[1][0]); sc.close(); } diff --git a/src/main/java/com/thealgorithms/others/Conway.java b/src/main/java/com/thealgorithms/others/Conway.java index b1d54e61..db1c102f 100644 --- a/src/main/java/com/thealgorithms/others/Conway.java +++ b/src/main/java/com/thealgorithms/others/Conway.java @@ -13,7 +13,7 @@ public class Conway { *1s, two 2s, one 1" or 312211. https://en.wikipedia.org/wiki/Look-and-say_sequence * */ - private static final StringBuilder builder = new StringBuilder(); + private static final StringBuilder BUILDER = new StringBuilder(); protected static List generateList(String originalString, int maxIteration) { List numbers = new ArrayList<>(); @@ -25,9 +25,9 @@ public class Conway { } public static String generateNextElement(String originalString) { - builder.setLength(0); + BUILDER.setLength(0); String[] stp = originalString.split("(?<=(.))(?!\\1)"); - Arrays.stream(stp).forEach(s -> builder.append(s.length()).append(s.charAt(0))); - return builder.toString(); + Arrays.stream(stp).forEach(s -> BUILDER.append(s.length()).append(s.charAt(0))); + return BUILDER.toString(); } } diff --git a/src/main/java/com/thealgorithms/others/RabinKarp.java b/src/main/java/com/thealgorithms/others/RabinKarp.java index 8757f03b..6358ce4a 100644 --- a/src/main/java/com/thealgorithms/others/RabinKarp.java +++ b/src/main/java/com/thealgorithms/others/RabinKarp.java @@ -9,15 +9,15 @@ import java.util.Scanner; // Program will simply end if there is no match public class RabinKarp { - public static Scanner scanner = null; - public static final int d = 256; + public static Scanner SCANNER = null; + public static final int ALPHABET_SIZE = 256; public static void main(String[] args) { - scanner = new Scanner(System.in); + SCANNER = new Scanner(System.in); System.out.println("Enter String"); - String text = scanner.nextLine(); + String text = SCANNER.nextLine(); System.out.println("Enter pattern"); - String pattern = scanner.nextLine(); + String pattern = SCANNER.nextLine(); int q = 101; searchPat(text, pattern, q); @@ -32,14 +32,14 @@ public class RabinKarp { int j = 0; int i = 0; - h = (int) Math.pow(d, m - 1) % q; + h = (int) Math.pow(ALPHABET_SIZE, m - 1) % q; for (i = 0; i < m; i++) { // hash value is calculated for each character and then added with the hash value of the // next character for pattern as well as the text for length equal to the length of // pattern - p = (d * p + pattern.charAt(i)) % q; - t = (d * t + text.charAt(i)) % q; + p = (ALPHABET_SIZE * p + pattern.charAt(i)) % q; + t = (ALPHABET_SIZE * t + text.charAt(i)) % q; } for (i = 0; i <= n - m; i++) { @@ -67,7 +67,7 @@ public class RabinKarp { // value of the next character after the end of the evaluated characters is added to get // the hash value of the next window of characters in the text if (i < n - m) { - t = (d * (t - text.charAt(i) * h) + text.charAt(i + m)) % q; + t = (ALPHABET_SIZE * (t - text.charAt(i) * h) + text.charAt(i + m)) % q; // if hash value becomes less than zero than q is added to make it positive if (t < 0) { diff --git a/src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java b/src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java index e7745464..cc8387f6 100644 --- a/src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java +++ b/src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java @@ -6,8 +6,7 @@ public final class RabinKarpAlgorithm { private RabinKarpAlgorithm() { } - // d is the number of characters in the input alphabet - private static final int d = 256; + private static final int ALPHABET_SIZE = 256; public static int search(String pattern, String text, int primeNumber) { @@ -19,13 +18,13 @@ public final class RabinKarpAlgorithm { int h = 1; // The value of h would be "pow(d, patternLength-1)%primeNumber" - for (int i = 0; i < patternLength - 1; i++) h = (h * d) % primeNumber; + for (int i = 0; i < patternLength - 1; i++) h = (h * ALPHABET_SIZE) % primeNumber; // Calculate the hash value of pattern and first // window of text for (int i = 0; i < patternLength; i++) { - hashForPattern = (d * hashForPattern + pattern.charAt(i)) % primeNumber; - hashForText = (d * hashForText + text.charAt(i)) % primeNumber; + hashForPattern = (ALPHABET_SIZE * hashForPattern + pattern.charAt(i)) % primeNumber; + hashForText = (ALPHABET_SIZE * hashForText + text.charAt(i)) % primeNumber; } // Slide the pattern over text one by one @@ -51,7 +50,7 @@ public final class RabinKarpAlgorithm { // Calculate hash value for next window of text: Remove // leading digit, add trailing digit if (i < textLength - patternLength) { - hashForText = (d * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber; + hashForText = (ALPHABET_SIZE * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber; // handling negative hashForText if (hashForText < 0) hashForText = (hashForText + primeNumber); diff --git a/src/main/java/com/thealgorithms/sorts/BogoSort.java b/src/main/java/com/thealgorithms/sorts/BogoSort.java index 75f1e843..7a1f7b21 100644 --- a/src/main/java/com/thealgorithms/sorts/BogoSort.java +++ b/src/main/java/com/thealgorithms/sorts/BogoSort.java @@ -8,7 +8,7 @@ import java.util.Random; */ public class BogoSort implements SortAlgorithm { - private static final Random random = new Random(); + private static final Random RANDOM = new Random(); private static > boolean isSorted(T[] array) { for (int i = 0; i < array.length - 1; i++) { @@ -24,7 +24,7 @@ public class BogoSort implements SortAlgorithm { int length = array.length; for (int i = 0; i < array.length; i++) { - int randomIndex = i + random.nextInt(length - i); + int randomIndex = i + RANDOM.nextInt(length - i); SortUtils.swap(array, randomIndex, i); } } diff --git a/src/main/java/com/thealgorithms/sorts/SortUtilsRandomGenerator.java b/src/main/java/com/thealgorithms/sorts/SortUtilsRandomGenerator.java index e75b8e1e..d39d40e7 100644 --- a/src/main/java/com/thealgorithms/sorts/SortUtilsRandomGenerator.java +++ b/src/main/java/com/thealgorithms/sorts/SortUtilsRandomGenerator.java @@ -4,12 +4,12 @@ import java.util.Random; public class SortUtilsRandomGenerator { - private static final Random random; - private static final long seed; + private static final Random RANDOM; + private static final long SEED; static { - seed = System.currentTimeMillis(); - random = new Random(seed); + SEED = System.currentTimeMillis(); + RANDOM = new Random(SEED); } /** @@ -30,7 +30,7 @@ public class SortUtilsRandomGenerator { * @return Double value [0, 1) */ public static Double generateDouble() { - return random.nextDouble(); + return RANDOM.nextDouble(); } /** @@ -39,6 +39,6 @@ public class SortUtilsRandomGenerator { * @return int value [0, n) */ public static int generateInt(int n) { - return random.nextInt(n); + return RANDOM.nextInt(n); } }