-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14-Scope
More file actions
45 lines (34 loc) · 1.55 KB
/
Day14-Scope
File metadata and controls
45 lines (34 loc) · 1.55 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
Problem:
Objective
Today we're discussing scope. Check out the Tutorial tab for learning materials and an instructional video!
The absolute difference between two integers, a and b, is written as |a-b| .
The maximum absolute difference between two integers in a set of positive integers, elements, is the largest absolute difference
between any two integers in elements .
The Difference class is started for you in the editor. It has a private integer array (elements) for storing
N non-negative integers, and a public integer (maximumDifference) for storing the maximum absolute difference.
Task
Complete the Difference class by writing the following:
1. A class constructor that takes an array of integers as a parameter and saves it to the elements instance variable.
2. A computeDifference method that finds the maximum absolute difference between any 2 numbers in N and stores
it in the maximumDifference instance variable.
Solution:
public int[] getElements() {
return elements;
}
public void setElements(int[] elements) {
this.elements = elements;
}
public Difference(int[] elements) {
setElements(elements);
}
public int computeDifference() {
int [] array = Difference.this.getElements();
List<Integer> list = new LinkedList<Integer>();
for (int i=0; i<array.length; i++) {
list.add(array[i]);
}
int min = Collections.min(list);
int max = Collections.max(list);
maximumDifference = Math.abs(max-min);
return maximumDifference;
}