Question: I need this without the .append my assignment is to Add the capability to use negative ints / subtraction using 2's compliment. my code below
I need this without the .append
my assignment is to Add the capability to use negative ints / subtraction using 2's compliment.
my code below , i feel it's a bit messy but I need it to be simple as I am new to codeing
import java.util.*; public class NegativeRevise { public static void main(String[] args) { int num1; int num2; Scanner input = new Scanner(System.in); System.out.print("Enter first Integer less than 128: "); num1 = Integer.parseInt(input.nextLine()); while (true) { if (num1 < 0) System.out.println("Number must be a positive value less than 128."); else if (num1 >= 128) System.out.println("Number must be a positive number less than 128."); else break; System.out.print(" Enter the integer value again: "); num1 = Integer.parseInt(input.nextLine()); } System.out.print("Enter second Integer less than 128: "); num2 = Integer.parseInt(input.nextLine()); while (true) { if (num2 < 0) System.out.println("Number must be a positive value less than 128."); else if (num2 >= 128) System.out.println("Number must be a positive number less than 128."); else break; System.out.print("Enter the integer value again: "); num2 = Integer.parseInt(input.nextLine()); } String binaryString1 = intToBin(num1); String binaryString2 = intToBin(num2); String answer=binaryStringAddition(binaryString1,binaryString2); System.out.println("The number after addition is: "); System.out.println(binaryString1 + "+" + binaryString2 + "=" + answer); System.out.println(getIntFromBinary(binaryString1) + "+" +getIntFromBinary(binaryString2) + "="+ getIntFromBinary(answer)); answer=binaryStringSubstraction(binaryString1,binaryString2); System.out.println("The number after substraction is: "); System.out.println(binaryString1 + "-" + binaryString2 + "=" + answer); System.out.println(getIntFromBinary(binaryString1) + "-" +getIntFromBinary(binaryString2) + "="+ getIntFromBinary(answer)); input.close(); } public static String intToBin(int x){ String binary = "00000000"; int i = 7; while (x != 0) { if (x % 2 == 0) binary = binary.substring(0, i) + "0" + binary.substring(i + 1, 8); else binary = binary.substring(0, i) + "1" + binary.substring(i + 1, 8); x = x / 2; i--; } return binary; } static String binaryStringAddition(String a, String b){ String answer = "00000000"; char[] temp = answer.toCharArray(); char carry = '0'; int i = a.length(); --i; while (i >= 0){ if (a.charAt(i) == b.charAt(i)){ if (a.charAt(i) == '1') { if (carry == '0') { temp[i] = '0'; carry = '1'; } else{ temp[i] = '1'; carry = '1'; } } else{ if (carry == '0'){ temp[i] = '0'; carry = '0'; } else{ temp[i] = '1'; carry = '0'; } } } else{ if (carry == '0'){ temp[i] = '1'; carry = '0'; } else { temp[i] = '0'; carry = '1'; } } --i; } answer = String.valueOf(temp); if (carry == '1' || answer.charAt(0) == '1'){ } else{} return answer; } static String binaryStringSubstraction(String a, String b){ String res=binaryStringAddition(a,makeNegative(b)); return res; } static String makeNegative(String b){ String newB=invertBinary(b); String one=intToBin(1); String twoCompl=binaryStringAddition(newB.toString(),one); return twoCompl; } static String invertBinary(String b){ StringBuilder newB = new StringBuilder(); for(char bChar:b.toCharArray()){ if(bChar=='1'){ newB.append('0'); } else newB.append('1'); } return newB.toString(); } public static int getIntFromBinary(String binaryInt){ if (binaryInt.charAt(0) == '1'){ String invertedInt = invertBinary(binaryInt); int decimalValue = Integer.parseInt(invertedInt, 2); decimalValue = (decimalValue + 1) * -1;
return decimalValue; } else{ return Integer.parseInt(binaryInt, 2); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
