Skip to content

Commit

Permalink
Unit test all the things! Fix all the bugs!
Browse files Browse the repository at this point in the history
-Cleaned up some of my mistakes
-Fixed Java Doc comments
-Changed access modifiers from public to private where appropriate
-Renamed some stuff
-Rewrote some methods
-Made the GUI auto-expand to fit device screen (scales in both height
and width)
-Added Unit Tests (wrote new tests for important BaseConversion methods
and adapted the old unit tests for this version of Pr0Ca1... didn't get
around to Pr0Number testing though)
-Generated new signed apk and tested on both my Nexus 7 tablet and my
ZTE Maven smartphone
  • Loading branch information
DrewHans committed Jan 3, 2018
1 parent 6ddd52a commit f95f860
Show file tree
Hide file tree
Showing 11 changed files with 1,629 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ public String mul(String dec1, String dec2) throws NumberFormatException {
* @param dec1 - dividend
* @param dec2 - divisor
* @return quotient
* @throws NumberFormatException on Long.parseLong error
* @throws NumberFormatException on Long.parseLong error
* @throws IllegalArgumentException on divisor == 0
*/
public String div(String dec1, String dec2) throws NumberFormatException {
if (Long.parseLong(dec2) == 0) {
throw new IllegalArgumentException("Argument 'dec2 - divisor' is 0");
}

long value = Long.parseLong(dec1) / Long.parseLong(dec2);
return "" + (value);
}//end div method
Expand All @@ -77,9 +82,14 @@ public String div(String dec1, String dec2) throws NumberFormatException {
* @param dec1 - modulo dividend
* @param dec2 - modulo divisor
* @return remainder
* @throws NumberFormatException on Long.parseLong error
* @throws NumberFormatException on Long.parseLong error
* @throws IllegalArgumentException on modulo divisor == 0
*/
public String mod(String dec1, String dec2) throws NumberFormatException {
if (Long.parseLong(dec2) == 0) {
throw new IllegalArgumentException("Argument 'dec2 - modulo divisor' is 0");
}

long value = Long.parseLong(dec1) % Long.parseLong(dec2);
return "" + (value);
}//end mod method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ public BaseConverter() {
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base8 representation of binString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when binString input exceeds bitPrecision
*/
public String convertBase2toBase8(String binString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String buffer = "";

// check that binString does not exceed our bitPrecision
if (this.binStringIsWithinBounds(binString, bitPrecision)) {
// check that binString is not empty and does not exceed our bitPrecision
if (binString.isEmpty()) {
buffer = "";
} else if (this.binStringIsWithinBounds(binString, bitPrecision)) {
// initialize variables for the for-loop
String temp = "";
char[] bits = binString.toCharArray();
Expand Down Expand Up @@ -92,14 +94,16 @@ public String convertBase2toBase8(String binString, int bitPrecision, boolean si
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base10 representation of binString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when binString input exceeds bitPrecision
*/
public String convertBase2toBase10(String binString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String buffer;
long temp;

// check that binString does not exceed our bitPrecision
if (this.binStringIsWithinBounds(binString, bitPrecision)) {
// check that binString is not empty and does not exceed our bitPrecision
if (binString.isEmpty()) {
buffer = "";
} else if (this.binStringIsWithinBounds(binString, bitPrecision)) {
// pad with zeros when necessary (assume user forgot to input zeros for positive signed value)
binString = String.format("%" + bitPrecision + "s", binString).replace(' ', '0');

Expand Down Expand Up @@ -129,13 +133,15 @@ public String convertBase2toBase10(String binString, int bitPrecision, boolean s
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base16 representation of binString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when binString input exceeds bitPrecision
*/
public String convertBase2toBase16(String binString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String buffer = "";

// check that binString does not exceed our bitPrecision
if (this.binStringIsWithinBounds(binString, bitPrecision)) {
// check that binString is not empty and does not exceed our bitPrecision
if (binString.isEmpty()) {
buffer = "";
} else if (this.binStringIsWithinBounds(binString, bitPrecision)) {
// initialize variables for the for-loop
String temp = "";
char[] bits = binString.toCharArray();
Expand Down Expand Up @@ -230,17 +236,19 @@ public String convertBase2toBase16(String binString, int bitPrecision, boolean s
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base2 representation of octString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when octString input exceeds bitPrecision
*/
public String convertBase8toBase2(String octString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String buffer = "";

// check that octString does not exceed our base2 bitPrecision
if (this.octStringIsWithinBounds(octString, bitPrecision)) {
// check that octString is not empty and does not exceed our base2 bitPrecision
if (octString.isEmpty()) {
buffer = "";
} else if (this.octStringIsWithinBounds(octString, bitPrecision)) {
// initialize variable for the for-loop
char[] bits = octString.toCharArray();

// substitute every hexadecimal value with the approriate binary value
// substitute every hexadecimal value with the appropriate binary value
for (int i = 0; i < bits.length; i++) {

if (bits[i] == '7') {
Expand Down Expand Up @@ -292,7 +300,7 @@ public String convertBase8toBase2(String octString, int bitPrecision, boolean si
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base10 representation of octString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when octString input exceeds bitPrecision
*/
public String convertBase8toBase10(String octString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String binString = convertBase8toBase2(octString, bitPrecision, signed); // throws exception on failure
Expand All @@ -306,7 +314,7 @@ public String convertBase8toBase10(String octString, int bitPrecision, boolean s
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base16 representation of octString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when octString input exceeds bitPrecision
*/
public String convertBase8toBase16(String octString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String binString = convertBase8toBase2(octString, bitPrecision, signed); // throws exception on failure
Expand All @@ -323,12 +331,15 @@ public String convertBase8toBase16(String octString, int bitPrecision, boolean s
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base2 representation of decInteger or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when decString input exceeds bitPrecision or Long.parseLong throws NumberFormatException
*/
public String convertBase10toBase2(String decString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String buffer;

if (decStringIsWithinBounds(decString, bitPrecision, signed)) {
// check that decString is not empty and does not exceed our base2 bitPrecision
if (decString.isEmpty()) {
buffer = "";
} else if (decStringIsWithinBounds(decString, bitPrecision, signed)) {
long decValue;
try {
decValue = Long.parseLong(decString);
Expand Down Expand Up @@ -361,7 +372,7 @@ public String convertBase10toBase2(String decString, int bitPrecision, boolean s
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base8 representation of decInteger or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when decString input exceeds bitPrecision or Long.parseLong throws NumberFormatException
*/
public String convertBase10toBase8(String decInteger, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String binString = convertBase10toBase2(decInteger, bitPrecision, signed); // throws exception on failure
Expand All @@ -375,7 +386,7 @@ public String convertBase10toBase8(String decInteger, int bitPrecision, boolean
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base16 representation of decInteger or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when decString input exceeds bitPrecision or Long.parseLong throws NumberFormatException
*/
public String convertBase10toBase16(String decInteger, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String binString = convertBase10toBase2(decInteger, bitPrecision, signed); // throws exception on failure
Expand All @@ -392,17 +403,19 @@ public String convertBase10toBase16(String decInteger, int bitPrecision, boolean
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base2 representation of hexString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when hexString input exceeds bitPrecision
*/
public String convertBase16toBase2(String hexString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String buffer = "";

// check that hexString does not exceed our base2 bitPrecision
if (this.hexStringIsWithinBounds(hexString, bitPrecision)) {
// check that hexString is not empty and does not exceed our base2 bitPrecision
if (hexString.isEmpty()) {
buffer = "";
} else if (this.hexStringIsWithinBounds(hexString, bitPrecision)) {
// initialize variable for the for-loop
char[] bits = hexString.toCharArray();

// substitute every hexadecimal value with the approriate binary value
// substitute every hexadecimal value with the appropriate binary value
for (int i = 0; i < bits.length; i++) {

if (bits[i] == 'F') {
Expand Down Expand Up @@ -486,7 +499,7 @@ public String convertBase16toBase2(String hexString, int bitPrecision, boolean s
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base8 representation of hexString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when hexString input exceeds bitPrecision
*/
public String convertBase16toBase8(String hexString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String binString = convertBase16toBase2(hexString, bitPrecision, signed); // throws exception on failure
Expand All @@ -500,7 +513,7 @@ public String convertBase16toBase8(String hexString, int bitPrecision, boolean s
* @param bitPrecision - any bit precision in range 5 - 32 will work for both signed/unsigned
* @param signed - a boolean that indicates two's compliment conversion
* @return a string base8 representation of hexString or an error message
* @throws NumberOutOfModeBoundsException
* @throws NumberOutOfModeBoundsException when hexString input exceeds bitPrecision
*/
public String convertBase16toBase10(String hexString, int bitPrecision, boolean signed) throws NumberOutOfModeBoundsException {
String binString = convertBase16toBase2(hexString, bitPrecision, signed); // throws exception on failure
Expand All @@ -517,7 +530,7 @@ public String convertBase16toBase10(String hexString, int bitPrecision, boolean
* @param bitPrecision - the number of binary bits we have to represent binString
* @return true if binString is within our bitPrecision bounds
*/
public boolean binStringIsWithinBounds(String binString, int bitPrecision) {
private boolean binStringIsWithinBounds(String binString, int bitPrecision) {
return binString.length() <= bitPrecision;
}//end binStringIsWithinBounds method

Expand Down Expand Up @@ -645,4 +658,3 @@ private long unsignedBinaryStringToPositiveDecimalInt(String binString) {
}//end unsignedBinaryStringToPositiveDecimalInt method

}//end BaseConverter class

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public BooleanLogicOperator() {
*
* @param bitPrecision - the number of bits available for representing not-binString
* @param binString - assume a String of 1s and 0s
* @return flipped bit version of binNum
* @return bitwise NOT of binString (after padding with 0s) as a String
*/
public String not(int bitPrecision, String binString) {
binString = String.format("%" + bitPrecision + "s", binString).replace(' ', '0'); //pad with zeros when necessary
Expand Down
Loading

0 comments on commit f95f860

Please sign in to comment.