Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Bit String Flick
- // Write methods for a program that will perform Bit-String Flick operations fr Shifts and Circulates.
- import java.util.Scanner;
- public class BitStringFlick
- {
- private char leftRight;
- private String circShift;
- private int index;
- private String bitString;
- private char[] answer;
- /** Calls setExpression with default values */
- public BitStringFlick()
- {
- setExpression('L', "Shift", 0, "");
- }
- /** Calls setExpression with parameters given */
- public BitStringFlick(char leftOrRight, String circOrShift, int userIndex, String userBits)
- {
- setExpression(leftOrRight, circOrShift, userIndex, userBits);
- }
- /** Sets the instance data based upon the parameters given */
- public void setExpression(char leftOrRight, String circOrShift, int userIndex, String userBits)
- {
- leftRight = leftOrRight;
- circShift = circOrShift;
- index = userIndex;
- bitString = userBits;
- answer = new char[bitString.length()];
- }
- /** Performs an r-shift operation on the bitString - the result is stored in char[] answer */
- public void rShift()
- {
- int place = index;
- int bitsToMove = bitString.length() - index;
- for (int i = bitString.length(); i > bitsToMove; i--)
- {
- answer[i - 1] = bitString.charAt(place);
- place--;
- }
- for (place = 0; place < bitsToMove; place++)
- {
- answer[place] = '0';
- }
- }
- /** Performs an l-shift operation on the bitString - the result is stored in char[] answer */
- public void lShift()
- {
- int place = index;
- int bitsToMove = bitString.length() - index;
- for (int i = 0; i < bitsToMove; i++)
- {
- answer[i] = bitString.charAt(place);
- place++;
- }
- for (place = bitsToMove; place < bitString.length(); place++)
- {
- answer[place] = '0';
- }
- }
- /** Performs an r-circ operation on the bitString - the result is stored in char[] answer */
- public void rCirc()
- {
- int length = bitString.length();
- int circIndex = index % length;
- for(int i = 0; i < circIndex; i++)
- {
- answer[i] = bitString.charAt((length - circIndex) + i);
- }
- for (int i = 0; i < (length - circIndex); i++)
- {
- answer[circIndex + i] = bitString.charAt(i);
- }
- }
- /** Performs an l-circ operation on the bitString - the result is stored in char[] answer */
- public void lCirc()
- {
- int length = bitString.length();
- int circIndex = index % length;
- for(int i = circIndex; i < length; i++)
- {
- answer[i - circIndex] = bitString.charAt(i);
- }
- for (int i = (length - circIndex); i < length; i++)
- {
- answer[i] = bitString.charAt(i - 1);
- }
- }
- /** Calls the appropriate method based upon the values of leftRight and circShift */
- public void solve()
- {
- if (circShift.equalsIgnoreCase("Shift"))
- {
- if (leftRight == 'R' || leftRight == 'r')
- {
- rShift();
- }
- if (leftRight == 'L' || leftRight == 'l')
- {
- lShift();
- }
- }
- else if (circShift.equalsIgnoreCase("Circ"))
- {
- if (leftRight == 'R' || leftRight == 'r')
- {
- rCirc();
- }
- if (leftRight == 'L' || leftRight == 'l')
- {
- lCirc();
- }
- }
- }
- /* Converts the char[] answer to a String and returns the result */
- public String toString()
- {
- String result = "";
- // convesion from char[] to String
- for(char c : answer)
- {
- result += Character.toString(c);
- }
- return result;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Bit String Flick
- // Write methodds for a program that will perform Bit-String Flick operations fr Shifts and Circulates.
- import java.util.Stack;
- import java.util.Scanner;
- public class BitStringFlickClient
- {
- public static void main(String args[])
- {
- BitStringFlick test = new BitStringFlick('L', "Shift", 3, "10101");
- test.solve();
- System.out.println(test);
- test.setExpression('R', "Shift", 3, "10101");
- test.solve();
- System.out.println(test);
- test.setExpression('L', "Circ", 4, "10101");
- test.solve();
- System.out.println(test);
- test.setExpression('R', "Circ", 2, "10101");
- test.solve();
- System.out.println(test);
- test.setExpression('L', "Circ", 12, "10101");
- test.solve();
- System.out.println(test);
- }
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement