forked from ironhack-labs/lab-java-loops-and-version-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
55 lines (41 loc) · 1.57 KB
/
Main.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
import java.sql.SQLOutput;
public class Main {
public static void main(String[] args) {
int[] numberArray = {3, 6, 5, 7, 9};
int min = numberArray[0];
int max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) {
if (min > numberArray[i]) {
min = numberArray[i];
} else if (max < numberArray[i]) {
max = numberArray[i];
}
}
int difference = max - min;
System.out.println(difference);
//Second Task
int[] smallestArray = {3,9,5,6,1,4};
findSmallest(smallestArray);
double xValue = 4.0;
double yValue = 2.0;
double finalResult = evaluateExpression(xValue, yValue);
System.out.println("Result: " + finalResult);
}
public static void findSmallest(int[] numbers) {
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for (int number : numbers) {
if(number < smallest){
secondSmallest = smallest;
smallest = number;
} else if ( number < secondSmallest && number != smallest) {
secondSmallest=number;
}
}
System.out.println("Smallest number: " + smallest);
System.out.println("Second Smallest Number:" + secondSmallest);
}
public static double evaluateExpression(double x, double y) {
return Math.pow(x, 2) + Math.pow((4 * y / 5 - x), 2);
}
}