Advertisement
Guest User

Untitled

a guest
Apr 10th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1.  
  2. /**
  3. * Represents a string of bit values (0 or 1)
  4. * @
  5. */
  6. public class StringOfBits extends Object {
  7. private StringOfBits sb;
  8. private CharSequence chars;
  9. /**
  10. * Constructs the empty bit string ; length() == 0
  11. */
  12.  
  13. public StringOfBits() {
  14. this.sb = null;
  15. this.chars = null;
  16. }
  17. /**
  18. * Copy constructor
  19. * @param sb
  20. */
  21. public StringOfBits(StringOfBits sb){
  22. this.chars = null;
  23. this.sb = sb;
  24. }
  25.  
  26. /**
  27. * Constructs a bit string from a sequence of '0' and '1' characters. If
  28. * the parameter is not comprised of '0' and '1' characters the behavior
  29. * is undefined.
  30. *
  31. * @param chars the sequence to convert into bits
  32. */
  33.  
  34. public StringOfBits(CharSequence chars){
  35. this.sb = null;
  36. this.chars = chars;
  37. }
  38.  
  39. /**
  40. * Returns the length of this bit string
  41. * @return the number of bits in this string
  42. */
  43. public int length(){
  44. return this.sb.length();
  45. }
  46.  
  47. /**
  48. * Appends the bit string representation of the parameter to this bit string.
  49. * Each digit represents a single bit. If any digit of the parameter is not
  50. * 0 or 1 the behavior is undefined.
  51. * @param i
  52. * @return a reference to this bit string
  53. */
  54. public StringOfBits append(int i) {
  55. this.sb.append(i);
  56. return sb;
  57. }
  58.  
  59. /**
  60. * Appends the bit string representation of the parameter to this bit string.
  61. * Each digit represents a single bit. If any digit of the parameter is not
  62. * 0 or 1 the behavior is undefined.
  63. * @param str - a string
  64. * @return a reference to this bit string
  65. */
  66. public StringOfBits append(CharSequence str){
  67. this.sb.append(str);
  68. return sb;
  69. }
  70. /**
  71. * Appends the paramter to this bit string
  72. * @param bitstr - a string
  73. * @return a reference to this bit string
  74. */
  75. public StringOfBits append(StringOfBits bitstr){
  76. return this.sb.append(bitstr);
  77. }
  78.  
  79. /**
  80. * Returns a char corresponding to the bit at the specified index.
  81. * @param index - the index of the desired bit value
  82. * @return the char value at the specified index
  83. * @throws IndexOutOfBoundsException - if index is negative or greater
  84. * than or equal to length
  85. */
  86. public char charAt(int index) throws IndexOutOfBoundsException{
  87. if(index < 0 || index >= length()){
  88. throw new IndexOutOfBoundsException("Out Of Bounds");
  89. }
  90. return this.sb.charAt(index);
  91. }
  92.  
  93. /**
  94. * Returns an int corresponding to the bit at the specified index.
  95. * @param index - the index of the desired bit value
  96. * @return an int value corresponding to specified index
  97. * @throws IndexOutOfBoundsException - if index is negative or greater
  98. * than or equal to length
  99. */
  100.  
  101. public int intAt(int index){
  102. if(index < 0 || index >= length()){
  103. throw new IndexOutOfBoundsException("Out Of Bounds");
  104. }
  105. return this.sb.intAt(index);
  106. }
  107.  
  108. /**
  109. * Returns a boolean corresponding to the bit at the specified index.
  110. * @param index - the index of the desired bit value
  111. * @return the boolean value at the specified index
  112. * @throws IndexOutOfBoundsException - if index is negative or greater
  113. * than or equal to length
  114. */
  115. public boolean booleanAt(int index){
  116. if (index < 0 || index >= length()){
  117. throw new IndexOutOfBoundsException("Out Of Bounds");
  118. }
  119. if (this.sb.charAt(index) == 0) {
  120. return true;
  121. }
  122. else if (this.sb.charAt(index) == 1) {
  123. return false;
  124. }
  125. else {
  126. return false;
  127. }
  128.  
  129. }
  130.  
  131. /**
  132. * Sets the bit at the specified index. If the parameter is not '0' or '1'
  133. * the behavior of this method is undefined
  134. * @param index - the index of the bit to modify
  135. * @param c - the new value
  136. */
  137.  
  138. public void setBitAt(int index, char c){
  139.  
  140. }
  141.  
  142. /**
  143. * Sets the bit at the specified index. If the parameter is not '0' or '1'
  144. * the behavior of this method is undefined
  145. * @param index - the index of the bit to modify
  146. * @param i - the new value
  147. */
  148.  
  149. public void setBitAt(int index, int i){
  150.  
  151. }
  152.  
  153. /**
  154. * Sets the bit at the specified index.
  155. * @param index - the index of the bit to modify
  156. * @param b- the new value
  157. */
  158.  
  159. public void setBitAt(int index, boolean b){
  160.  
  161. }
  162.  
  163. /**
  164. * Method that over rides th to string method
  165. */
  166. @Override
  167. public String toString(){
  168. return "";
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement