For this lab, you will enter two numbers in base ten and translate them to binar
ID: 3661760 • Letter: F
Question
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 ones digit (2^0) stored at index 0, the twos digit (2^1) stored at index 1, all the way up to the 2^7 digit 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
Explanation / Answer
Below shown the java program to get the desired results :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.