forked from ironhack-labs/lab-java-loops-and-version-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL104.java
More file actions
87 lines (52 loc) · 2.28 KB
/
Copy pathL104.java
File metadata and controls
87 lines (52 loc) · 2.28 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Arrays;
public class L104 {
public static void main(String[] args) {
int[] arrayLab104 = {13, 5, 756, 19, 11, 13, 151, 9};
System.out.println("Task 1");
int difference = checkDiff(arrayLab104);
System.out.println("The difference between the largest and the smallest number is: " + difference);
//System.out.println("The difference between the smallest and the largest number is: " + checkDiff(arrayLab104));
System.out.println("\nTask 2");
smallestSecondSmallest(arrayLab104);
System.out.println("\nTask 3");
double result = mathCheck();
System.out.println("Result for task 3 is: " + result);
}
public static int checkDiff(int[] arrayLab104) {
//System.out.println("The array to check is: " + Arrays.toString(arrayLab104) +"\n");
int smallestN = arrayLab104[0];
int largestN = arrayLab104[0];
for (int i = 1; i < arrayLab104.length; i++) {
if (arrayLab104[i] < smallestN) {
smallestN = arrayLab104[i];
}
}
for (int i = 1; i < arrayLab104.length; i++) {
if (arrayLab104[i] > largestN) {
largestN = arrayLab104[i];
}
}
int diffN = largestN - smallestN;
/*System.out.println("the smallest number: " + smallestN);
System.out.println("the largest number: " + largestN);
System.out.println("The difference between largest and smallest: " + diffN);
System.out.println("End of module\n");*/
return diffN;
}
public static void smallestSecondSmallest(int[] arrayLab104) {
Arrays.sort(arrayLab104);
// System.out.println("sorted array is: " + Arrays.toString(arrayLab104)); This checks that the array is sorted
System.out.println("\nThe smallest number is: " + arrayLab104[0]);
System.out.println("The second smallest number is: " + arrayLab104[1]);
//System.out.println("end of method\n");
}
public static double mathCheck() {
// BIDMAS order
double x = 8;
double y = 6;
//1 bracket
double res = Math.pow(x, 2) + Math.pow(((4 * y / 5) - x), 2);
//System.out.println(res);
return res;
}
}