Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Represents a string of bit values (0 or 1)
- * @
- */
- public class StringOfBits extends Object {
- private StringOfBits sb;
- private CharSequence chars;
- /**
- * Constructs the empty bit string ; length() == 0
- */
- public StringOfBits() {
- this.sb = null;
- this.chars = null;
- }
- /**
- * Copy constructor
- * @param sb
- */
- public StringOfBits(StringOfBits sb){
- this.chars = null;
- this.sb = sb;
- }
- /**
- * Constructs a bit string from a sequence of '0' and '1' characters. If
- * the parameter is not comprised of '0' and '1' characters the behavior
- * is undefined.
- *
- * @param chars the sequence to convert into bits
- */
- public StringOfBits(CharSequence chars){
- this.sb = null;
- this.chars = chars;
- }
- /**
- * Returns the length of this bit string
- * @return the number of bits in this string
- */
- public int length(){
- return this.sb.length();
- }
- /**
- * Appends the bit string representation of the parameter to this bit string.
- * Each digit represents a single bit. If any digit of the parameter is not
- * 0 or 1 the behavior is undefined.
- * @param i
- * @return a reference to this bit string
- */
- public StringOfBits append(int i) {
- this.sb.append(i);
- return sb;
- }
- /**
- * Appends the bit string representation of the parameter to this bit string.
- * Each digit represents a single bit. If any digit of the parameter is not
- * 0 or 1 the behavior is undefined.
- * @param str - a string
- * @return a reference to this bit string
- */
- public StringOfBits append(CharSequence str){
- this.sb.append(str);
- return sb;
- }
- /**
- * Appends the paramter to this bit string
- * @param bitstr - a string
- * @return a reference to this bit string
- */
- public StringOfBits append(StringOfBits bitstr){
- return this.sb.append(bitstr);
- }
- /**
- * Returns a char corresponding to the bit at the specified index.
- * @param index - the index of the desired bit value
- * @return the char value at the specified index
- * @throws IndexOutOfBoundsException - if index is negative or greater
- * than or equal to length
- */
- public char charAt(int index) throws IndexOutOfBoundsException{
- if(index < 0 || index >= length()){
- throw new IndexOutOfBoundsException("Out Of Bounds");
- }
- return this.sb.charAt(index);
- }
- /**
- * Returns an int corresponding to the bit at the specified index.
- * @param index - the index of the desired bit value
- * @return an int value corresponding to specified index
- * @throws IndexOutOfBoundsException - if index is negative or greater
- * than or equal to length
- */
- public int intAt(int index){
- if(index < 0 || index >= length()){
- throw new IndexOutOfBoundsException("Out Of Bounds");
- }
- return this.sb.intAt(index);
- }
- /**
- * Returns a boolean corresponding to the bit at the specified index.
- * @param index - the index of the desired bit value
- * @return the boolean value at the specified index
- * @throws IndexOutOfBoundsException - if index is negative or greater
- * than or equal to length
- */
- public boolean booleanAt(int index){
- if (index < 0 || index >= length()){
- throw new IndexOutOfBoundsException("Out Of Bounds");
- }
- if (this.sb.charAt(index) == 0) {
- return true;
- }
- else if (this.sb.charAt(index) == 1) {
- return false;
- }
- else {
- return false;
- }
- }
- /**
- * Sets the bit at the specified index. If the parameter is not '0' or '1'
- * the behavior of this method is undefined
- * @param index - the index of the bit to modify
- * @param c - the new value
- */
- public void setBitAt(int index, char c){
- }
- /**
- * Sets the bit at the specified index. If the parameter is not '0' or '1'
- * the behavior of this method is undefined
- * @param index - the index of the bit to modify
- * @param i - the new value
- */
- public void setBitAt(int index, int i){
- }
- /**
- * Sets the bit at the specified index.
- * @param index - the index of the bit to modify
- * @param b- the new value
- */
- public void setBitAt(int index, boolean b){
- }
- /**
- * Method that over rides th to string method
- */
- @Override
- public String toString(){
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement