-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changed integers.java constants location and implemented constants in file #237
Open
SameerM123
wants to merge
1
commit into
phishman3579:master
Choose a base branch
from
SameerM123:new_branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rest of code is formatting issues that bothered me when trying to implement the constants. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As well as implementing constants obviously |
||
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,85 +70,70 @@ 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); | ||
} | ||
|
||
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<Integer,String> singleDigits = new HashMap<Integer,String>(); | ||
private static final Map<Integer, String> singleDigits = new HashMap<Integer, String>(); | ||
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<Integer,String> multiDigits = new HashMap<Integer,String>(); | ||
private static final Map<Integer, String> multiDigits = new HashMap<Integer, String>(); | ||
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(); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constants start from here now