Skip to content
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

Implemented Refactoring techniques to improve the code quality #175

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
128 changes: 4 additions & 124 deletions src/com/jwetherell/algorithms/numbers/Integers.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.HashMap;
import java.util.Map;

public class Integers {
public class Integers extends NumericalConversions{

private static final BigDecimal ZERO = new BigDecimal(0);
private static final BigDecimal TWO = new BigDecimal(2);
Expand All @@ -23,16 +23,8 @@ 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);
StringBuilder builder = new StringBuilder();
int temp = 0;
while (integer > 0) {
temp = integer;
integer = (temp >> 1);
builder.append(temp % 2);
}
return builder.reverse().toString();
return toBinaryUsingShiftsAndModulusNC(numberToConvert);

}

public static final String toBinaryUsingBigDecimal(int numberToConvert) {
Expand Down Expand Up @@ -99,117 +91,5 @@ public static final boolean powerOfTwoUsingBits(int numberToCheck) {
return false;
}

// Integer to English
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");
}

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");
}

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;
int m = x / HUNDRED;
int r = x % HUNDRED;
if (m > 0) {
builder.append(singleDigits.get(m)).append("-hundred");
x = x % HUNDRED;
}
if (r > 0) {
if (m > 0) builder.append(" ");
if (x <= 19) {
builder.append(singleDigits.get(x));
} else {
m = x / TEN;
r = x % TEN;
if (r == 0) {
builder.append(multiDigits.get(x));
} else {
x = x - r;
builder.append(multiDigits.get(x)).append("-");
builder.append(singleDigits.get(r));
}
}
}
return builder.toString();
}

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);
StringBuilder builder = new StringBuilder();
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) {
builder.append("negative ");
// Make the number positive
x = x * -1;
}
int m = x / BILLION;
if (m > 0) {
billion = true;
builder.append(handleUnderOneThousand(m)).append("-billion");
x = x % BILLION;
}
m = x / MILLION;
if (m > 0) {
if (billion) builder.append(" ");
million = true;
builder.append(handleUnderOneThousand(m)).append("-million");
x = x % MILLION;
}
m = x / THOUSAND;
if (m > 0) {
if (billion || million) builder.append(" ");
thousand = true;
builder.append(handleUnderOneThousand(m)).append("-thousand");
x = x % THOUSAND;
}
if (billion || million || thousand && x!=0) builder.append(" ");
builder.append(handleUnderOneThousand(x));
return builder.toString();
}
// Intergers to English - refactored into another class; Refactoring Technique used: Extract Class
}
129 changes: 129 additions & 0 deletions src/com/jwetherell/algorithms/numbers/IntegersToEnglish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.jwetherell.algorithms.numbers;

import java.util.HashMap;
import java.util.Map;

// Refactoring Technique used: Extract Class
public class IntegersToEnglish {
// Integer to English
// Refactoring Technique: Move Field.
// 1. Moved the below fields from Integer class to InterToEnglish class
private static final int HUNDRED = 100;
private static final int TEN = 10;

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");
}

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");
}

// Refactoring Technique: Move Method; Previous: Integer class ; After: IntegerToEnglish class
private static final String handleUnderOneThousand(int number) {
StringBuilder builder = new StringBuilder();
int x = number;
int m = x / HUNDRED;
int r = x % HUNDRED;
if (m > 0) {
builder.append(singleDigits.get(m)).append("-hundred");
x = x % HUNDRED;
}
if (r > 0) {
if (m > 0) builder.append(" ");
if (x <= 19) {
builder.append(singleDigits.get(x));
} else {
m = x / TEN;
r = x % TEN;
if (r == 0) {
builder.append(multiDigits.get(x));
} else {
x = x - r;
builder.append(multiDigits.get(x)).append("-");
builder.append(singleDigits.get(r));
}
}
}
return builder.toString();
}

// Refactoring Technique: Move Method; Previous: Integer class ; After: IntegerToEnglish class
public static final String toEnglish(int number) {
// Refactoring Technique: Move Field.F
// 1. Moved the below fields from Integer class to InterToEnglish class
// 2. Within IntegerToEnglish class, moving the field to local method where it is required.
final int BILLION = 1000000000;
final int MILLION = 1000000;
final int THOUSAND = 1000;

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);
StringBuilder builder = new StringBuilder();
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) {
builder.append("negative ");
// Make the number positive
x = x * -1;
}
int m = x / BILLION;
if (m > 0) {
billion = true;
builder.append(handleUnderOneThousand(m)).append("-billion");
x = x % BILLION;
}
m = x / MILLION;
if (m > 0) {
if (billion) builder.append(" ");
million = true;
builder.append(handleUnderOneThousand(m)).append("-million");
x = x % MILLION;
}
m = x / THOUSAND;
if (m > 0) {
if (billion || million) builder.append(" ");
thousand = true;
builder.append(handleUnderOneThousand(m)).append("-thousand");
x = x % THOUSAND;
}
if (billion || million || thousand && x!=0) builder.append(" ");
builder.append(handleUnderOneThousand(x));
return builder.toString();
}
}
13 changes: 2 additions & 11 deletions src/com/jwetherell/algorithms/numbers/Longs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.math.BigDecimal;

public class Longs {
public class Longs extends NumericalConversions{

public static final String toBinaryUsingDivideAndModulus(long numberToConvert) {
long longNumber = numberToConvert;
Expand All @@ -18,16 +18,7 @@ public static final String toBinaryUsingDivideAndModulus(long numberToConvert) {
}

public static final String toBinaryUsingShiftsAndModulus(long numberToConvert) {
long longNumber = numberToConvert;
if (longNumber<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+longNumber);
StringBuilder builder = new StringBuilder();
long temp = 0l;
while (longNumber > 0) {
temp = longNumber;
longNumber = (temp >> 1);
builder.append(temp % 2);
}
return builder.reverse().toString();
return toBinaryUsingShiftsAndModulusNC(numberToConvert);
}

public static final String toBinaryUsingBigDecimal(long numberToConvert) {
Expand Down
23 changes: 23 additions & 0 deletions src/com/jwetherell/algorithms/numbers/NumericalConversions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.jwetherell.algorithms.numbers;

public class NumericalConversions {
public static final String toBinaryUsingShiftsAndModulusNC(Object obj) {
long number = 0;
long temp = 0;
if (obj instanceof Integer) {
number = (int) obj;
temp = 0;
} else if (obj instanceof Long) {
number = (long) obj;
temp = 0l;
}
if (number < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + number);
StringBuilder builder = new StringBuilder();
while (number > 0) {
temp = number;
number = (temp >> 1);
builder.append(temp % 2);
}
return builder.reverse().toString();
}
}