-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise2122.java
More file actions
60 lines (46 loc) · 1.14 KB
/
Copy pathExercise2122.java
File metadata and controls
60 lines (46 loc) · 1.14 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
public class Exercise2122
{
public static Boolean any(Boolean[] a){
int count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i]){
count++;
}
}
return count > 0;
}
public static Boolean all(Boolean[] a){
int count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == true){
count++;
}
}
return count == a.length;
}
public static Boolean any2(Boolean[] a){
for (int i = 0; i < a.length; i++) {
if (a[i] == true){
return true;
}
}
return false;
}
public static Boolean all2(Boolean[] a){
boolean allTrueSoFar = true;
for (int i = 0; i < a.length; i++) {
allTrueSoFar = allTrueSoFar && (a[i] == true);
}
return allTrueSoFar;
}
public static void main(String[] args)
{
Boolean[] arrayOfBooleans = {false, true, true, true};
//call method to test for at least item one true:
Boolean atLeastOneTrue = any(arrayOfBooleans);
//call method for all of the items are true:
Boolean allAreTrue = all(arrayOfBooleans);
System.out.println(atLeastOneTrue);
System.out.println(allAreTrue);
}
}