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 pathLab2.java
More file actions
90 lines (63 loc) · 2.42 KB
/
Lab2.java
File metadata and controls
90 lines (63 loc) · 2.42 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
84
85
86
87
88
89
90
import java.util.Objects;
public class Lab2 {
/*
Task 1: Method difference
Write a Java method that returns the difference between the
largest and smallest values in an array of integers. The length of the
array must be at least 1.*/
/*
Task 2: method smallest
Write a Java method that finds the smallest and second smallest
elements of a given array and prints them to the console.
*/
/*
Task 3: method mathematical
Write a Java method that calculates the result of the following
mathematical expression, where x and y are two variables that
have been pre-set in your code:
*/
private double x=2; // variables preestablecidas para la tarea 3
private double y =3;
private double z; //variable que modifica la Task3
public Integer difference (Integer[] integerList){
Integer smallest;
Integer largest;
if (integerList.length==0) {
System.out.println("La lista está vacia");
return (-1);
}else if (integerList.length==1) {
return integerList[0];
} else {// al menos tiene dos elementos
smallest=integerList[0];
largest=integerList[1];
for (int i=2; i<integerList.length;i++) {
if (integerList[i]<smallest){
smallest=integerList[i];
}
if (integerList[i]>largest){
largest=integerList[i];
}
}// end for
} //end else-if
return largest-smallest;
} //end method difference
public String smallest (Integer[] integerList){
// Inicializados con el mayor valor posible
Integer min1 = Integer.MAX_VALUE; // El menor número
Integer min2 = Integer.MAX_VALUE; // El segundo menor número
for (Integer num : integerList){
if (num < min1) {
min2 = min1; // El antiguo mínimo pasa a ser el segundo menor
min1 = num; // Se actualiza el menor
} else if (num < min2) {
min2 = num; // Se actualiza el segundo menor
}
}
return "Los dos números menores de la lista son " +min1.toString() + " y " + min2.toString();
} //end method smallest
//method Task3
public void mathematical(){
z= Math.pow(x,2) + Math.pow( ((4*y/5)-x),2 );
}
public double getZ(){return z;}
}