From 0982e3bd6fdba6aa8ddeea6f609fa5a8342d20fe Mon Sep 17 00:00:00 2001 From: Sameer Mahmood <64174313+SameerM123@users.noreply.github.com> Date: Wed, 30 Oct 2024 19:34:28 -0400 Subject: [PATCH] Changed integers.java constants location and implemented constants into file. --- .../algorithms/numbers/Integers.java | 116 ++++++++---------- 1 file changed, 53 insertions(+), 63 deletions(-) diff --git a/src/com/jwetherell/algorithms/numbers/Integers.java b/src/com/jwetherell/algorithms/numbers/Integers.java index fb86fd3d..3ba0b8ba 100644 --- a/src/com/jwetherell/algorithms/numbers/Integers.java +++ b/src/com/jwetherell/algorithms/numbers/Integers.java @@ -9,9 +9,15 @@ public class Integers { private static final BigDecimal ZERO = new BigDecimal(0); private static final BigDecimal TWO = new BigDecimal(2); + private static final int BILLION = 1000000000; + private static final int MILLION = 1000000; + private static final int THOUSAND = 1000; + private static final int HUNDRED = 100; + private static final int TEN = 10; + public static final String toBinaryUsingDivideAndModulus(int numberToConvert) { int integer = numberToConvert; - if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer); + if (integer < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + integer); StringBuilder builder = new StringBuilder(); int temp = 0; while (integer > 0) { @@ -24,7 +30,7 @@ public static final String toBinaryUsingDivideAndModulus(int numberToConvert) { public static final String toBinaryUsingShiftsAndModulus(int numberToConvert) { int integer = numberToConvert; - if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer); + if (integer < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + integer); StringBuilder builder = new StringBuilder(); int temp = 0; while (integer > 0) { @@ -37,7 +43,7 @@ public static final String toBinaryUsingShiftsAndModulus(int numberToConvert) { public static final String toBinaryUsingBigDecimal(int numberToConvert) { int integer = numberToConvert; - if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer); + if (integer < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + integer); StringBuilder builder = new StringBuilder(); BigDecimal number = new BigDecimal(integer); BigDecimal[] decimals = null; @@ -51,11 +57,11 @@ public static final String toBinaryUsingBigDecimal(int numberToConvert) { public static final String toBinaryUsingDivideAndDouble(int numberToConvert) { int integer = numberToConvert; - if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer); + if (integer < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + integer); StringBuilder builder = new StringBuilder(); double temp = 0d; while (integer > 0) { - temp = integer/2d; + temp = integer / 2d; integer = (int) temp; builder.append((temp > integer) ? 1 : 0); } @@ -64,22 +70,17 @@ public static final String toBinaryUsingDivideAndDouble(int numberToConvert) { public static final boolean powerOfTwoUsingLoop(int numberToCheck) { int number = numberToCheck; - if (number == 0) - return false; + if (number == 0) return false; while (number % 2 == 0) { number /= 2; } - if (number > 1) - return false; - return true; + return number == 1; } public static final boolean powerOfTwoUsingRecursion(int numberToCheck) { int number = numberToCheck; - if (number == 1) - return true; - if (number == 0 || number % 2 != 0) - return false; + if (number == 1) return true; + if (number == 0 || number % 2 != 0) return false; return powerOfTwoUsingRecursion(number / 2); } @@ -87,62 +88,52 @@ public static final boolean powerOfTwoUsingLog(int numberToCheck) { int number = numberToCheck; double doubleLog = Math.log10(number) / Math.log10(2); int intLog = (int) doubleLog; - if (Double.compare(doubleLog, intLog) == 0) - return true; - return false; + return Double.compare(doubleLog, intLog) == 0; } public static final boolean powerOfTwoUsingBits(int numberToCheck) { int number = numberToCheck; - if (number != 0 && ((number & (number - 1)) == 0)) - return true; - return false; + return number != 0 && ((number & (number - 1)) == 0); } // Integer to English - private static final Map singleDigits = new HashMap(); + private static final Map singleDigits = new HashMap(); static { - singleDigits.put(0,"zero"); - singleDigits.put(1,"one"); - singleDigits.put(2,"two"); - singleDigits.put(3,"three"); - singleDigits.put(4,"four"); - singleDigits.put(5,"five"); - singleDigits.put(6,"six"); - singleDigits.put(7,"seven"); - singleDigits.put(8,"eight"); - singleDigits.put(9,"nine"); - singleDigits.put(10,"ten"); - singleDigits.put(11,"eleven"); - singleDigits.put(12,"twelve"); - singleDigits.put(13,"thirteen"); - singleDigits.put(14,"fourteen"); - singleDigits.put(15,"fifteen"); - singleDigits.put(16,"sixteen"); - singleDigits.put(17,"seventeen"); - singleDigits.put(18,"eighteen"); - singleDigits.put(19,"nineteen"); + singleDigits.put(0, "zero"); + singleDigits.put(1, "one"); + singleDigits.put(2, "two"); + singleDigits.put(3, "three"); + singleDigits.put(4, "four"); + singleDigits.put(5, "five"); + singleDigits.put(6, "six"); + singleDigits.put(7, "seven"); + singleDigits.put(8, "eight"); + singleDigits.put(9, "nine"); + singleDigits.put(10, "ten"); + singleDigits.put(11, "eleven"); + singleDigits.put(12, "twelve"); + singleDigits.put(13, "thirteen"); + singleDigits.put(14, "fourteen"); + singleDigits.put(15, "fifteen"); + singleDigits.put(16, "sixteen"); + singleDigits.put(17, "seventeen"); + singleDigits.put(18, "eighteen"); + singleDigits.put(19, "nineteen"); } - private static final Map multiDigits = new HashMap(); + private static final Map multiDigits = new HashMap(); static { - multiDigits.put(10,"ten"); - multiDigits.put(20,"twenty"); - multiDigits.put(30,"thirty"); - multiDigits.put(40,"forty"); - multiDigits.put(50,"fifty"); - multiDigits.put(60,"sixty"); - multiDigits.put(70,"seventy"); - multiDigits.put(80,"eighty"); - multiDigits.put(90,"ninety"); + multiDigits.put(10, "ten"); + multiDigits.put(20, "twenty"); + multiDigits.put(30, "thirty"); + multiDigits.put(40, "forty"); + multiDigits.put(50, "fifty"); + multiDigits.put(60, "sixty"); + multiDigits.put(70, "seventy"); + multiDigits.put(80, "eighty"); + multiDigits.put(90, "ninety"); } - private static final int BILLION = 1000000000; - private static final int MILLION = 1000000; - private static final int THOUSAND = 1000; - private static final int HUNDRED = 100; - private static final int TEN = 10; - private static final String handleUnderOneThousand(int number) { StringBuilder builder = new StringBuilder(); int x = number; @@ -173,19 +164,18 @@ private static final String handleUnderOneThousand(int number) { public static final String toEnglish(int number) { int x = number; - if (x>Integer.MAX_VALUE || x<=Integer.MIN_VALUE) throw new IllegalArgumentException("Number has to be <= Integer.MAX_VALUE and > Integer.MIN_VALUE. number="+x); + if (x > Integer.MAX_VALUE || x <= Integer.MIN_VALUE) throw new IllegalArgumentException("Number has to be <= Integer.MAX_VALUE and > Integer.MIN_VALUE. number=" + x); StringBuilder builder = new StringBuilder(); - if (x==0) { - //Zero is a special case + if (x == 0) { + // Zero is a special case builder.append(singleDigits.get(x)); return builder.toString(); } boolean billion = false; boolean million = false; boolean thousand = false; - if (x<0) { + if (x < 0) { builder.append("negative "); - // Make the number positive x = x * -1; } int m = x / BILLION; @@ -208,7 +198,7 @@ public static final String toEnglish(int number) { builder.append(handleUnderOneThousand(m)).append("-thousand"); x = x % THOUSAND; } - if (billion || million || thousand && x!=0) builder.append(" "); + if ((billion || million || thousand) && x != 0) builder.append(" "); builder.append(handleUnderOneThousand(x)); return builder.toString(); }