-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryNumber.java
More file actions
142 lines (124 loc) · 3.53 KB
/
BinaryNumber.java
File metadata and controls
142 lines (124 loc) · 3.53 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
//Susmitha Shailesh
//I pledge my honor that I have abided by the Stevens Honor System.
package hw1;
public class BinaryNumber {
//data fields
private int[] data; //stores digits of BinaryNumber
private boolean overflow = false; //true until cleared if overflow error was raised
//constructors
BinaryNumber(int length){
//parameters: int length
for(int i=0; i<length; i++) {
data[i] = 0;
}
}
BinaryNumber(String str){
//parameters: String str
data = new int[str.length()];
for(int i=0; i<str.length(); i++) {
data[i]=java.lang.Character.getNumericValue(str.charAt(i));
}
}
//methods
public int getLength() {
//returns length of binary number
return data.length;
}
public int getDigit(int index) {
//returns the digit at a certain index of binary number
//does not work if index is negative or greater than length of binary number
if(index<0 | index>=data.length) {
System.out.println("index out of bounds");
return -1;
}
return data[index];
}
public int toDecimal() {
//returns conversion of binary number to decimal number
int num = 0;
for(int i=0; i<data.length; i++) {
num += (data[i]*(Math.pow(2, i)));
}
return num;
}
public void shiftR(int amount) {
//shifts all digits of binary number amount places to the right
//replaces all newly empty spaces with 0
//does not work if amount is less than 1
if(amount<0) {
System.out.println("invalid shift amount");
}
else {
int[] newArr = new int[data.length+amount];
for(int i=0; i<amount; i++) {
//add zeros
newArr[i] = 0;
}
for(int i=amount; i<newArr.length; i++) {
//copy array contents
newArr[i] = data[i-amount];
}
data = newArr;
}
}
public void add(BinaryNumber aBinaryNumber) {
//adds two BinaryNumbers and replaces receiving number with the sum
//does not work if the two BinaryNumbers have different lengths
//does not work if the sum is longer than the summands; causes overflow
int cin = 0;
int[] ans = new int[data.length];
if(this.getLength() != aBinaryNumber.getLength()) {
//check that BinaryNumbers have same length
System.out.println("Lengths of binary numbers do now coincide");
}
else {
//adds numbers to ans[i] based on addition of the digits of the BinaryNumbers
//at index i
for(int i=0; i<this.getLength(); i++) {
if((this.getDigit(i) + aBinaryNumber.getDigit(i) + cin) == 0 ) {
ans[i] = 0;
cin=0;
}
else if((this.getDigit(i) + aBinaryNumber.getDigit(i) + cin) == 1 ) {
ans[i] = 1;
cin=0;
}
else if((this.getDigit(i) + aBinaryNumber.getDigit(i) + cin) == 2 ) {
if(i==(this.getLength()-1)) {
//catches overflow if there is a carryover digit at the addition
//of the last digits of the BinaryNumbers
overflow = true;
break;
}
ans[i] = 0;
cin=1;
}
else if((this.getDigit(i) + aBinaryNumber.getDigit(i) + cin) == 3 ) {
if(i==(this.getLength()-1)) {
overflow = true;
break;
}
ans[i] = 1;
cin=1;
}
}
}
data = ans;
}
public void clearOverflow() {
//resets overflow boolean to false
overflow = false;
}
public String toString() {
//toString prints BinaryNumber digits
//returns overflow if error was previously raised
if(overflow) {
return "Overflow";
}
String dataStr = "";
for(int i=0; i<data.length; i++) {
dataStr += data[i];
}
return dataStr;
}
}