Newer
Older
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package fi.utu.tech;
import java.util.List;
import java.util.Scanner;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
interface neliöi {
int neliö(int x);
}
public class Main {
/**
* Main class.
* @param args Command line arguments
*/
public static void main(String[] args) throws Exception {
tehtA();
tehtB();
tehtC();
tehtD();
System.out.print("Neliöitävä luku: ");
var luku = new Scanner(System.in).nextInt();
tehtE(luku);
}
/**
*
* Teht a)
*
* @.pre true
* @.post list(FOREACH(System.out.print())
*/
public static void tehtA() {
var lista1 = List.of(1, 2, 3);
var lista2 = List.of(1, 2, 3);
var virta1 = lista1.stream();
var virta2 = lista2.stream();
var temp = Stream.concat(virta1, virta2);
var ans = temp.collect(Collectors.toList());
for (int i : ans) System.out.print(i);
System.out.print("\n" + ans);
}
/**
* Teht b)
*
* @.pre true
* @.post print(list(FOREACH:index * 2 && index != 4))
*/
public static void tehtB() {
var lista1 = List.of(1, 2, 3);
var lista2 = List.of(1, 2, 3);
var virta1 = lista1.stream();
var virta2 = lista2.stream();
var temp = Stream.concat(virta1, virta2);
var temp1 = temp.collect(Collectors.toList());
var ans = temp1.stream()
.map(i -> i*2)
.filter(i -> i!=4);
var lista = ans.collect(Collectors.toList());
System.out.println(lista);
}
/**
* Teht c)
*
* @.pre true
* @.post print(list: maxValue)
*/
public static void tehtC(){
var lista1 = List.of(1, 2, 3);
var lista2 = List.of(1, 2, 3);
var virta1 = lista1.stream();
var virta2 = lista2.stream();
var temp = Stream.concat(virta1, virta2);
var temp1 = temp.collect(Collectors.toList());
int max = temp1.stream()
.max(Integer::compareTo).get();
System.out.println(max);
}
/**
* Teht d)
*
* @.pre
* @.post print(list(index) + "\n"))
*/
public static void tehtD(){
var lista1 = List.of(1, 2, 3);
var lista2 = List.of(1, 2, 3);
var virta1 = lista1.stream();
var virta2 = lista2.stream();
var temp = Stream.concat(virta1, virta2);
var temp1 = temp.collect(Collectors.toList());
temp1.forEach(System.out::println);
}
/**
*
* @param luku
* @.pre luku!=null
* @.post true
*
* @.pre param > 0
* @.post print(param*param) (squared)
*
*
* @throws Exception
*/
public static void tehtE(int luku)throws Exception{
tehtE e = new tehtE();
neliöi n = (int x) -> x*x;
anon a = new anon();
Function<Integer, Integer> neliöijä = x -> x*x;
int ans = n.neliö(luku);
//ilmeisesti teht f??
try {
System.out.println(e.neliöi(luku));
} catch (Exception exception){
throw new Exception("ei skulaa nyt koska " + exception.getMessage());
}
try{
System.out.println(luku + " * " + luku + " = " + neliöijä.apply(luku));
} catch (Exception exception){
throw new Exception("Homma ei rokkaa koska " + exception.getMessage());
}
try{
System.out.println(ans);
}catch (Exception exception){
throw new Exception("Ei ihan menny putkeen koska " + exception.getMessage());
}
try{
System.out.println(a.neliöi(luku));
}catch (Exception exception){
throw new Exception("Ei toimi koska " + exception.getMessage());
}
}
}
/**
* Anonymous class in main-class to return square of user input value
*
* @.pre luku != null
* @.post return luku * luku
*
*/
class anon implements neliöijä{
public int neliöi(int luku){
Function<Integer, Integer> neliöi = x -> luku*luku;
int ans = neliöi.apply(luku);
return ans;
}
}