Question: Need help to fix code: I'm having trouble converting negative decimals into 8 bit binary numbers. for example, -122 Also, ignore the decToBin(-122). I was
Need help to fix code:
I'm having trouble converting negative decimals into 8 bit binary numbers. for example, -122
Also, ignore the decToBin(-122). I was using that to debug.

import java.util.Scanner;
import java.lang.*;
import java.io.*;
public class Lab2 {
public static void main(String[] args) {
int response;
Scanner keyboard = new Scanner(System.in);
do {
System.out.println("Enter 1 to convert a signed decimal to a binary number.");
System.out.println("Enter 2 to convert a binary number to a signed decimal.");
System.out.println("Enter any other integer to quit.");
response = keyboard.nextInt();
if (response == 1) {
System.out.println();
System.out.print("Please enter a decimal between -128 and 127: ");
int decimal = keyboard.nextInt();
System.out.print("The binary equivalent is: " + decToBin(decimal) + ".");
System.out.println();
continue;
} else if (response == 2) {
System.out.println();
System.out.print("Please enter an 8-bit binary number: ");
String binary = keyboard.next();
System.out.print("The decimal equivalent is: " + binToDec(binary) + ".");
System.out.println();
continue;
}
}
while (response == 1 || response == 2);
System.out.println();
System.out.println("Program is terminating!");
decToBin(-122);
System.exit(0);
}
// Converts decimal to Binary with the use of an array.
public static String decToBin(int x) {
String bin= "";
int container[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; // Array for binary values
int i = 7;
// Checks whether input is positive or negative.
if (x >= 0) {
// If positive convert binary directly
while (x > 0) {
container[i] = x % 2;
i--;
x = x / 2;
}
}
// If negative, we use 2's complement method
else if (x
int n = Math.abs(x);
while (n > 0) {
container[i] = n % 2;
i--;
n = n / 2;
}
for (int j = 0; j
if (container[j] == 0) {
container[j] = 1;
} else if (container[j] == 1) {
container[j] = 0;
}
}
if (container[7] == 0) {
container[7] = 1;
} else if(container[7] == 1){
container[7] = 0;
}
if (container[7] == 0 && container[6] ==1) {
container[6] =0;
}
}
else if (container[7] == 0 && container[6] ==1 && container[5] ==1) {
container[6] =0;
container[5] =0;
}
else if (container[7] == 0 && container[6] ==1 && container[5] ==1 && container[4] ==1) {
container[6] =0;
container[5] =0;
container[4] =0;
}
else if (container[7] == 0 && container[6] ==1 && container[5] ==1 && container[4] ==1 && container[3] ==0 ) {
container[6] =0;
container[5] =0;
container[4] =0;
container[3] =0;
}
else if (container[7] == 0 && container[6] ==1 && container[5] ==1 && container[4] ==1 && container[3] ==1
&& container[2] ==1 ) {
container[6] =0;
container[5] =0;
container[4] =0;
container[3] =0;
container[2] =0;
}
else if(container[7] == 0 && container[6] ==1 && container[5] ==1 && container[4] ==1 && container[3] ==1
&& container[2] ==1 && container[1] ==1) {
container[6] =0;
container[5] =0;
container[4] =0;
container[3] =0;
container[2] =0;
container[1] =0;
}
else if (container[7] == 0 && container[6] ==1 && container[5] ==1 && container[4] ==1 && container[3] ==1
&& container[2] ==1 && container[1] ==1 && container[0] ==1) {
container[6] =0;
container[5] =0;
container[4] =0;
container[3] =0;
container[2] =0;
container[1] =0;
container[0] =0;
}
else {
container[6] =container[6];
container[5] =container[5];
container[4] =container[4];
container[3] =container[3];
container[2] =container[2];
container[1] =container[1];
container[0] =container[0];
}
// Prints output
for (int j = 7; j > -1; j--) {
bin = container[j] + bin;
}
return bin;
}
// Converts Binary to Decimal
public static int binToDec(String s) {
int sum = 0;
//String into an array
char[] data = s.toCharArray();
// Control variable to check the Negative values
int neg = 0;
if (data[0] == '1') {
neg = 1;
for (int i = 0; i
if (data[i] == '1') {
data[i] = '0';
} else if (data[i] == '0') {
data[i] = '1';
}
}
if (data[7] == '1') {
data[7] = '0';
} else if (data[7] == '0') {
data[7] = '1';
}
}
for(int i= 7; i>=0; i--) {
if(data[i]== '1')
sum= sum+ (int)Math.pow(2,7-i);
}
if (neg == 1) {
sum = (sum * -1)-2;
}
return sum;
}
}
Write an interactive Java program to a) Output an signed decimal number between -128 and 127 corresponding to an input 8-bit binary number (a string)in the two's complement system, and b) Output an 8-bit binary number (as a string)in the two's complement system corresponding to an input signed decimal number between -128 and 127 Your program must be driven by a menu that gives the user three choices: 1. Convert decimal to binary 2. Convert binary to decimal 3. Quit. should continue to run until the user decides stated in (a) and (b) above a series of comments specifying: t write Java methods to perform the to quit. You mus ram should be well documented and in particular e, courset, section, semester and other releva information. prief description of what the program does. strate to the instructor or the lab assistant th Demon rogram runs correctly. After d print out two cgnie
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
