mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 05:22:39 +08:00
@ -30,13 +30,8 @@ public class AnyBaseToAnyBase {
|
||||
try {
|
||||
System.out.print("Enter number: ");
|
||||
n = in.next();
|
||||
System.out.print(
|
||||
"Enter beginning base (between " +
|
||||
MINIMUM_BASE +
|
||||
" and " +
|
||||
MAXIMUM_BASE +
|
||||
"): "
|
||||
);
|
||||
System.out.print("Enter beginning base (between " + MINIMUM_BASE + " and "
|
||||
+ MAXIMUM_BASE + "): ");
|
||||
b1 = in.nextInt();
|
||||
if (b1 > MAXIMUM_BASE || b1 < MINIMUM_BASE) {
|
||||
System.out.println("Invalid base!");
|
||||
@ -47,12 +42,7 @@ public class AnyBaseToAnyBase {
|
||||
continue;
|
||||
}
|
||||
System.out.print(
|
||||
"Enter end base (between " +
|
||||
MINIMUM_BASE +
|
||||
" and " +
|
||||
MAXIMUM_BASE +
|
||||
"): "
|
||||
);
|
||||
"Enter end base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): ");
|
||||
b2 = in.nextInt();
|
||||
if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE) {
|
||||
System.out.println("Invalid base!");
|
||||
|
@ -11,9 +11,7 @@ import java.util.ArrayList;
|
||||
public class DecimalToAnyBase {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(System.in)
|
||||
);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Enter the decimal input below: ");
|
||||
int decInput = Integer.parseInt(br.readLine());
|
||||
System.out.println();
|
||||
@ -22,15 +20,10 @@ public class DecimalToAnyBase {
|
||||
int base = Integer.parseInt(br.readLine());
|
||||
System.out.println();
|
||||
|
||||
System.out.println("Decimal Input" + " is: " + decInput);
|
||||
System.out.println(
|
||||
"Value of " +
|
||||
decInput +
|
||||
" in base " +
|
||||
base +
|
||||
" is: " +
|
||||
convertToAnyBase(decInput, base)
|
||||
);
|
||||
System.out.println("Decimal Input"
|
||||
+ " is: " + decInput);
|
||||
System.out.println("Value of " + decInput + " in base " + base
|
||||
+ " is: " + convertToAnyBase(decInput, base));
|
||||
|
||||
br.close();
|
||||
}
|
||||
|
@ -24,9 +24,7 @@ class DecimalToBinary {
|
||||
public static void conventionalConversion() {
|
||||
int n, b = 0, c = 0, d;
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.printf(
|
||||
"Conventional conversion.%n Enter the decimal number: "
|
||||
);
|
||||
System.out.printf("Conventional conversion.%n Enter the decimal number: ");
|
||||
n = input.nextInt();
|
||||
while (n != 0) {
|
||||
d = n % 2;
|
||||
|
@ -61,7 +61,8 @@ public class HexToOct {
|
||||
hexadecnum = scan.nextLine();
|
||||
|
||||
// first convert hexadecimal to decimal
|
||||
decnum = hex2decimal(hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
|
||||
decnum = hex2decimal(
|
||||
hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
|
||||
// variable decnum
|
||||
|
||||
// convert decimal to octal
|
||||
|
@ -19,69 +19,30 @@ public class RgbHsvConversion {
|
||||
// Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html
|
||||
|
||||
// Test hsvToRgb-method
|
||||
assert Arrays.equals(hsvToRgb(0, 0, 0), new int[] { 0, 0, 0 });
|
||||
assert Arrays.equals(hsvToRgb(0, 0, 1), new int[] { 255, 255, 255 });
|
||||
assert Arrays.equals(hsvToRgb(0, 1, 1), new int[] { 255, 0, 0 });
|
||||
assert Arrays.equals(hsvToRgb(60, 1, 1), new int[] { 255, 255, 0 });
|
||||
assert Arrays.equals(hsvToRgb(120, 1, 1), new int[] { 0, 255, 0 });
|
||||
assert Arrays.equals(hsvToRgb(240, 1, 1), new int[] { 0, 0, 255 });
|
||||
assert Arrays.equals(hsvToRgb(300, 1, 1), new int[] { 255, 0, 255 });
|
||||
assert Arrays.equals(
|
||||
hsvToRgb(180, 0.5, 0.5),
|
||||
new int[] { 64, 128, 128 }
|
||||
);
|
||||
assert Arrays.equals(
|
||||
hsvToRgb(234, 0.14, 0.88),
|
||||
new int[] { 193, 196, 224 }
|
||||
);
|
||||
assert Arrays.equals(
|
||||
hsvToRgb(330, 0.75, 0.5),
|
||||
new int[] { 128, 32, 80 }
|
||||
);
|
||||
assert Arrays.equals(hsvToRgb(0, 0, 0), new int[] {0, 0, 0});
|
||||
assert Arrays.equals(hsvToRgb(0, 0, 1), new int[] {255, 255, 255});
|
||||
assert Arrays.equals(hsvToRgb(0, 1, 1), new int[] {255, 0, 0});
|
||||
assert Arrays.equals(hsvToRgb(60, 1, 1), new int[] {255, 255, 0});
|
||||
assert Arrays.equals(hsvToRgb(120, 1, 1), new int[] {0, 255, 0});
|
||||
assert Arrays.equals(hsvToRgb(240, 1, 1), new int[] {0, 0, 255});
|
||||
assert Arrays.equals(hsvToRgb(300, 1, 1), new int[] {255, 0, 255});
|
||||
assert Arrays.equals(hsvToRgb(180, 0.5, 0.5), new int[] {64, 128, 128});
|
||||
assert Arrays.equals(hsvToRgb(234, 0.14, 0.88), new int[] {193, 196, 224});
|
||||
assert Arrays.equals(hsvToRgb(330, 0.75, 0.5), new int[] {128, 32, 80});
|
||||
|
||||
// Test rgbToHsv-method
|
||||
// approximate-assertions needed because of small deviations due to converting between
|
||||
// int-values and double-values.
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(0, 0, 0),
|
||||
new double[] { 0, 0, 0 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(255, 255, 255),
|
||||
new double[] { 0, 0, 1 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(255, 0, 0),
|
||||
new double[] { 0, 1, 1 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(255, 255, 0),
|
||||
new double[] { 60, 1, 1 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(0, 255, 0),
|
||||
new double[] { 120, 1, 1 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(0, 0, 255),
|
||||
new double[] { 240, 1, 1 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(255, 0, 255),
|
||||
new double[] { 300, 1, 1 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(64, 128, 128),
|
||||
new double[] { 180, 0.5, 0.5 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(193, 196, 224),
|
||||
new double[] { 234, 0.14, 0.88 }
|
||||
);
|
||||
assert approximatelyEqualHsv(
|
||||
rgbToHsv(128, 32, 80),
|
||||
new double[] { 330, 0.75, 0.5 }
|
||||
);
|
||||
assert approximatelyEqualHsv(rgbToHsv(0, 0, 0), new double[] {0, 0, 0});
|
||||
assert approximatelyEqualHsv(rgbToHsv(255, 255, 255), new double[] {0, 0, 1});
|
||||
assert approximatelyEqualHsv(rgbToHsv(255, 0, 0), new double[] {0, 1, 1});
|
||||
assert approximatelyEqualHsv(rgbToHsv(255, 255, 0), new double[] {60, 1, 1});
|
||||
assert approximatelyEqualHsv(rgbToHsv(0, 255, 0), new double[] {120, 1, 1});
|
||||
assert approximatelyEqualHsv(rgbToHsv(0, 0, 255), new double[] {240, 1, 1});
|
||||
assert approximatelyEqualHsv(rgbToHsv(255, 0, 255), new double[] {300, 1, 1});
|
||||
assert approximatelyEqualHsv(rgbToHsv(64, 128, 128), new double[] {180, 0.5, 0.5});
|
||||
assert approximatelyEqualHsv(rgbToHsv(193, 196, 224), new double[] {234, 0.14, 0.88});
|
||||
assert approximatelyEqualHsv(rgbToHsv(128, 32, 80), new double[] {330, 0.75, 0.5});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,35 +55,23 @@ public class RgbHsvConversion {
|
||||
*/
|
||||
public static int[] hsvToRgb(double hue, double saturation, double value) {
|
||||
if (hue < 0 || hue > 360) {
|
||||
throw new IllegalArgumentException(
|
||||
"hue should be between 0 and 360"
|
||||
);
|
||||
throw new IllegalArgumentException("hue should be between 0 and 360");
|
||||
}
|
||||
|
||||
if (saturation < 0 || saturation > 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"saturation should be between 0 and 1"
|
||||
);
|
||||
throw new IllegalArgumentException("saturation should be between 0 and 1");
|
||||
}
|
||||
|
||||
if (value < 0 || value > 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"value should be between 0 and 1"
|
||||
);
|
||||
throw new IllegalArgumentException("value should be between 0 and 1");
|
||||
}
|
||||
|
||||
double chroma = value * saturation;
|
||||
double hueSection = hue / 60;
|
||||
double secondLargestComponent =
|
||||
chroma * (1 - Math.abs(hueSection % 2 - 1));
|
||||
double secondLargestComponent = chroma * (1 - Math.abs(hueSection % 2 - 1));
|
||||
double matchValue = value - chroma;
|
||||
|
||||
return getRgbBySection(
|
||||
hueSection,
|
||||
chroma,
|
||||
matchValue,
|
||||
secondLargestComponent
|
||||
);
|
||||
return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -135,21 +84,15 @@ public class RgbHsvConversion {
|
||||
*/
|
||||
public static double[] rgbToHsv(int red, int green, int blue) {
|
||||
if (red < 0 || red > 255) {
|
||||
throw new IllegalArgumentException(
|
||||
"red should be between 0 and 255"
|
||||
);
|
||||
throw new IllegalArgumentException("red should be between 0 and 255");
|
||||
}
|
||||
|
||||
if (green < 0 || green > 255) {
|
||||
throw new IllegalArgumentException(
|
||||
"green should be between 0 and 255"
|
||||
);
|
||||
throw new IllegalArgumentException("green should be between 0 and 255");
|
||||
}
|
||||
|
||||
if (blue < 0 || blue > 255) {
|
||||
throw new IllegalArgumentException(
|
||||
"blue should be between 0 and 255"
|
||||
);
|
||||
throw new IllegalArgumentException("blue should be between 0 and 255");
|
||||
}
|
||||
|
||||
double dRed = (double) red / 255;
|
||||
@ -172,7 +115,7 @@ public class RgbHsvConversion {
|
||||
|
||||
hue = (hue + 360) % 360;
|
||||
|
||||
return new double[] { hue, saturation, value };
|
||||
return new double[] {hue, saturation, value};
|
||||
}
|
||||
|
||||
private static boolean approximatelyEqualHsv(double[] hsv1, double[] hsv2) {
|
||||
@ -184,11 +127,7 @@ public class RgbHsvConversion {
|
||||
}
|
||||
|
||||
private static int[] getRgbBySection(
|
||||
double hueSection,
|
||||
double chroma,
|
||||
double matchValue,
|
||||
double secondLargestComponent
|
||||
) {
|
||||
double hueSection, double chroma, double matchValue, double secondLargestComponent) {
|
||||
int red;
|
||||
int green;
|
||||
int blue;
|
||||
@ -219,7 +158,7 @@ public class RgbHsvConversion {
|
||||
blue = convertToInt(secondLargestComponent + matchValue);
|
||||
}
|
||||
|
||||
return new int[] { red, green, blue };
|
||||
return new int[] {red, green, blue};
|
||||
}
|
||||
|
||||
private static int convertToInt(double input) {
|
||||
|
@ -58,11 +58,8 @@ public class TurkishToLatinConversion {
|
||||
'G',
|
||||
};
|
||||
for (int i = 0; i < turkishChars.length; i++) {
|
||||
param =
|
||||
param.replaceAll(
|
||||
new String(new char[] { turkishChars[i] }),
|
||||
new String(new char[] { latinChars[i] })
|
||||
);
|
||||
param = param.replaceAll(
|
||||
new String(new char[] {turkishChars[i]}), new String(new char[] {latinChars[i]}));
|
||||
}
|
||||
return param;
|
||||
}
|
||||
|
Reference in New Issue
Block a user