1public static String makeBinaryString(int n) {
2 StringBuilder sb = new StringBuilder();
3 while (n > 0) {
4 sb.append(n % 2);
5 n /= 2;
6 }
7 sb.reverse();
8 return sb.toString();
9}
1import java.util.*;
2
3class Bin {
4
5 //fields
6 private Double number;
7
8 //constructors
9 public Bin(){
10 number = 0.0;
11 }
12
13 public Bin(Double num){
14 number = num;
15 }
16
17 //methods
18 public ArrayList<String> reverseArrayList(ArrayList<String> alist) {
19
20 ArrayList<String> array = new ArrayList<String>();
21
22 int x = 0;
23 int i = alist.size() - 1;
24
25 while(x < alist.size()){
26 array.add(alist.get(i));
27 i -= 1;
28 x += 1;
29 }
30
31 return array;
32 }
33
34 //getter methods
35 public ArrayList<String> getBinVal(){
36 ArrayList<String> binaryVal = new ArrayList<String>();
37 ArrayList<String> array = new ArrayList<String>();
38 Double num = number;
39 Double sol1, check;
40
41 while( num > 0 ) {
42
43 sol1 = num/2;
44 check = sol1 - Math.floor(sol1);
45
46 if(check > 0) {
47 binaryVal.add("1");
48 num = sol1 - 0.5;
49 }
50
51 else {
52 binaryVal.add("0");
53 num = sol1;
54 }
55 }
56
57 while(binaryVal.size() < 4) {
58 binaryVal.add("0");
59 }
60
61 array = reverseArrayList(binaryVal);
62 return array;
63 }
64
65}
66
67class MainClass {
68
69 public static void main(String[] args) {
70
71 //add the number in as a command line argument
72 Double num = Double.valueOf(args[0]);
73
74 //create an instance of Bin
75 Bin value = new Bin(num);
76
77 //print the binary value
78 System.out.println(value.getBinVal());
79 }
80}