Last active
March 27, 2018 11:54
-
-
Save ziginsider/3e6e1f21ae89ceda46e791f6491f8284 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package someproject; | |
/** | |
* | |
* @author Alex Kisel | |
*/ | |
public class BinaryConverter { | |
public static void main(String[] args) { | |
for(int i = -5; i < 33; i++) { | |
System.out.println(i + ": " + toBinary(i)); | |
System.out.println(i); | |
//always another way | |
System.out.println(i + ": " + Integer.toBinaryString(i)); | |
} | |
} | |
/* | |
* pre: none | |
* post: returns a String with base10Num in base 2 | |
*/ | |
public static String toBinary(int base10Num) { | |
boolean isNeg = base10Num < 0; | |
base10Num = Math.abs(base10Num); | |
String result = ""; | |
while(base10Num > 1) { | |
result = (base10Num % 2) + result; | |
base10Num /= 2; | |
} | |
assert base10Num == 0 || base10Num == 1 : "value is not <= 1: " + base10Num; | |
result = base10Num + result; | |
assert all0sAnd1s(result); | |
if(isNeg) | |
result = "-" + result; | |
return result; | |
} | |
/* | |
* pre: cal != null | |
* post: return true if val consists only of characters 1 and 0, false otherwise | |
*/ | |
public static boolean all0sAnd1s(String val) { | |
assert val != null : "Failed precondition all0sAnd1s. parameter cannot be null"; | |
boolean all = true; | |
int i = 0; | |
char c; | |
while(all && i < val.length()) { | |
c = val.charAt(i); | |
all = c == '0' || c == '1'; | |
i++; | |
} | |
return all; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment