-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment6.java
70 lines (62 loc) · 1.34 KB
/
Assignment6.java
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
61
62
63
64
65
66
67
68
69
70
public class NumStatsArray{
private final double[] arr;
public NumStatsArray(double[] arr){
this.arr = arr;
}
public String toString(){
String str = "{";
for (int i = 0; i < arr.length; i++) {
double item = arr[i];
if (i != arr.length-1) {
str += item + ", ";
} else {
str += item;
}
}
str += "}";
return str;
}
public double average(){
double sum = 0;
for (double item : arr) {
sum = sum + item;
}
return sum / arr.length;
}
public double range(){
double min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (min > arr[i]) {
min = arr[i];
}
}
double max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return (max - min);
}
public int sortStatus() {
int increase = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] >= arr[i - 1]) {
increase += 1;
}
}
int decrease = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] <= arr[i - 1]) {
decrease += 1;
}
}
if (increase == arr.length - 1) {
return 1;
} else if (decrease == arr.length - 1) {
return -1;
} else {
return 0;
}
}
}