-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinaryReader.java
More file actions
60 lines (54 loc) · 1.58 KB
/
BinaryReader.java
File metadata and controls
60 lines (54 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.codefortomorrow.intermediate.chapter10.solutions;
/*
Create a program called BinaryReader in which a 4 x 4
array of booleans is created. Have the computer loop
through the array, printing a 0 if the current element is
set to false and a 1 if it set to true.
Example Array:
{
{true, false, false, true},
{false, true, true, true},
{true, true, false, true},
{true, false, false, false}
}
Example Output:
1001
0111
1101
1000
*/
public class BinaryReader
{
public static void main(String[] args)
{
//Create a 2D array of booleans
boolean[][] arr =
{
{true, false, false, true},
{false, true, true, true},
{true, true, false, true},
{true, false, false, false}
};
//Loop through the array using nested loops
for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[0].length; j++)
{
//Check if current element is set to true
if(arr[i][j] == true)
{
//Print out a 1 if set to true
System.out.print(1);
}
//Otherwise, current element is set to false
else
{
//Print out a 0 if set to false
System.out.print(0);
}
}
//Move over to the next line
System.out.println();
}
}
}