hello programmers! i have a homework on java that i really need help with. it is
ID: 640868 • Letter: H
Question
hello programmers! i have a homework on java that i really need help with. it is add/sub two 8-bit singed binary numbers. if anybody can help that would be great. thank you in advance
write a java program to prefrom binary addtion and subtraction with 8-bit singed binary numbers.
you are to write a menu driven program that gives the user three choices, namely, (1) add, (2) subtract, or (3) Exit. the input to your program will be two singed binary numbers. the program should display the input/output numbers in binary.
A sample run:
senario #1: adding +13 + (-5)
choose (A)dd, (S)ubtract, (E)xit :A
Enter 8-bit singed binary number: 00001101 (i.e. +13)
Enter another binary number: 11111011 ( i.e. -5)
the sum in binary is: 00010010 (i.e. +8)
scenario #2 subtracting +13 - (-5)
choose (A)dd, (S)ubtract, (E)xit: S
enter 8-bit singed binary number: 00001101 (i.e.+13)
enter another binary binary number: 11111011 (i.e.-5)
the difference in binary is: 00010010 (i.e. +18)
choose (A)dd, (S)ubtract, (E)xit: E
thank you.
Explanation / Answer
import java.util.Scanner;
public class BinaryOperations {
public static String convert(String line){
String Temp = "";
int flag = 1;
for(int i = line.length() - 1; i >= 0; --i){
if(flag == 1){
if(line.charAt(i) == '1'){
flag = 0;
}
Temp += line.charAt(i);
}
else if(flag == 0){
if(line.charAt(i) == '1'){
Temp += '0';
}
else{
Temp += '1';
}
}
}
line = "";
for(int i = Temp.length() - 1; i >= 0; --i){
line += Temp.charAt(i);
}
return line;
}
public static void main(String args[]){
Scanner in = new Scanner(System.in);
char ch;
int num1, num2, res, mul1, mul2;
String line1, line2, temp;
do{
System.out.print("choose (A)dd, (S)ubtract, (E)xit :");
ch = in.nextLine().charAt(0);
switch(ch){
case 'A':
case 'a':
System.out.print("Enter 8-bit singed binary number: ");
line1 = in.nextLine();
System.out.print("Enter another binary number: ");
line2 = in.nextLine();
mul1 = 1;
mul2 = 1;
if(line1.charAt(0) == '1'){
line1 = convert(line1);
System.out.println(line1);
mul1 = -1;
}
if(line2.charAt(0) == '1'){
line2 = convert(line2);
System.out.println(line2);
mul2 = -1;
}
num1 = Integer.parseInt(line1, 2);
num2 = Integer.parseInt(line2, 2);
num1 *= mul1;
num2 *= mul2;
res = num1 + num2;
line1 = Integer.toString(res, 2);
System.out.println("the sum in binary is: " + line1 + "(i.e. " + res + ")");
break;
case 'S':
case 's':
System.out.print("Enter 8-bit singed binary number: ");
line1 = in.nextLine();
System.out.print("Enter another binary number: ");
line2 = in.nextLine();
mul1 = 1;
mul2 = 1;
if(line1.charAt(0) == '1'){
line1 = convert(line1);
mul1 = -1;
}
if(line2.charAt(0) == '1'){
line2 = convert(line2);
mul2 = -1;
}
num1 = Integer.parseInt(line1, 2);
num2 = Integer.parseInt(line2, 2);
num1 *= mul1;
num2 *= mul2;
res = num1 - num2;
line1 = Integer.toString(res, 2);
System.out.println("the difference in binary is: " + line1 + "(i.e. " + res + ")");
break;
default:
System.out.println("Please choose a valid option");
}
System.out.println();
}while(ch != 'E');
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.