answersLogoWhite

0


Best Answer

public class Exercise6{

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("Example: " + addBinaryNumbers("100", "111")); // Result = 1011

// Test Cases :

// Valid Equivalence Class

// Since the inputs are valid, there will be no errors

assert (addBinaryNumbers("1", "1").equals("10") ) : "Valid Case 1: Error";

assert (addBinaryNumbers("10", "10").equals("100") ) : "Valid Case 2: Error";

assert (addBinaryNumbers("111", "111").equals("1110") ) : "Valid Case 3: Error";

assert (addBinaryNumbers("1110", "1011").equals("11001") ) : "Valid Case 4: Error";

// Invalid Equivalence Class

// Since the inputs are invalid, there will be errors

assert (addBinaryNumbers("2", "3").equals("101") ) : "Invalid Case 1: Error";

}

public static String addBinaryNumbers(String n1, String n2) {

String result = "";

for(int i = 0; i < n1.length(); i++) {

if ( (n1.charAt(i) != '0') && (n1.charAt(i) != '1') ) {

System.out.println("First number is invalid");

}

}

for(int i = 0; i < n2.length(); i++) {

if ( (n2.charAt(i) != '0') && (n2.charAt(i) != '1') ) {

System.out.println("Second number is invalid");

}

}

int number1 = Integer.parseInt(n1, 2);

int number2 = Integer.parseInt(n2, 2);

int resultNumber = number1 + number2;

result = Integer.toBinaryString(resultNumber);

return result;

}

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you add two binary numbers using java code?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you script a RuneScape bot?

RuneScape bots use Java. Use a Java Binary Code.


Why does java use unicode?

Transform character s into numbers (binary)


What kind of file contain java source code?

'.java' files contain java source code. One can access these files on windows by using 'notepad'.


How do you convert numbers into words using java?

Using toString() method


Java source code for median filter algorithm?

research is going on to develop a source code for median filtering using java


How do you make menu using jcreator in java?

Currency Source Code Using JCreator


How can random numbers be generated in Java?

Random numbers can be generated in Java using the "random" class. One needs a single "random" object to generate a series of random numbers as a unit.


How do people make minecraft mods?

They program them using code and Java.


How do you create a login page by using swing in Java?

In order to create a log in page using swing in Java one must write programming code. The code to do this is about two pages long. If one is not familiar with programming code, it would be best to hire a programmer to do this.


Are binary strings used in Java?

No if there was then java wouldn't have over 4 billion down loads


Write a java code to draw a circle inside in an ellipse?

write a program draw circle and ellipse by using oval methods in java


Where can one find out how to implement threads using Java?

You can find out how to implement threads using Java through Stacker Overflow, Java Script Source, Java Code Geeks, Free Programming Resources and other websites. There are also tutorials on college sites as well as Youtube.