Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class BitStrings {
  4.    
  5.     static int commArray[];
  6.     static String bitStringOne;
  7.     static String bitStringTwo;
  8.     static String operator = " ";
  9.     static String command;
  10.     static String promptOne = "\nPlease enter an 8-bit string: ";
  11.     static String promptTwo = "\nPlease enter a command: ";
  12.     static String[] tokens = new String[2];
  13.     static Scanner in = new Scanner(System.in);
  14.  
  15.  
  16.     public static void main(String[] args) {
  17.         while (true)
  18.         {
  19.             System.out.println(promptOne);
  20.             bitStringOne = in.next();
  21.  
  22.             // Place scanner on the next line. If we don't do this, the next time we call nextLine(), we will get null.
  23.             in.nextLine();
  24.  
  25.             // If the bit string is 8 bits long, continue.
  26.             if (bitStringOne.length() == 8)
  27.             {
  28.                 while (promptUser())
  29.                 {
  30.                 }
  31.             }
  32.             // Otherwise, prompt the user again.
  33.             else
  34.             {
  35.                 System.out.println("The string is not 8 bits long!");
  36.             }
  37.         }
  38.     }
  39.  
  40.     public static boolean promptUser()
  41.     {
  42.         System.out.println(promptTwo);
  43.         command = in.nextLine();
  44.  
  45.         // If command contains a space character, split it into tokens.
  46.         if (command.indexOf(' ') != -1)
  47.         {
  48.             tokens = command.split(" ");
  49.             operator = tokens[0];
  50.             bitStringTwo = tokens[1];
  51.            
  52.             // If the bit string is not 8 bits long, prompt user again.
  53.             if (bitStringTwo.length() != 8)
  54.             {
  55.                 System.out.println("The string is not 8 bits long!");
  56.                 return true;
  57.             }
  58.  
  59.             // If the operator is 'and', perform AND operation.
  60.             if (operator.equalsIgnoreCase("and"))
  61.             {
  62.                 AND(bitStringOne, bitStringTwo);
  63.             }
  64.             // If the operator is 'or', perform OR operation.
  65.             else if (operator.equalsIgnoreCase("or"))
  66.             {
  67.                 OR(bitStringOne, bitStringTwo);
  68.             }
  69.             // If the user enters an invalid command, prompt again.
  70.             else
  71.             {
  72.                 System.out.println("Invalid command!");
  73.                 return true;
  74.             }
  75.         }
  76.         else
  77.         {
  78.             // If user types 'q', exit the program.
  79.             if (command.equalsIgnoreCase("q"))
  80.             {
  81.                 System.exit(0);
  82.             }
  83.             // If user types 'n', prompt the user for a new first bitstring.
  84.             else if (command.equalsIgnoreCase("n"))
  85.             {
  86.                 return false;
  87.             }
  88.             // If user types 'not', perform NOT operation.
  89.             else if (command.equalsIgnoreCase("not"))
  90.             {
  91.                 NOT(bitStringOne);
  92.             }
  93.             // If user types an invalid command, prompt again.
  94.             else
  95.             {
  96.                 System.out.println("Invalid command!");
  97.                 return true;
  98.             }
  99.         }
  100.  
  101.         return true;
  102.     }
  103.  
  104.     public static void AND(String str1, String str2)
  105.     {
  106.         int numArray1[] = new int[str1.length()];
  107.         int numArray2[] = new int[str2.length()];
  108.         int numArray3[] = new int[numArray1.length];
  109.         String output = "";
  110.        
  111.         for (int k = 0; k < str1.length(); k++) {
  112.             numArray1[k] = Character.digit(str1.charAt(k), 10);
  113.         }
  114.        
  115.         for (int l = 0; l < str2.length(); l++) {
  116.             numArray2[l] = Character.digit(str2.charAt(l), 10);
  117.         }
  118.        
  119.         ///////////////////////////////////////////////////////////////////
  120.  
  121.         for(int i =0; i<numArray1.length; i++){
  122.             if(numArray1[i] == 1 && numArray2[i]== 1){
  123.                 numArray3[i] = numArray1[i];   
  124.             }
  125.             else
  126.             {
  127.                 numArray3[i] = 0;
  128.             }
  129.         }
  130.        
  131.         for (int i = 0; i< numArray3.length; i++){
  132.             output+= numArray3[i];
  133.         }
  134.  
  135.         System.out.println("Result of operation AND: " + output);
  136.     }
  137.  
  138.     public static void OR(String str1, String str2)
  139.     {
  140.         int numArray1[] = new int[str1.length()];
  141.         int numArray2[] = new int[str2.length()];
  142.         int numArray3[] = new int[numArray1.length];
  143.         String output = "";
  144.        
  145.         for (int k = 0; k < str1.length(); k++) {
  146.             numArray1[k] = Character.digit(str1.charAt(k), 10);
  147.         }
  148.        
  149.         for (int l = 0; l < str2.length(); l++) {
  150.             numArray2[l] = Character.digit(str2.charAt(l), 10);
  151.         }
  152.        
  153.        
  154.         ///////////////////////////////////////////////////////////////////
  155.  
  156.         for(int i =0; i<numArray1.length; i++){
  157.             if(numArray1[i] == 1 || numArray2[i] == 1){
  158.                 numArray3[i] = 1;  
  159.             }
  160.             else
  161.             {
  162.                 numArray3[i] = 0;
  163.             }
  164.         }
  165.        
  166.         for (int i = 0; i< numArray3.length; i++){
  167.             output+= numArray3[i];
  168.         }
  169.        
  170.         System.out.println("Result of operation OR: " + output);       
  171.     }
  172.  
  173.     public static void NOT(String str1)                                    
  174.     {
  175.         int numArray1[] = new int[str1.length()];
  176.         int numArray2[] = new int[numArray1.length];
  177.         String output = "";
  178.  
  179.         for (int k = 0; k < str1.length(); k++) {
  180.             numArray1[k] = Character.digit(str1.charAt(k), 10);
  181.         }
  182.        
  183.         // Swap the bits
  184.         for(int i =0; i<numArray1.length; i++){
  185.             if(numArray1[i] == 1){
  186.                 numArray2[i] = 0;  
  187.             }
  188.             else
  189.             {
  190.                 numArray2[i] = 1;
  191.             }
  192.         }
  193.        
  194.         for (int i = 0; i< numArray2.length; i++){
  195.             output+= numArray2[i];
  196.         }
  197.        
  198.         System.out.println("Result of operation NOT: " + output);      
  199.    
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement