Reputation: 147
Currently, I just have data
store the length of the bits, which isn't what I'm looking for.I would like to store the bits generated by the BinaryNumber
constructor into the attribute private int data[]
. data
would later be used for other methods (for retrieving bit length,conversion to decimal, etc.). I am pretty new to Java programming, so I would very much appreciate if you can help me with this problem that I am facing.
public class BinaryNumber {
private int data[];
private boolean overflow;
/**
* Creates a binary number of length length
* and consisting only of zeros.
*
* @param Length
*
**/
public BinaryNumber(int length){
String binaryNum=" ";
for (int i=0; i<length;i++) {
binaryNum += "0";
}
System.out.println(binaryNum);
data= new int[length];
}
public static void main (String[] args) {
BinaryNumber t1=new BinaryNumber(7);
System.out.println(t1);
}
For example, the test above should generate seven 0s (0000000) and I would like to store those seven bits into the array data
instead of what I do in my current code, which is storing it into a String binaryNum
.
Upvotes: 3
Views: 1046
Reputation: 1106
public BinaryNumber(int length) {
data = new int[length];
for (int i = 0; i < length; i++) {
data[i] = 0;
}
}
The Java spec, however, guarantees that all int
values are initialized with 0, so your constructor could be simplified to just:
public BinaryNumber(int length) {
data = new int[length];
}
Upvotes: 2