Skip to content

Commit 8c4c333

Browse files
committed
Oracle Java8 MOOC Lesson Solutions
1 parent ef748ec commit 8c4c333

File tree

11 files changed

+236536
-2
lines changed

11 files changed

+236536
-2
lines changed
397 KB
Binary file not shown.
399 KB
Binary file not shown.
385 KB
Binary file not shown.

oracle-mooc-java8/java8-mooc-homeworks/pom.xml renamed to oracle-mooc-java8/homeworks/solutions/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10-
<maven.compiler.source>1.7</maven.compiler.source>
11-
<maven.compiler.target>1.7</maven.compiler.target>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
1212
</properties>
13+
<name>solutions</name>
1314
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package org.ecabrerar.examples.java8.mooc.lesson1;
2+
3+
4+
/**
5+
* Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
6+
*
7+
* JDK 8 MOOC Lesson 1 homework
8+
*/
9+
//package lesson1;
10+
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.TreeMap;
16+
17+
/**
18+
* @author Speakjava ([email protected])
19+
*/
20+
public class SolutionsLesson1 {
21+
22+
/**
23+
* Run the exercises to ensure we got the right answers
24+
*/
25+
public void runExercises() {
26+
System.out.println("JDK 8 Lambdas and Streams MOOC Lesson 1");
27+
System.out.println("Running exercise 1 solution...");
28+
exercise1();
29+
System.out.println("Running exercise 2 solution...");
30+
exercise2();
31+
System.out.println("Running exercise 3 solution...");
32+
exercise3();
33+
System.out.println("Running exercise 4 solution...");
34+
exercise4();
35+
System.out.println("Running exercise 5 solution...");
36+
exercise5();
37+
}
38+
39+
/**
40+
* All exercises should be completed using Lambda expressions and the new
41+
* methods added to JDK 8 where appropriate. There is no need to use an
42+
* explicit loop in any of the code. Use method references rather than full
43+
* lambda expressions wherever possible.
44+
*/
45+
/**
46+
* Exercise 1
47+
*
48+
* Create a string that consists of the first letter of each word in the
49+
* list of Strings provided.
50+
*/
51+
private void exercise1() {
52+
List<String> list = Arrays.asList(
53+
"alpha", "bravo", "charlie", "delta", "echo", "foxtrot");
54+
55+
/* YOUR CODE HERE */
56+
final StringBuilder sb = new StringBuilder();
57+
// list.forEach((String t) -> {
58+
// sb.append(t.charAt(0));
59+
// });
60+
61+
list.forEach(s -> sb.append(s.charAt(0)));
62+
63+
String result = sb.toString();
64+
65+
System.out.println("Exercise 1 result = " + result);
66+
}
67+
68+
/**
69+
* Exercise 2
70+
*
71+
* Remove the words that have odd lengths from the list.
72+
*/
73+
private void exercise2() {
74+
List<String> list = new ArrayList<>(Arrays.asList(
75+
"alpha", "bravo", "charlie", "delta", "echo", "foxtrot"));
76+
77+
/* YOUR CODE HERE */
78+
// list.removeIf((String t) -> t.length()% 2 > 0);
79+
list.removeIf(s -> s.length()% 2 > 0);
80+
list.forEach(System.out::println);
81+
82+
}
83+
84+
/**
85+
* Exercise 3
86+
*
87+
* Replace every word in the list with its upper case equivalent.
88+
*/
89+
private void exercise3() {
90+
List<String> list = new ArrayList<>(Arrays.asList(
91+
"alpha", "bravo", "charlie", "delta", "echo", "foxtrot"));
92+
93+
/* YOUR CODE HERE */
94+
list.replaceAll(String::toUpperCase);
95+
list.stream().forEach(System.out::println);
96+
}
97+
98+
/**
99+
* Exercise 4
100+
*
101+
* Convert every key-value pair of the map into a string and append them all
102+
* into a single string, in iteration order.
103+
*/
104+
private void exercise4() {
105+
Map<String, Integer> map = new TreeMap<>();
106+
map.put("c", 3);
107+
map.put("b", 2);
108+
map.put("a", 1);
109+
110+
/* YOUR CODE HERE */
111+
final StringBuilder sb = new StringBuilder();
112+
113+
// map.forEach((String k, Integer v) -> {
114+
// sb.append(k);
115+
// });
116+
117+
map.forEach((k,v) -> sb.append(String.format("%s%s", k,v)));
118+
//System.out.println(result.toString());
119+
120+
String result = sb.toString();
121+
System.out.println("Exercise 4 result = " + result);
122+
}
123+
124+
/**
125+
* Exercise 5
126+
*
127+
* Create a new thread that prints the numbers from the list.
128+
*/
129+
private void exercise5() {
130+
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
131+
132+
/* YOUR CODE HERE */
133+
new Thread(()-> list.forEach(System.out::println)).start();
134+
}
135+
136+
/**
137+
* Main entry point for application
138+
*
139+
* @param args the command line arguments
140+
*/
141+
public static void main(String[] args) {
142+
SolutionsLesson1 lesson = new SolutionsLesson1();
143+
lesson.runExercises();
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package org.ecabrerar.examples.java8.mooc.lesson2;
2+
3+
/**
4+
* Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* JDK 8 MOOC Lesson 2 homework
7+
*/
8+
//package lesson2;
9+
10+
import java.io.BufferedReader;
11+
import java.io.IOException;
12+
import java.nio.charset.StandardCharsets;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
import java.util.stream.Collectors;
18+
import java.util.stream.Stream;
19+
20+
/**
21+
* @author Speakjava ([email protected])
22+
*/
23+
public class SolutionsLesson2 {
24+
private static final String WORD_REGEXP = "[- .:,]+";
25+
26+
/**
27+
* Run the exercises to ensure we got the right answers
28+
*
29+
* @throws java.io.IOException
30+
*/
31+
public void runExercises() throws IOException {
32+
System.out.println("JDK 8 Lambdas and Streams MOOC Lesson 2");
33+
System.out.println("Running exercise 1 solution...");
34+
exercise1();
35+
System.out.println("Running exercise 2 solution...");
36+
exercise2();
37+
System.out.println("Running exercise 3 solution...");
38+
exercise3();
39+
System.out.println("Running exercise 4 solution...");
40+
exercise4();
41+
System.out.println("Running exercise 5 solution...");
42+
exercise5();
43+
System.out.println("Running exercise 6 solution...");
44+
exercise6();
45+
System.out.println("Running exercise 7 solution...");
46+
exercise7();
47+
}
48+
49+
/**
50+
* Exercise 1
51+
*
52+
* Create a new list with all the strings from original list converted to
53+
* lower case and print them out.
54+
*/
55+
private void exercise1() {
56+
List<String> list = Arrays.asList(
57+
"The", "Quick", "BROWN", "Fox", "Jumped", "Over", "The", "LAZY", "DOG");
58+
59+
/* YOUR CODE HERE */
60+
61+
List<String> newList = list
62+
.stream()
63+
.map(String::toLowerCase)
64+
.collect(Collectors.toList());
65+
66+
67+
newList.forEach(System.out::println);
68+
69+
70+
71+
}
72+
73+
/**
74+
* Exercise 2
75+
*
76+
* Modify exercise 1 so that the new list only contains strings that have an
77+
* odd length
78+
*/
79+
private void exercise2() {
80+
List<String> list = Arrays.asList(
81+
"The", "Quick", "BROWN", "Fox", "Jumped", "Over", "The", "LAZY", "DOG");
82+
83+
84+
List<String> newList = list
85+
.stream()
86+
.filter(s -> (s.length() % 2)==1)
87+
.map(String::toLowerCase)
88+
.collect(Collectors.toList());
89+
90+
91+
newList.forEach(System.out::println);
92+
93+
/* YOUR CODE HERE */
94+
}
95+
96+
/**
97+
* Exercise 3
98+
*
99+
* Join the second, third and forth strings of the list into a single string,
100+
* where each word is separated by a hyphen (-). Print the resulting string.
101+
*/
102+
private void exercise3() {
103+
List<String> list = Arrays.asList(
104+
"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog");
105+
106+
/* YOUR CODE HERE */
107+
String result = list
108+
.stream()
109+
.skip(1)
110+
.limit(3)
111+
.collect(Collectors.joining("-"));
112+
113+
System.out.println(result);
114+
115+
116+
}
117+
118+
/**
119+
* Count the number of lines in the file using the BufferedReader provided
120+
*/
121+
private void exercise4() throws IOException {
122+
try (BufferedReader reader = Files.newBufferedReader(
123+
Paths.get("SonnetI.txt"), StandardCharsets.UTF_8)) {
124+
/* YOUR CODE HERE */
125+
126+
long totalLines = reader.lines().count();
127+
128+
System.out.println("Number of lines = " + totalLines);
129+
}
130+
}
131+
132+
/**
133+
* Using the BufferedReader to access the file, create a list of words with
134+
* no duplicates contained in the file. Print the words.
135+
*
136+
* HINT: A regular expression, WORD_REGEXP, is already defined for your use.
137+
*/
138+
private void exercise5() throws IOException {
139+
try (BufferedReader reader = Files.newBufferedReader(
140+
Paths.get("SonnetI.txt"), StandardCharsets.UTF_8)) {
141+
/* YOUR CODE HERE */
142+
143+
List<String> uniqueWords = reader
144+
.lines()
145+
.flatMap(line -> Stream.of(line.split(WORD_REGEXP)))
146+
.distinct()
147+
.collect(Collectors.toList());
148+
149+
uniqueWords.stream().forEach(System.out::println);
150+
}
151+
}
152+
153+
/**
154+
* Using the BufferedReader to access the file create a list of words from
155+
* the file, converted to lower-case and with duplicates removed, which is
156+
* sorted by natural order. Print the contents of the list.
157+
*/
158+
private void exercise6() throws IOException {
159+
try (BufferedReader reader = Files.newBufferedReader(
160+
Paths.get("SonnetI.txt"), StandardCharsets.UTF_8)) {
161+
/* YOUR CODE HERE */
162+
163+
List<String> words = reader.lines()
164+
.flatMap(line -> Stream.of(line.split(WORD_REGEXP)))
165+
.map(String::toLowerCase)
166+
.sorted()
167+
.distinct()
168+
.collect(Collectors.toList());
169+
170+
words.stream().forEach(System.out::println);
171+
}
172+
}
173+
174+
/**
175+
* Modify exercise6 so that the words are sorted by length
176+
*/
177+
private void exercise7() throws IOException {
178+
try (BufferedReader reader = Files.newBufferedReader(
179+
Paths.get("SonnetI.txt"), StandardCharsets.UTF_8)) {
180+
/* YOUR CODE HERE */
181+
182+
183+
List<String> words = reader.lines()
184+
.flatMap(line -> Stream.of(line.split(WORD_REGEXP)))
185+
.map(String::toLowerCase)
186+
.sorted()
187+
.distinct()
188+
.collect(Collectors.toList());
189+
190+
words.stream().forEach(System.out::println);
191+
}
192+
}
193+
194+
/**
195+
* Main entry point for application
196+
*
197+
* @param args the command line arguments
198+
* @throws IOException If file access does not work
199+
*/
200+
public static void main(String[] args) throws IOException {
201+
SolutionsLesson2 lesson = new SolutionsLesson2();
202+
lesson.runExercises();
203+
}
204+
}
205+

0 commit comments

Comments
 (0)