mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
style: do not suppress lossy-conversions
(#6206)
This commit is contained in:
1
pom.xml
1
pom.xml
@ -78,7 +78,6 @@
|
|||||||
<arg>-Xlint:-auxiliaryclass</arg>
|
<arg>-Xlint:-auxiliaryclass</arg>
|
||||||
<arg>-Xlint:-rawtypes</arg>
|
<arg>-Xlint:-rawtypes</arg>
|
||||||
<arg>-Xlint:-unchecked</arg>
|
<arg>-Xlint:-unchecked</arg>
|
||||||
<arg>-Xlint:-lossy-conversions</arg>
|
|
||||||
<arg>-Werror</arg>
|
<arg>-Werror</arg>
|
||||||
</compilerArgs>
|
</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
@ -9,6 +9,9 @@ package com.thealgorithms.ciphers;
|
|||||||
* @author khalil2535
|
* @author khalil2535
|
||||||
*/
|
*/
|
||||||
public class Caesar {
|
public class Caesar {
|
||||||
|
private static char normalizeShift(final int shift) {
|
||||||
|
return (char) (shift % 26);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt text by shifting every Latin char by add number shift for ASCII
|
* Encrypt text by shifting every Latin char by add number shift for ASCII
|
||||||
@ -19,7 +22,7 @@ public class Caesar {
|
|||||||
public String encode(String message, int shift) {
|
public String encode(String message, int shift) {
|
||||||
StringBuilder encoded = new StringBuilder();
|
StringBuilder encoded = new StringBuilder();
|
||||||
|
|
||||||
shift %= 26;
|
final char shiftChar = normalizeShift(shift);
|
||||||
|
|
||||||
final int length = message.length();
|
final int length = message.length();
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
@ -29,10 +32,10 @@ public class Caesar {
|
|||||||
char current = message.charAt(i); // Java law : char + int = char
|
char current = message.charAt(i); // Java law : char + int = char
|
||||||
|
|
||||||
if (isCapitalLatinLetter(current)) {
|
if (isCapitalLatinLetter(current)) {
|
||||||
current += shift;
|
current += shiftChar;
|
||||||
encoded.append((char) (current > 'Z' ? current - 26 : current)); // 26 = number of latin letters
|
encoded.append((char) (current > 'Z' ? current - 26 : current)); // 26 = number of latin letters
|
||||||
} else if (isSmallLatinLetter(current)) {
|
} else if (isSmallLatinLetter(current)) {
|
||||||
current += shift;
|
current += shiftChar;
|
||||||
encoded.append((char) (current > 'z' ? current - 26 : current)); // 26 = number of latin letters
|
encoded.append((char) (current > 'z' ? current - 26 : current)); // 26 = number of latin letters
|
||||||
} else {
|
} else {
|
||||||
encoded.append(current);
|
encoded.append(current);
|
||||||
@ -50,16 +53,16 @@ public class Caesar {
|
|||||||
public String decode(String encryptedMessage, int shift) {
|
public String decode(String encryptedMessage, int shift) {
|
||||||
StringBuilder decoded = new StringBuilder();
|
StringBuilder decoded = new StringBuilder();
|
||||||
|
|
||||||
shift %= 26;
|
final char shiftChar = normalizeShift(shift);
|
||||||
|
|
||||||
final int length = encryptedMessage.length();
|
final int length = encryptedMessage.length();
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
char current = encryptedMessage.charAt(i);
|
char current = encryptedMessage.charAt(i);
|
||||||
if (isCapitalLatinLetter(current)) {
|
if (isCapitalLatinLetter(current)) {
|
||||||
current -= shift;
|
current -= shiftChar;
|
||||||
decoded.append((char) (current < 'A' ? current + 26 : current)); // 26 = number of latin letters
|
decoded.append((char) (current < 'A' ? current + 26 : current)); // 26 = number of latin letters
|
||||||
} else if (isSmallLatinLetter(current)) {
|
} else if (isSmallLatinLetter(current)) {
|
||||||
current -= shift;
|
current -= shiftChar;
|
||||||
decoded.append((char) (current < 'a' ? current + 26 : current)); // 26 = number of latin letters
|
decoded.append((char) (current < 'a' ? current + 26 : current)); // 26 = number of latin letters
|
||||||
} else {
|
} else {
|
||||||
decoded.append(current);
|
decoded.append(current);
|
||||||
|
@ -56,7 +56,7 @@ public final class AliquotSum {
|
|||||||
// if n is a perfect square then its root was added twice in above loop, so subtracting root
|
// if n is a perfect square then its root was added twice in above loop, so subtracting root
|
||||||
// from sum
|
// from sum
|
||||||
if (root == (int) root) {
|
if (root == (int) root) {
|
||||||
sum -= root;
|
sum -= (int) root;
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ public final class PerfectNumber {
|
|||||||
// if n is a perfect square then its root was added twice in above loop, so subtracting root
|
// if n is a perfect square then its root was added twice in above loop, so subtracting root
|
||||||
// from sum
|
// from sum
|
||||||
if (root == (int) root) {
|
if (root == (int) root) {
|
||||||
sum -= root;
|
sum -= (int) root;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum == n;
|
return sum == n;
|
||||||
|
Reference in New Issue
Block a user