How to do this java program?

Create a Java program called BinaryConverter.java that asks the user to input an 4-bit binary number from the keyboard and proceed to convert the binary input to a decimal value. The program will have to separate the individual bits for display and calculation purposes (note example display below). For example, if the input is 1100, the output should be each digit displayed one per line, followed by the calculated result 12.

Hint #1: use the division and remainder operators.

Hint #2: you will breaking the input down into individual values that represent the single bits. If the bits are b0, b1, b2, and b3, the decimal equivalent is: 8(b0) + 4(b1) + 2(b2) + (b3).

Example Output:

Welcome to the Binary Converter!!

Please enter your name: Larry

Larry, please enter a 4-bit binary number to convert: 1100

converting...

1

1

0

0

I have converted your input of 1100 to the decimal value of 12.

Have a nice day Larry!

Comments

  • You just multiply each integer by 2 to its places power. You ought to be able to figure out how to do that pretty easily with the javadocs.

  • I'd take the input as string, convert it to an array with toCharArray, then if char 1 = 1, total = total + 8, if char 2 = 1, total = total + 4, etc.

    I'd also hope your teacher doesn't search and see you've asked for all your homework answers here, espec when the first few are easily found in any textbook.

Sign In or Register to comment.