Advertisement
Crenox

BitStringFlick Java Program

Nov 10th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. // Sammy Samkough
  2. // Bit String Flick
  3. // Write methods for a program that will perform Bit-String Flick operations fr Shifts and Circulates.
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class BitStringFlick
  8. {
  9. private char leftRight;
  10. private String circShift;
  11. private int index;
  12. private String bitString;
  13. private char[] answer;
  14.  
  15. /** Calls setExpression with default values */
  16. public BitStringFlick()
  17. {
  18. setExpression('L', "Shift", 0, "");
  19. }
  20.  
  21. /** Calls setExpression with parameters given */
  22. public BitStringFlick(char leftOrRight, String circOrShift, int userIndex, String userBits)
  23. {
  24. setExpression(leftOrRight, circOrShift, userIndex, userBits);
  25. }
  26.  
  27. /** Sets the instance data based upon the parameters given */
  28. public void setExpression(char leftOrRight, String circOrShift, int userIndex, String userBits)
  29. {
  30. leftRight = leftOrRight;
  31. circShift = circOrShift;
  32. index = userIndex;
  33. bitString = userBits;
  34. answer = new char[bitString.length()];
  35. }
  36.  
  37. /** Performs an r-shift operation on the bitString - the result is stored in char[] answer */
  38. public void rShift()
  39. {
  40. int place = index;
  41. int bitsToMove = bitString.length() - index;
  42.  
  43. for (int i = bitString.length(); i > bitsToMove; i--)
  44. {
  45. answer[i - 1] = bitString.charAt(place);
  46. place--;
  47. }
  48.  
  49. for (place = 0; place < bitsToMove; place++)
  50. {
  51. answer[place] = '0';
  52. }
  53. }
  54.  
  55. /** Performs an l-shift operation on the bitString - the result is stored in char[] answer */
  56. public void lShift()
  57. {
  58. int place = index;
  59. int bitsToMove = bitString.length() - index;
  60.  
  61. for (int i = 0; i < bitsToMove; i++)
  62. {
  63. answer[i] = bitString.charAt(place);
  64. place++;
  65. }
  66.  
  67. for (place = bitsToMove; place < bitString.length(); place++)
  68. {
  69. answer[place] = '0';
  70. }
  71. }
  72.  
  73. /** Performs an r-circ operation on the bitString - the result is stored in char[] answer */
  74. public void rCirc()
  75. {
  76. int length = bitString.length();
  77. int circIndex = index % length;
  78.  
  79. for(int i = 0; i < circIndex; i++)
  80. {
  81. answer[i] = bitString.charAt((length - circIndex) + i);
  82. }
  83.  
  84. for (int i = 0; i < (length - circIndex); i++)
  85. {
  86. answer[circIndex + i] = bitString.charAt(i);
  87. }
  88. }
  89.  
  90. /** Performs an l-circ operation on the bitString - the result is stored in char[] answer */
  91. public void lCirc()
  92. {
  93. int length = bitString.length();
  94. int circIndex = index % length;
  95.  
  96. for(int i = circIndex; i < length; i++)
  97. {
  98. answer[i - circIndex] = bitString.charAt(i);
  99. }
  100.  
  101. for (int i = (length - circIndex); i < length; i++)
  102. {
  103. answer[i] = bitString.charAt(i - 1);
  104. }
  105. }
  106.  
  107. /** Calls the appropriate method based upon the values of leftRight and circShift */
  108. public void solve()
  109. {
  110. if (circShift.equalsIgnoreCase("Shift"))
  111. {
  112. if (leftRight == 'R' || leftRight == 'r')
  113. {
  114. rShift();
  115. }
  116. if (leftRight == 'L' || leftRight == 'l')
  117. {
  118. lShift();
  119. }
  120. }
  121. else if (circShift.equalsIgnoreCase("Circ"))
  122. {
  123. if (leftRight == 'R' || leftRight == 'r')
  124. {
  125. rCirc();
  126. }
  127. if (leftRight == 'L' || leftRight == 'l')
  128. {
  129. lCirc();
  130. }
  131. }
  132. }
  133.  
  134. /* Converts the char[] answer to a String and returns the result */
  135. public String toString()
  136. {
  137. String result = "";
  138.  
  139. // convesion from char[] to String
  140. for(char c : answer)
  141. {
  142. result += Character.toString(c);
  143. }
  144.  
  145. return result;
  146. }
  147. }
  148. -------------------------------------------------------------------------------------------------------------------------------
  149. // Sammy Samkough
  150. // Bit String Flick
  151. // Write methodds for a program that will perform Bit-String Flick operations fr Shifts and Circulates.
  152.  
  153. import java.util.Stack;
  154. import java.util.Scanner;
  155.  
  156. public class BitStringFlickClient
  157. {
  158. public static void main(String args[])
  159. {
  160. BitStringFlick test = new BitStringFlick('L', "Shift", 3, "10101");
  161. test.solve();
  162. System.out.println(test);
  163.  
  164. test.setExpression('R', "Shift", 3, "10101");
  165. test.solve();
  166. System.out.println(test);
  167.  
  168. test.setExpression('L', "Circ", 4, "10101");
  169. test.solve();
  170. System.out.println(test);
  171.  
  172. test.setExpression('R', "Circ", 2, "10101");
  173. test.solve();
  174. System.out.println(test);
  175.  
  176. test.setExpression('L', "Circ", 12, "10101");
  177. test.solve();
  178. System.out.println(test);
  179. }
  180. }
  181. /*
  182.  
  183. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement