Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- 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".
- 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.
- Your program should include the following methods:
- int[] convertToBinary(int b)Translates the parameter to a binary value and returns it stored as an array of ints.
- 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.
- 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.
- Sample Run 1:
- Enter a base ten number between 0 and 255, inclusive.
- 56
- Enter a base ten number between 0 and 255, inclusive.
- 2
- First binary number:
- 0 0 1 1 1 0 0 0
- Second binary number:
- 0 0 0 0 0 0 1 0
- Added:
- 0 0 1 1 1 0 1 0
- Sample Run 2:
- Enter a base ten number between 0 and 255, inclusive.
- 200
- Enter a base ten number between 0 and 255, inclusive.
- 150
- First binary number:
- 1 1 0 0 1 0 0 0
- Second binary number:
- 1 0 0 1 0 1 1 0
- Added:
- Error: overflow
- 0 1 0 1 1 1 1 0
- */
- import java.util.Scanner;
- import java.lang.Math;
- class Main {
- public static int[] convertToBinary(int b)
- {
- // 128 64 32 16 8 4 2 1
- int bin[] = new int[8];
- while (b > 0)
- {
- if (b/128 == 1)
- {
- bin[0] = 1;
- b -= 128;
- }
- else if (b/64 == 1)
- {
- bin[1] = 1;
- b -= 64;
- }
- else if (b/32 == 1)
- {
- bin[2] = 1;
- b -= 32;
- }
- else if (b/16 == 1)
- {
- bin[3] = 1;
- b -= 16;
- }
- else if (b/8 == 1)
- {
- bin[4] = 1;
- b -= 8;
- }
- else if (b/4 == 1)
- {
- bin[5] = 1;
- b -= 4;
- }
- else if (b/2 == 1)
- {
- bin[6] = 1;
- b -= 2;
- }
- else if (b/1 == 1)
- {
- bin[7] = 1;
- b -= 1;
- }
- for (int i = 0; i < bin.length; i++)
- {
- if (bin[i] != 1)
- bin[i] = 0;
- }
- }
- return bin;
- }
- //
- public static void printBin(int b[])
- {
- for (int i = 0; i < 8; i++)
- System.out.print(b[i] + " ");
- }
- //
- public static int[] addBin(int a[], int b[])
- {
- int add[] = new int[8];
- int sum = 0;
- int tot = 128;
- for (int i = 0; i < 8; i++)
- {
- if (a[i] == 1)
- {
- sum += tot;
- }
- if (b[i] == 1)
- {
- sum += tot;
- }
- tot = tot/2;
- }
- tot = 128;
- if (sum > 255)
- {
- System.out.print("Error: overflow");
- }
- for (int i = 0; i < 8; i++)
- {
- if (sum/tot == 1)
- {
- sum -= tot;
- add[i] = 1;
- }
- else
- add[i] = 0;
- tot = tot/2;
- }
- return add;
- }
- //
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in);
- int num;
- System.out.println("Enter a number between 0 and 255, inclusive.");
- num = scan.nextInt();
- int bin1[] = convertToBinary(num);
- System.out.println("Enter a number between 0 and 255, inclusive.");
- num = scan.nextInt();
- int bin2[] = convertToBinary(num);
- System.out.println("First binary number:");
- printBin(bin1);
- System.out.println();
- System.out.println("Second binary number:");
- printBin(bin2);
- System.out.println();
- int bin3[] = addBin(bin1, bin2);
- System.out.println("Added:");
- printBin(bin3);
- }
- }
Add Comment
Please, Sign In to add comment