-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary.java
144 lines (132 loc) · 3.55 KB
/
Binary.java
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
/*
Jason Chua
APCS1 pd9
HW45 -- Come Together
2015-12-09
*/
public class Binary implements Comparable {
private int _decNum;
private String _binNum;
public Binary() {
_decNum = 0;
_binNum = "0";
}
public Binary( int n ) {
_decNum = n;
_binNum = decToBin(n);
}
public Binary( String s ) {
_decNum = binToDec(s);
_binNum = s;
}
public String toString() {
return _binNum;
}
//while the quotient is not 0, divides quotient by 2 and adds the remainder
//to the front of String variable remainder
public static String decToBin( int n ) {
String remainder = "";
while (n != 0) {
remainder = n%2 + remainder;
n /= 2;
}
return remainder;
}
public static String decToBinR( int n ) {
String remainder = "";
if (n != 0) {
remainder += n%2;
remainder = decToBinR(n/2) + remainder;
}
return remainder;
}
public static int binToDec( String s ) {
int dec = 0;
for (int i = 0; i < s.length(); i++) {
dec += Integer.parseInt(s.substring(i,i+1))
* Math.pow(2,s.length()-i-1);
}
return dec;
}
public static int binToDecR( String s ) {
int dec = 0;
if (s.length() != 0) {
dec += binToDecR(s.substring(1))
+ Integer.parseInt(s.substring(0,1)) * Math.pow(2,s.length()-1);
}
return dec;
}
public boolean equals( Object other ) {
boolean ret = this == other;
if (!ret) {
ret = other instanceof Binary
&& (this.compareTo((Binary)other) == 0);
}
return ret;
}
public int compareTo( Object o ) {
int ret = 0;
//If o is not an instance of Comparable, a ClassCastException is thrown.
if (!(o instanceof Comparable)) {
throw new ClassCastException("\ncompareTo() input not a Comparable");
}
//If o is null, a NullPointerException is thrown.
if (o == null) {
throw new NullPointerException("\ncompareTo() input is null");
}
//Otherwise, tests are run for which instance of Comparable o is.
else {
//If o is an instance of Binary, o is typecasted to a
//Binary, and the decimal numbers of each Binary
//are compared.
if (o instanceof Binary) {
Binary x = (Binary)o;
if (_decNum == x._decNum) {
ret = 0;
}
else if (x._decNum > _decNum) {
ret = -1;
}
else {
ret = 1;
}
}
//If o is an instance of Hexadecimal, o is typecasted to Hexadecimal
//and using the hexToDec method from Hexadecimal, the Hexadecimal
//number of o is changed to a decimal and stored in int a.
//The decimal number of this Hexadecimal is compared to int a.
else if (o instanceof Hexadecimal) {
int a = Hexadecimal.hexToDec(((Hexadecimal)o).toString());
if (_decNum == a) {
ret = 0;
}
else if (a > _decNum) {
ret = -1;
}
else {
ret = 1;
}
}
//If o is an instance of Rational, o is typecasted to Rational and
//a new Rational is created with the decimal number of this
//Binary as the numerator and 1 as the denominator.
//The numerator of the first Rational is multiplied by the
//denominator of the second and the numerator of the second
//is multiplied by the denominator of the first.
//These new numerators are then compared.
else if (o instanceof Rational) {
int thisNumerator, otherNumerator, num1, denom1, num2, denom2;
Rational x = new Rational(_decNum,1);
Rational y = (Rational)o;
num1 = x.getNum();
denom1 = x.getDenom();
num2 = y.getNum();
denom2 = y.getDenom();
thisNumerator = num1 * denom2;
otherNumerator = denom1 * num2;
ret = thisNumerator - otherNumerator;
}
}
return ret;
}
}