Advertisement
Guest User

Lab 2

a guest
Sep 19th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. package sep19;
  2. /*
  3.  * Jacob Cacciamani
  4.  * ID: E02073983
  5.  * 9/19/19
  6.  * Fall 2019
  7.  * COSC 221
  8.  * Section 1
  9.  */
  10.  
  11. import java.util.Scanner;
  12. /*
  13.  * This program is a simple menu based program that can be used to
  14.  * convert binary to decimal
  15.  * and decimal to binary in the two's compliment system.
  16.  */
  17. public class ConverterTwosCompliment {
  18.  
  19.     public static String twosCompliment(String bin) {
  20.         char arr[] = bin.toCharArray();
  21.         int len = bin.length();
  22.         boolean addOne = true;
  23.         // Flip Bits
  24.         for (int i = len - 1; i >= 0; i--) {
  25.             if (arr[i] == '0')
  26.                 arr[i] = '1';
  27.             else
  28.                 arr[i] = '0';
  29.         }
  30.         // Add 1
  31.         for (int i = len - 1; i >= 0 && addOne; i--) {
  32.                 if (arr[i] == '0') {
  33.                     arr[i] = '1';
  34.                     addOne = false;
  35.                 }
  36.                 else {
  37.                     arr[i] = '0';
  38.                 }
  39.         }
  40.         return new String(arr);
  41.     }
  42.  
  43.     public static String convertToBinary(String dec) {
  44.         int decimal = Integer.parseInt(dec);
  45.         boolean negative = decimal < 0;
  46.         String res = "";
  47.         for (int i = 0; i < 7; i++) {
  48.             res += (char)decimal % 2;
  49.             decimal /= 2;
  50.         }
  51.         // If negative, use two's compliment, otherwise return
  52.         return (negative) ? twosCompliment(reverseString(res + "0")) : reverseString(res + "0");
  53.     }
  54.  
  55.     // Helper method for reversing binary strings so they aren't backwards
  56.     public static String reverseString(String s) {
  57.         String res = "";
  58.         for (int i = s.length() - 1; i >= 0; i--) {
  59.             res += s.charAt(i);
  60.         }
  61.         return res;
  62.     }
  63.  
  64.     public static int convertToDecimal(String binary) {
  65.         int sum = 0;
  66.         int len = binary.length();
  67.         boolean negative = false;
  68.         if (binary.charAt(0) == '1') {
  69.             binary = twosCompliment(binary);
  70.             negative = true;
  71.         }
  72.         for (int i = 1; i < len; i++) {
  73.             if (binary.charAt(i) == '1')
  74.                 sum += Math.pow(2, 7 - i);
  75.         }
  76.         return (negative) ? -sum : sum;
  77.     }
  78.  
  79.     public static void main(String args[]) {
  80.         int response;
  81.         String num;
  82.         Scanner scan = new Scanner(System.in);
  83.         do {
  84.             System.out.println("********************************************");
  85.             System.out.println("Please enter 1 to convert decimal to binary");
  86.             System.out.println("Please enter 2 to convert binary to decimal");
  87.             System.out.println("Enter 3 to quit");
  88.             response = scan.nextInt();
  89.             if (response == 1) {
  90.                 System.out.println("Enter a decimal number between 127 and -128");
  91.                 num = scan.next();
  92.                 System.out.println("Your converted number is: " + convertToBinary(num));
  93.             }
  94.             else if (response == 2) {
  95.                 System.out.println("Enter an 8-bit binary string");
  96.                 num = scan.next();
  97.                 System.out.println("Your converted number is: " + convertToDecimal(num));
  98.             }
  99.         } while(response == 1 || response == 2);
  100.         System.out.println("Goodbye!");
  101.         scan.close();
  102.     }
  103. }
  104. /*
  105. ********************************************
  106. Please enter 1 to convert decimal to binary
  107. Please enter 2 to convert binary to decimal
  108. Enter 3 to quit
  109. 1
  110. Enter a decimal number between 127 and -128
  111. -2
  112. Your converted number is: 11111110
  113. ********************************************
  114. Please enter 1 to convert decimal to binary
  115. Please enter 2 to convert binary to decimal
  116. Enter 3 to quit
  117. 2
  118. Enter an 8-bit binary string
  119. 01100100
  120. Your converted number is: 100
  121. ********************************************
  122. Please enter 1 to convert decimal to binary
  123. Please enter 2 to convert binary to decimal
  124. Enter 3 to quit
  125. 3
  126. Goodbye!
  127. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement