Write a Java program that creates an array data of size 8, representing a binary number of 8 bits. The user must fill/enter the elements of this array only by inputting 0 or 1. If the user enters any different number, the program must stop and display "Invalid data" message. If all entered digits are valid binary digits, the program must calculate and output the special decimal number represented by this binary number as shown in the below example: If the array data is 1 1 1 1 X 23 + O X 26 + + 0 + 0 X 2' + O X 27 1 X 22 + + 4 + 8 + 0 + Result = 1 X 2°+ O X 24 + O X 25 + Result = 1 13 Then the result is calculated by multiplying the value of the data array at each index with the result of 2index then adding all the answers. (shown in the above example) Sample run1: Enter the digits consisting of (0s or 1s): 2 5 4 1 3 2 5 7 Invalid data Sample run2: Enter the digits consisting of (0s or 1s): 1 1 1 1 0 0 0 2 Invalid data Sample run3: Enter the digits consisting of (0s or 1s): 1 1 1 1 0 0 0 0 The number in decimal is 15 Sample run4: Enter the digits consisting of (Os or 1s): 1 0 1 1 0 0 0 0 The number in decimal is 13

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter6: Arrays
Section: Chapter Questions
Problem 18RQ
icon
Related questions
Question
Please solve it by java
Write a Java program that creates an array data of size 8, representing a binary number of 8 bits.
The user must fill/enter the elements of this array only by inputting 0 or 1. If the user enters any different
number, the program must stop and display "Invalid data" message.
If all entered digits are valid binary digits, the program must calculate and output the special decimal number
represented by this binary number as shown in the below example:
If the array data is
1
1
0 X 2' +
1 X 2? +
0 X 26 +
1 X 23 +
O X 24 +
+ 0
O X 25 +
0 +
Result = 1 X 2º +
O X 27
Result =
1
+
4
8
+
13
Then the result is calculated by multiplying the value of the data array at each index with the result of 2index
then adding all the answers. (shown in the above example)
Sample run1:
Enter the digits consisting of (0s or 1s): 2 5 4 1 3 2 5 7
Invalid data
Sample run2:
Enter the digits consisting of (Os or 1s): 1 1 1 1 0 0 0 2
Invalid data
Sample run3:
Enter the digits consisting of (Os or 1s): 1 1 1 1 0 0 0 0
The number in decimal is 15
Sample run4:
Enter the digits consisting of (0s or 1s): 1 0 1 1 0 0 0 0
The number in decimal is 13
Transcribed Image Text:Write a Java program that creates an array data of size 8, representing a binary number of 8 bits. The user must fill/enter the elements of this array only by inputting 0 or 1. If the user enters any different number, the program must stop and display "Invalid data" message. If all entered digits are valid binary digits, the program must calculate and output the special decimal number represented by this binary number as shown in the below example: If the array data is 1 1 0 X 2' + 1 X 2? + 0 X 26 + 1 X 23 + O X 24 + + 0 O X 25 + 0 + Result = 1 X 2º + O X 27 Result = 1 + 4 8 + 13 Then the result is calculated by multiplying the value of the data array at each index with the result of 2index then adding all the answers. (shown in the above example) Sample run1: Enter the digits consisting of (0s or 1s): 2 5 4 1 3 2 5 7 Invalid data Sample run2: Enter the digits consisting of (Os or 1s): 1 1 1 1 0 0 0 2 Invalid data Sample run3: Enter the digits consisting of (Os or 1s): 1 1 1 1 0 0 0 0 The number in decimal is 15 Sample run4: Enter the digits consisting of (0s or 1s): 1 0 1 1 0 0 0 0 The number in decimal is 13
Expert Solution
steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT