hi I was wondering if I could get help with this program. I have the program jus
ID: 3664820 • Letter: H
Question
hi I was wondering if I could get help with this program. I have the program just about fully written. just at the last method where It converts from a binary number into a decimal it does not always add up. it works fine when i try to do the positive numbers. But some of the negative numbers that i try inputting comes up with the wrong answer. For example 10000000 comes up as 128, not -128 and 11111111 comes up as -127. if you could help with it at all. most of the program should be good up to that point but maybe not? I think it is something to do with the binary to decimal method. thanks in advance
/*
* Description: Simple program to convert decimal to binary
* and binary to decimal
*/
import java.util.*;
import java.lang.Math;
public class lab1 {
public static void main (String [] args){
Scanner keyIn = new Scanner(System.in);
int response;
// menu options to choose how to run the program and continue running the
// program until user decides to quit.
do{
System.out.println("Enter 1 to convert decimal to binary");
System.out.println("Enter 2 to convert binary to decimal");
System.out.println("Enter 3 to quit");
System.out.print("Please enter your choice: ");
response = keyIn.nextInt();
//choosing 1 uses case 1 for decimal conversion and 2 for binary conversion
switch(response)
{case 1:
System.out.println("");
System.out.print("Enter a whole number between -128 and +127: ");
int decimal = keyIn.nextInt();
while(decimal < -128 || decimal > 127)
{
System.out.print("Enter a whole number between -128 and +127: ");
decimal = keyIn.nextInt();
}
System.out.println("");
System.out.print(decimal + " converted into a binary is: ");
decConvert(decimal);
System.out.println("");
break;
case 2:
System.out.println("");
System.out.println("Enter a 8-bit binary number:");
String a = keyIn.next();
System.out.println("");
System.out.println(a + " converted into a decimal is: ");
int i = binconvert(a);
System.out.println(i);
break;
default:
break;
}
System.out.println("");
}while(response!=3);
System.out.println("The program has ended");
}
//method to convert from decimal to binary
public static void decConvert(int num){
String out = "";
if(num >= 0)
{
while(num > 0)
{
out += num % 2;
num = num/2;
}
int length = out.length();
while(length < 8)
{
out += 0;
length++;
}
String temp = "";
int y = 7;
//Start from right of string moving to left
while(y > -1){
temp += out.charAt(y);
y--;
}
System.out.print(temp);
}
else
{ num += 1;
num *= -1;
// flip the numbers
while(num > 0){
out += num % 2;
num = num/2;
}
int length = out.length();
while(length < 8){
out += 0;
length++;
}
String temp = "";
int y = 7;
while(y > -1){
temp += out.charAt(y);
y--;
}
out = temp;
int bin [] = new int [8];
//do{
for(int i = 0;i < 8; i++){
bin [i]= out.charAt(i)-48;
if(bin[i] == 0)
bin[i]= 1;
else
bin [i] = 0;
System.out.print(bin[i]);}
//Start from right of string moving to left
}
}
//method to convert from binary to decimal
private static int binconvert(String a)
{
double j = 0;
for(int i = 0; i<a.length(); i++)
{
if(a.charAt(i)== '1')
j=j+ Math.pow(2, a.length()-1-i);
}
if(j > 128)
j = 128 - j;
else if(j < 0)
j =-128;
return (int) j;
}
}
Explanation / Answer
You can modify the method as below:
//method to convert from binary to decimal
private static int binconvert(String a)
{
int binaryNum = Integer.parseInt(a);
int decimal = 0;
int i = 0;
while(true){
if(binaryNum == 0){
break;
} else {
int temp = binaryNum%10;
decimal += temp*Math.pow(2, i);
binaryNum = binaryNum/10;
i++;
}
}
decimal = (decimal + 128)%256 - 128; //this is the key line which change 128 to -128
return decimal;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.