Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

****Note: The Pad Method that was mentioned is a part of this task: Implement a

ID: 3586257 • Letter: #

Question

****Note: The Pad Method that was mentioned is a part of this task:

Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length Suggested pseudocode: carry_bit -0 sum = "" f (operand_1 is shorter than operand_2) pad operand 1 to the length of operand 2 else if (operand_2 is shorter than operand_1) pad operand 2 to the length of operand 1 for index from (length of operand 1 -1) down through 0 f (carry_bit + (value of operand_1[index]) (value of operand_2[index])0) sum"O" sum carry_bit 0 else if (carry-bit + (value of operand-1[index]) + (value of operand-2(index)-= 1) sum-“1" + sum carry_bit 0 else if (carry-bit + (value of operand-1[index]) + (value of operand-2(index)-= 2) sum "0" +sum carry_bit 1 else://a sum of 3 means write down a 1 and carry the other 1 sum = “1" carry_bit -1 + sum if (carry_bit1)://Deal with any leftover carry bit sum = "1" + sum return sum

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication7;

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class BinaryAddition {
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String binaryOne="";
String binaryTwo="";
System.out.println("Enter the first binary Number");
binaryOne=br.readLine();
System.out.println("Enter the Second binary Number");
binaryTwo=br.readLine();
//System.out.println("first number"+binaryOne+"second number"+binaryTwo);
String sum = addBinary(binaryOne,binaryTwo);
System.out.println("Sum of binary number is "+sum);
  
}catch(Exception e){
  
}
}
//Binary Addition method
static String addBinary(String b1, String b2){
System.out.println("first number : "+b1+" second number : "+b2);
//String shorter;
//String longer;
int bin1_len=b1.length();
int bin2_len=b2.length();
  
//checking if first number less than second
if (bin1_len<bin2_len){
int j=bin2_len-bin1_len;
b1=pad(b1,j);
}
//Checking if secong less than first
else if(bin2_len<bin1_len){
int j=bin1_len-bin2_len;
  
b2=pad(b2,j);
}
  
// binary Addition
int p1 = b1.length() - 1;
int p2 = b2.length() - 1;
StringBuilder buf = new StringBuilder();
int carry = 0;
while (p1 >= 0 || p2 >= 0) {
int sum = carry;
if (p1 >= 0) {
sum += b1.charAt(p1) - '0';
p1--;
}
if (p2 >= 0) {
sum += b2.charAt(p2) - '0';
p2--;
}
carry = sum >> 1;
sum = sum & 1;
buf.append(sum == 0 ? '0' : '1');
}
if (carry > 0) {
buf.append('1');
}
buf.reverse();
return buf.toString();
  
  
}

//Method for padding string
static String pad(String binValue, int n){
StringBuilder s = new StringBuilder();
for(int i = 0;i<n;i++){
s.append('0');
}
s.append(binValue); //s now contains the 0 padded binary string
binValue=s.toString();
return binValue;
}
  
}