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

write a java program tht accepts 2 binary numbs. of the same length and adds the

ID: 3625819 • Letter: W

Question

write a java program tht accepts 2 binary numbs. of the same length and adds them together. binary sum of the same input numbs. is printed as are the decimal values of the input and sum.once the program has printed the results, the program asks the user if the program is to continue to sum another set of 2 binary numbs.if yes, the program is to ask for input. if no more sums are to be computed your program is to end.

NOTE: use comments through out program. the program demonstrates the use of both conditional and loop statements. the program should work correctly with any case variations of yes or no. program should have at least one method to the input, check the input, do the addition, convert the data to decimal, and output the sums.
program cannot use any classes/methods in the api that convert or add any form of
numbers that are not decimal. program can only use methods that you write to do
binary addition.also,must have whether binary number is illegal character and have the user to reenter.

Explanation / Answer

import java.util.Scanner; class BinaryAdder { //string(binary) representations String operand1; String operand2; String result; //integer(decimal) representations int op1; int op2; int sum; //scanner to get input Scanner kbd; /** * default constructor */ public BinaryAdder() { kbd = new Scanner(System.in); operand1 = "01"; operand2 = "2"; result = new String(""); } /** * prompts user for input */ public void getOperands() { System.out.println("Please input two binary numbers to add: "); operand1 = kbd.next(); operand2 = kbd.next(); } /** * validates input for conditions: 1. equal length, 2. valid binary * returns true if valid, false otherwise */ public boolean validateInput() { if(operand1.length() != operand2.length()) return false; for(int i = 0; i=0; i--) { //0 + 0 = 0+carry(0 or 1) if(operand1.charAt(i) == '0' && operand2.charAt(i) == '0') { if(carry) { result = "1"+result; carry = false; } else { result = "0"+result; carry = false; } } //if 0+1, 1+0 = 1 + carry(1 or 0) if((operand1.charAt(i) == '1' && operand2.charAt(i) == '0') || (operand1.charAt(i) == '0' && operand2.charAt(i) == '1')) { if(carry) { result = "0"+result; carry = true; } else { result = "1"+result; carry = false; } } // for the 1+1 = 10 + carry(1 or 0) case if(operand1.charAt(i) == '1' && operand2.charAt(i) == '1') { if(carry) { result = "1"+result; carry = true; } else { result = "0"+result; carry = true; } } } //any remainding carries if(carry) { result="1"+result; } } /** * converts all operands and summations from string form to decimal */ public void convertToDecimal() { op1 = binaryToDecimal(operand1); op2 = binaryToDecimal(operand2); sum = binaryToDecimal(result); } /** * takes a binary representation and converts it into a int * @param string * @return int */ private int binaryToDecimal(String binary) { int decimal = 0; for(int i = 0; i