(It would probably help to do Programming Project 4 before doing this one.) Write a program that converts numbers from base-2 notation to base-10 (ordinary decimal) notation. The program uses Swing to perform input and output via a window interface. The user enters a base-2 number in one text field and clicks a Convert button. The equivalent base-10 number then appears in another text field. Be sure that the two text fields are labeled. Include a Clear button that clears both text fields when clicked. Also, be sure that the close-window button works correctly. Hint: Include a private method that converts the string for a base-2 number to an equivalent int value.

Positional weighting is used to convert from binary to decimal; the binary string is processed one character at a time and the weight for that position added to a running total until all digits have been processed, so the final value is the decimal equivalent of the binary number. Just as with the conversion from decimal to binary in the previous Project, a slight complication arises from the order of the bits. The first binary digit is the most significant digit, so that weight must be determined first and the weight of each successive digit must be reduced by a factor of 2.

See the code in BinaryToDecimal.java. Uses WindowDestroyer.java.

Computer Science & Information Technology

You might also like to view...

Which function would be the most useful for determining if a certain word is contained in a string representing a sentence?

a) strcspn b) strchr c) strstr d) strrchr

Computer Science & Information Technology

Why is using named constants a good programming practice?

What will be an ideal response?

Computer Science & Information Technology