int to binary java
String binary = Integer.toBinaryString(num);int n = 1000;
String s = Integer.toBinaryString(n);import java.util.*;
class Bin {
//fields
private Double number;
//constructors
public Bin(){
number = 0.0;
}
public Bin(Double num){
number = num;
}
//methods
public ArrayList<String> reverseArrayList(ArrayList<String> alist) {
ArrayList<String> array = new ArrayList<String>();
int x = 0;
int i = alist.size() - 1;
while(x < alist.size()){
array.add(alist.get(i));
i -= 1;
x += 1;
}
return array;
}
//getter methods
public ArrayList<String> getBinVal(){
ArrayList<String> binaryVal = new ArrayList<String>();
ArrayList<String> array = new ArrayList<String>();
Double num = number;
Double sol1, check;
while( num > 0 ) {
sol1 = num/2;
check = sol1 - Math.floor(sol1);
if(check > 0) {
binaryVal.add("1");
num = sol1 - 0.5;
}
else {
binaryVal.add("0");
num = sol1;
}
}
while(binaryVal.size() < 4) {
binaryVal.add("0");
}
array = reverseArrayList(binaryVal);
return array;
}
}
class MainClass {
public static void main(String[] args) {
//add the number in as a command line argument
Double num = Double.valueOf(args[0]);
//create an instance of Bin
Bin value = new Bin(num);
//print the binary value
System.out.println(value.getBinVal());
}
}
Also in Java:
- java obtain list string from list object
- java run project from command line
- how to remove duplicates from an array java
- why python is slower than java
- set color of text for jlabel
- working with buttons in applet java
- java hello world
- spigot run command as console
- java declare an int inside an if statement
- foreach java
- java how to print a newline
- java substring
- declareing a variable in java
- remove first character from string
- java remove non numbers from string
- java valeur absolue
- usaco 2018 january contest
- how do you make a method that returns an array java
- android java how to stop users fromgoing back too much
- treeset java descending order using comparator
- java stream find specific element
- check java version cmd
- how to change actionbar color in android programmatically
- javadoc reference general toString