My code is a class for converting decimal to binary. it will display the binary, one's complement and two's complement. So far I have figured out the one's complement but having a problem finding a solution for the two's complement. my plan was to use the output of the ones complement as an input to the two's complement method and convert there. Is this the right approach?
//This method will convert binary result to ones complement
public String do1sComp(){
if (binaryValue.length() == 0)
doBinary();
//Ones Complement
char[] digits = binaryValue.toCharArray();
for (char digit : digits){
if(digit == '0'){
onesComp += "1";
}
else
onesComp += "0";
}
return onesComp;
}
/* This was my attempt to start the method
public String do2sComp(){
if (onesComp.length() == 0){
if (binaryValue.length() == 0)
doBinary();
do1sComp();
}
//Twos Complement
} */
}