-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
47 lines (28 loc) · 1.74 KB
/
Copy pathSolution.java
File metadata and controls
47 lines (28 loc) · 1.74 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
//And if any man shall take away from the words of the book of this prophecy, God shall take away his part out of the book of life, and out of the holy city, and from the things which are written in this book. (Revelation 22:19)
package com.javarush.task.task29.task2904;
/*
Особенности автобоксинга
*/
public class Solution {
private Integer[] array = new Integer[]{1, 2, 3, 4};
public static void main(String[] args) {
Number value1 = new Solution().getValueByIndex(5); //-1.0, class java.lang.Double expected
Number value2 = new Solution().getValueByIndex(2); //3, class java.lang.Integer expected
System.out.println(value1 + ", " + value1.getClass().toString());
System.out.println(value2 + ", " + value2.getClass().toString());
}
Number getValueByIndex(int index) {
if (index >= 0 && index < array.length) {return array[index];}
else {return new Double(-1);}
}
}
/*
Особенности автобоксинга
Исправь ошибку в методе getValueByIndex.
Читай доп. статью про особенности автобоксинга.
Требования:
1. Метод getValueByIndex должен возвращать объект типа Integer из массива array, если элемент с индексом index есть в массиве.
2. Метод getValueByIndex должен возвращать объект типа Double, равный -1, если в массиве array нет элемента с индексом index.
3. Метод main не изменять.
4. Программа должна вывести две строки: "-1.0, class java.lang.Double" и "3, class java.lang.Integer".
*/