Design and implement a class called DecToBinArray and write a test program called DecToBinArrayTest.
DecToBinArray is utility class that converts a nonnegative decimal number between 0 and 255 into an array of 0's and 1's. For example, you should be able to do the following:
DecToBinArray arr = new DecToBinArray(120);
arr.convert();
System.out.println(arr);
DecToBinArrayTest should also implement toString() method properly so that the above code snippet print out the standard output 01111000.
NOTE: that the length of the array used in DecToBinArray is always 8 since integers from 0 to 255 can be represented with 8 bits.
DecToBinArrayTest reads a string of digits representing a nonnegative integer from 0 to 255. you may read a line of text from the standard in using the following:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in))
1
Expert's answer
2012-04-10T07:35:10-0400
public class DecToBinArray {
private int n;
public DecToBinArray(int n){
if ((n<0) || (n>255)) throw new IllegalArgumentException("Number shold be [0;255]");
Comments
Leave a comment