StefanTobler

Assignment 7

Dec 8th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. /*
  2. For this lab, you will enter two numbers in base ten and translate them to binary. You will then add the numbers in binary and print out the result.
  3. All numbers entered will be between 0 and 255, inclusive, and binary output is limited to 8 bits. This means that the sum of the two added numbers will also be limited to 8 bits. If the sum of the two numbers is more than 8 bits, please print the first 8 digits of the sum and the message "Error: overflow".
  4. Your program should represent binary numbers using integer arrays, with the leftmost digit (2^7) stored at index 0 and the rightmost digit (2^0) stored at index 7.
  5. Your program should include the following methods:
  6. int[] convertToBinary(int b)Translates the parameter to a binary value and returns it stored as an array of ints.
  7. void printBin(int b[])Outputs the binary number stored in the array on one line. Please note, there should be exactly one space between each output 0 or 1.
  8. int[] addBin(int a[], int b[])Adds the two binary numbers stored in the arrays, and returns the sum in a new array of ints.
  9. Sample Run 1:
  10. Enter a base ten number between 0 and 255, inclusive.
  11. 56
  12. Enter a base ten number between 0 and 255, inclusive.
  13. 2
  14.  
  15. First binary number:
  16. 0 0 1 1 1 0 0 0
  17.  
  18. Second binary number:
  19. 0 0 0 0 0 0 1 0
  20.  
  21. Added:
  22. 0 0 1 1 1 0 1 0
  23. Sample Run 2:
  24. Enter a base ten number between 0 and 255, inclusive.
  25. 200
  26. Enter a base ten number between 0 and 255, inclusive.
  27. 150
  28.  
  29. First binary number:
  30. 1 1 0 0 1 0 0 0
  31.  
  32. Second binary number:
  33. 1 0 0 1 0 1 1 0
  34.  
  35. Added:
  36. Error: overflow
  37. 0 1 0 1 1 1 1 0
  38. */
  39. import java.util.Scanner;
  40. import java.lang.Math;
  41.  
  42. class Main {
  43.  
  44.   public static int[] convertToBinary(int b)
  45.   {
  46.     // 128 64 32 16 8 4 2 1
  47.     int bin[] = new int[8];
  48.     while (b > 0)
  49.     {
  50.       if (b/128 == 1)
  51.       {
  52.         bin[0] = 1;
  53.         b -= 128;
  54.       }
  55.       else if (b/64 == 1)
  56.       {
  57.         bin[1] = 1;
  58.         b -= 64;
  59.       }
  60.       else if (b/32 == 1)
  61.       {
  62.         bin[2] = 1;
  63.         b -= 32;
  64.       }
  65.       else if (b/16 == 1)
  66.       {
  67.         bin[3] = 1;
  68.         b -= 16;
  69.       }
  70.       else if (b/8 == 1)
  71.       {
  72.         bin[4] = 1;
  73.         b -= 8;
  74.       }
  75.       else if (b/4 == 1)
  76.       {
  77.         bin[5] = 1;
  78.         b -= 4;
  79.       }
  80.       else if (b/2 == 1)
  81.       {
  82.         bin[6] = 1;
  83.         b -= 2;
  84.       }
  85.       else if (b/1 == 1)
  86.       {
  87.         bin[7] = 1;
  88.         b -= 1;
  89.       }
  90.       for (int i = 0; i < bin.length; i++)
  91.       {
  92.         if (bin[i] != 1)
  93.           bin[i] = 0;
  94.       }
  95.     }
  96.     return bin;
  97.   }
  98.  
  99.   //
  100.  
  101.   public static void printBin(int b[])
  102.   {
  103.     for (int i = 0; i < 8; i++)
  104.         System.out.print(b[i] + " ");
  105.   }
  106.  
  107.   //
  108.  
  109.   public static int[] addBin(int a[], int b[])
  110.   {
  111.     int add[] = new int[8];
  112.     int sum = 0;
  113.     int tot = 128;
  114.      
  115.     for (int i = 0; i < 8; i++)
  116.     {
  117.       if (a[i] == 1)
  118.       {
  119.         sum += tot;
  120.       }
  121.       if (b[i] == 1)
  122.       {
  123.         sum += tot;
  124.       }
  125.       tot = tot/2;
  126.     }
  127.    
  128.     tot = 128;
  129.     if (sum > 255)
  130.     {
  131.       System.out.print("Error: overflow");
  132.     }
  133.     for (int i = 0; i < 8; i++)
  134.     {
  135.       if (sum/tot == 1)
  136.       {
  137.         sum -= tot;
  138.         add[i] = 1;
  139.       }
  140.       else
  141.         add[i] = 0;
  142.       tot = tot/2;
  143.     }
  144.    
  145.     return add;
  146.   }
  147.  
  148.   //
  149.  
  150.   public static void main(String[] args)
  151.      {
  152.       Scanner scan = new Scanner(System.in);
  153.       int num;
  154.      
  155.       System.out.println("Enter a number between 0 and 255, inclusive.");
  156.       num = scan.nextInt();
  157.       int bin1[] = convertToBinary(num);
  158.       System.out.println("Enter a number between 0 and 255, inclusive.");
  159.       num = scan.nextInt();
  160.       int bin2[] = convertToBinary(num);
  161.       System.out.println("First binary number:");
  162.       printBin(bin1);
  163.       System.out.println();
  164.       System.out.println("Second binary number:");
  165.       printBin(bin2);
  166.       System.out.println();
  167.       int bin3[] = addBin(bin1, bin2);
  168.       System.out.println("Added:");
  169.       printBin(bin3);
  170.     }
  171. }
Add Comment
Please, Sign In to add comment