2 Assignment Define a class BinaryNumber that represents binary numbers and a fe
ID: 3743147 • Letter: 2
Question
2 Assignment
Define a class BinaryNumber that represents binary numbers and a few simple operations on them, as indicated below. An example of a binary number is
1011
Its length is 4. Note that its leftmost digit is the most significant one: it represents the decimal number 123 +022 +121 +120 = 11. This is called big-endian format. Please be sure to use big-endian format in your program.
This assignment requests that a number of operations be supported. They are divided into two groups. The first is a set of basic operations, the second is slightly more challenging and addresses addition of binary numbers.
2.1 Basic operations
The following operations should be supported:
A constructor BinaryNumber(int length) for creating a binary number of length length and consisting only of zeros.
A constructor BinaryNumber(String str) for creating a binary number given a string. For example, given the string "1011", the corresponding binary number should be created. For this exercise you will have to use some standard String operations. These are listed in the “Hints” section below.
1
An operation int getLength() for determining the length of a binary number.
An operation int[] getInnerArray() that returns the integer array representing the bi-
nary number.
An operation int getDigit(int index) for obtaining a digit of a binary number given an index. The starting index is 0. If the index is out of bounds, then a message should be printed on the screen indicating this fact.
An operation int toDecimal() for transforming a binary number to its decimal notation (cf. the example given above).
An operation void bitShift(int direction, int amount) for shifting all digits in a binary number any number of places to the left or right. The direction parameter indicates a left shift when the value is -1. When direction is given the value 1, the shift should be to the right. Any other value for direction should be seen as invalid. The amount parameter specifies how many digits the BinaryNumber will be shifted, and is only valid when it is nonnegative. For example, ’1011’ shifted right by 2 is ’10’. ’1011’ shifted left by 2 is ’101100’. Notice that shifting right decreases the number by factors of 2, while shifting left increases the number by factors of 2. These operations are equivalent to the ”>>” and ”<<” operators in Java.
An operation static int[] bwor(BinaryNumber bn1, BinaryNumber bn2) that computes the bitwise or of the two numbers. Note that both argument BinaryNumbers must be of the same length for the input to be considered valid. The bitwise or of ’1010’ and ’1100’ is ’1110’.
An operation static int[] bwand(BinaryNumber bn1, BinaryNumber bn2) that computes the bitwise and of the two numbers. Note that both argument BinaryNumbers must be of the same length for the input to be considered valid. The bitwise and of ’1010’ and ’1100’ is ’1000’.
An operation String toString() that returns the BinaryNumber as the corresponding encoded string.
2.2 Addition of Binary Numbers
Here is an example of how two binary numbers of the same length are added1.
1 1 (carried digits)
01101 + 01001
= 1 0 1 1 0 =22
Note that it is possible for the addition of two numbers to yield a result which has a larger length than the summands. In that case, room should be made for the extra digit - meaning the array should be copied over to a new one that is one greater in length.
2
1 1 1 (carried digits) 10110
+ 11101 =110011 =51
The int[] field data should be added to the data fields of BinaryNumber. Implement the following operations:
• void add(BinaryNumber aBinaryNumber) for adding two binary numbers, one is the binary number that receives the message and the other is given as a parameter. If the lengths of the two BinaryNumbers do not coincide, then the smaller one should have 0’s prepended to it in order to prevent errors. Note how ’101’ + ’1’ is the same as ’101’ + ’001’. The BinaryNumber which receives aBinaryNumber should be modified with the result of addition.
CODE MUST IMPLEMENT THE FOLLOWING UML CHART WHICH IS BELOW
BinaryNumber
private int data[]
private int length
public BinaryNumber(int length)
public BinaryNumber(String str)
public int getLength()
public int getDigit(int index)
public int[] getInnerArray()
public static int[] bwor(BinaryNumber bn1, BinaryNumber bn2)
public static int[] bwand(BinaryNumber bn1, BinaryNumber bn2)
public void bitShift(int direction, int amount)
public void add(BinaryNumber aBinaryNumber)
public String toString()
public int toDecimal()
BinaryNumber
private int data[]
private int length
public BinaryNumber(int length)
public BinaryNumber(String str)
public int getLength()
public int getDigit(int index)
public int[] getInnerArray()
public static int[] bwor(BinaryNumber bn1, BinaryNumber bn2)
public static int[] bwand(BinaryNumber bn1, BinaryNumber bn2)
public void bitShift(int direction, int amount)
public void add(BinaryNumber aBinaryNumber)
public String toString()
public int toDecimal()
Explanation / Answer
//java program
import java.lang.Math;
class BinaryNumber {
private int data[];
private int length;
public BinaryNumber(int length) {
data = new int [length];
this.length=length;
for (int i=0;i<length ;i++) {
data[i] = 0;
}
}
public BinaryNumber(String str) {
this.length=str.length();
data = new int [length];
for (int i=0;i<length ;i++) {
data[i] = str.charAt(i)-'0';
}
}
public int getLength() {
return this.length;
}
public int getDigit(int index) {
if(index < this.length)return data[index];
else return -1;
}
public int[] getInnerArray() {
return data;
}
public void bitShift(int direction, int amount) {
if(direction == -1) {
for (int i=0;i<length-amount ;i++)
data [i]=data[i+amount];
for (int i=amount ;i<length ;i++)
data [i]=0;
}
if(direction == 1) {
for (int i=0;i<length-amount ;i++)
data [i+amount]=data[i];
for (int i=0 ;i<amount ;i++)
data [i]=0;
}
}
public void add(BinaryNumber aBinaryNumber) {
int carry =0,sum;
int l1=this.length-1;
int l2 =aBinaryNumber.length-1;
while(l1>=0 &&l2>=0) {
sum=(this.data[l1]+aBinaryNumber.data[l2]+carry);
this.data[l1]=sum%2;
carry=sum/2;
l1--;
l2--;
}
while(l1>=0) {
sum=(this.data[l1]+carry);
this.data[l1]=sum%2;
carry=sum/2;
l1--;
}
}
public String toString() {
String str ="";
for (int i=0;i<length ;i++)
str+=data[i];
return str;
}
public int toDecimal() {
int total=0;
for(int i=0; i<length ;i++) {
total +=data[i]*Math.pow(2,length-i-1);
}
return total;
}
}
public class Binary {
public static void main(String args[]) {
}
public static int[] bwor(BinaryNumber bn1, BinaryNumber bn2) {
int l1=bn1.getLength()-1;
int l2 =bn2.getLength()-1;
int max = l1>l2 ?l1:l2;
int val=0;
int d[] =new int [max+1];
while(l1>=0 && l2>=0) {
if(bn1.getDigit(l1)==0 &&bn2.getDigit(l2)==0)val=0;
if(bn1.getDigit(l1)==0 &&bn2.getDigit(l2)==1)val=1;
if(bn1.getDigit(l1)==1 &&bn2.getDigit(l2)==0)val=1;
if(bn1.getDigit(l1)==1 &&bn2.getDigit(l2)==1)val=1;
d[max]=val;
l1--;
l2--;
max--;
}
while(l1>=0) {
d[max]=bn1.getDigit(l1);
l1--;
max--;
}
while(l2>=0) {
d[max]=bn2.getDigit(l2);
l2--;
max--;
}
return d;
}
public static int[] bwand(BinaryNumber bn1, BinaryNumber bn2) {
int l1=bn1.getLength()-1;
int l2 =bn2.getLength()-1;
int max = l1>l2 ?l1:l2;
int val=0;
int d[] =new int [max+1];
while(l1>=0 && l2>=0) {
if(bn1.getDigit(l1)==0 &&bn2.getDigit(l2)==0)val=0;
if(bn1.getDigit(l1)==0 &&bn2.getDigit(l2)==1)val=0;
if(bn1.getDigit(l1)==1 &&bn2.getDigit(l2)==0)val=0;
if(bn1.getDigit(l1)==1 &&bn2.getDigit(l2)==1)val=1;
d[max]=val;
l1--;
l2--;
max--;
}
while(l1>=0) {
d[max]=bn1.getDigit(l1);
l1--;
max--;
}
while(l2>=0) {
d[max]=bn2.getDigit(l2);
l2--;
max--;
}
return d;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.