-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDecimalFractionToBinary.java
More file actions
178 lines (130 loc) · 5.1 KB
/
DecimalFractionToBinary.java
File metadata and controls
178 lines (130 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import java.util.Random;
public class DecimalFractionToBinary
{
/********DRIVER CODE*******/
public static void main(String[] args)
{
displayTable();
}
//this group work for COA
/*FUNCTION TO TRANSFORM FRACTION PARTS TO BINARY. Using Queues to store the order of most significant bits*/
public static Queue<Integer> convertFractionPart(double fractionNumber)
{
Queue<Integer> fractionBitQueue = new LinkedList<>();
int remainder =0;
if(fractionNumber ==0)
{
fractionBitQueue.add(0);
}
while(remainder != fractionNumber && fractionNumber!= 0)
{
fractionNumber *= 2;
remainder =(int) fractionNumber;
fractionBitQueue.add(remainder);
fractionNumber -= remainder;
}
return fractionBitQueue;
}
/*FUNCTION TO FILL OUT THE REMARKS COLUMN BASED ON THE NUMBER OF BITS ON FRACTION PART */
public static String returnRemarks(Queue<Integer> fractionBitQueue)
{
int sizeOfQueue = fractionBitQueue.size();
String remark = (sizeOfQueue > 5) ? "Approximate": "Exactly";
return remark;
}
/*FUNCTION TO GENERATE STREAM OF BITS FROM FRACTION PART AS A STRING FROM THE QUEUE*/
public static String printFractionBits(Queue<Integer> fractionBitQueue)
{
Iterator<Integer> iterator = fractionBitQueue.iterator();
String fractionBitStream="";
int count =1;
while(iterator.hasNext())
{
int element = iterator.next();
fractionBitStream += element;
if(count >=5 && returnRemarks(fractionBitQueue) =="Approximate")
{
break;
}
count ++;
}
return fractionBitStream;
}
/*FUNCTION TO PRINT ANY LIST PASSED AN ARGUMENT*/
public static String printList(List<Integer> listPassed)
{
String returnedList = "";
for(int i: listPassed)
{
returnedList+=i;
}
return returnedList;
}
/*FUNCTION TRANSFORMING INTEGER PART TO BINARY EQUIVALENT USING STACKS*/
public static List<Integer> convertToBinary(int decimalNumber)
{
int remainder =0;
Stack <Integer>stack = new Stack<Integer>();
List<Integer> listOfRemainders = new ArrayList<Integer>();
if(decimalNumber == 0) //if the number is zero we return the result
{
stack.push(0);
}
while(decimalNumber > 0) // if number greater than zero then continue dividing by 2 while keeping track of the remainders
{
remainder = decimalNumber % 2;
stack.push(remainder);
decimalNumber/=2;
}
while(!stack.isEmpty())
{
Integer storePoppedElement = stack.pop();
listOfRemainders.add(storePoppedElement); // returns final output list read from the most significant bit(falls at top of the stack)
}
return listOfRemainders;
}
/*FUNCTION TO GENERATE THE TABLE COLUMNS AND PASS IN THE VALUES OF THE FUNCTIONS*/
public static void displayTable()
{
System.out.println("===============================================================================================");
System.out.printf("%9s %24s %26s %24s %n","S/No","Decimal Number","Binary Number","Remarks");
System.out.println("===============================================================================================");
String RADIXPOINT =".";
for(int count =1;count <=30;count++)
{
double numberToConvert = generateRandomNumbers();
/***separate the integer part from the fraction part of the number generated**/
int integerPart = (int)numberToConvert;
double fractionPart = numberToConvert - integerPart;
/*convert both the integer and fraction parts to binary equivalents*/
List<Integer> integerPartBitList = convertToBinary(integerPart);
Queue<Integer> fractionPartBitQueue = convertFractionPart(fractionPart);
/*make the output in form of a string*/
String integerBitStream = printList(integerPartBitList);
String fractionBitStream = printFractionBits(fractionPartBitQueue);
String finalBinaryOutput = integerBitStream + RADIXPOINT + fractionBitStream;
String verdict = returnRemarks(fractionPartBitQueue);
System.out.println("_______________________________________________________________________________________________");
System.out.printf("%9s %24s %26s %24s %n",count,numberToConvert,finalBinaryOutput,verdict);
}
}
/*GENERATES RANDOM FLOATING POINT NUMBERS{FROM 0-30} WHILE SETTING THE PRECISION TO AT MOST 3 */
public static double generateRandomNumbers()
{
Random random = new Random();
double floatingPointRandom = (random.nextFloat());
int integerRandom = random.ints(1, 5, 30).findFirst().getAsInt();
double generatedRandomNumber = integerRandom + floatingPointRandom;
@SuppressWarnings("deprecation")
Double truncatedValue= new Double(generatedRandomNumber);
return BigDecimal.valueOf(truncatedValue).setScale(3, RoundingMode.HALF_UP).doubleValue();
}
}