Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java algorithm write a program that reads 4 digit binary number from the keyboar

ID: 3780202 • Letter: J

Question

java algorithm

write a program that reads 4 digit binary number from the keyboard as a and then converts it into decimal. for e.g., if the input is 1100, the output should be 12. Break the string into substrings and then 132 convert each substring to a value for a single bit. If the bits are b0 , b1 , b2 , and b3 , the decimal equivalent is 8b0 + 4b1 + 2b2 + b3 .)

Chapter Number and Problem Number, Page Number
Problem Description (you may copy the text from the book):    
GIVEN (Based on your analysis, answer the question: “Is any data given to me?”:  
INPUT (Is the user required to enter data?):  
LOGIC (Does anything need to happen with the given data or the input data?):  
OUTPUT (What needs to be displayed back on the screen?) :  
ALGORITHM:
(use MS Word Paragraph indent buttons to indent. Hitting tab will create a new row in the table)   1)     
a)     
i)     
2)     
a)     
i)     
3)     
a)  

i)     

Explanation / Answer

package binarytodec;

import java.util.Scanner;

public class BTD {
public int F_BTD(int binary){

int dec = 0;
int pwr = 0;
while(true){
if(binary == 0){
break;
} else {
int tmp = binary%10;
dec += tmp*Math.pow(2, pwr);
binary = binary/10;
pwr++;
}
}
System.out.println("The binary number converted to decimal is:"+dec);
return dec;
}
public static boolean isBinary(int number) {
int temp = number;
while (temp != 0) {
if (temp % 10 > 1) {
System.out.println("Not a binary number");
return false;
}
temp = temp / 10;

}
return true; }


public static void main(String[] args) {
// TODO code application logic here
Scanner scn=new Scanner(System.in);
int num;
System.out.println("Please enter the number:");
num=scn.nextInt();
BTD b1=new BTD();
if(b1.isBinary(num)==true){
b1.F_BTD(num);
  
}
  
}
  
}

*************************output***********************************************

Please enter the number:
10101
The binary number converted to decimal is:21