2. Build on programming basics, by using
the world’s most popular language to:
• Maintain your advantage
• Refine your skills
• Write functional code
2
Marius Claassen,
Java Intermediate
3. About me
Marius Claassen, Java Developer and Teacher
I am a self-taught Java developer. Having been a teacher for many
years, I am now working full-time as an independent Java instructor,
making video tutorials. It is my mission to help others learn
programming in general and Java in particular.
3
Marius Claassen,
Java Intermediate
4. Benefits
• Read Java code
• Develop useful Java applications
• Devise Java solutions when given a
problem statement
4
Marius Claassen,
Java Intermediate
5. Major components
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
5
Marius Claassen,
Java Intermediate
6. Lecture outline
• 51 Lectures
• 5 minutes
1. Coding exercise
2. Lecture description
3. PDF
4. Video
6
Marius Claassen,
Java Intermediate
7. Ideal student
* Completed Java beginner course
* You want to improve your skills by:
• watching live coding
• doing coding exercises
• checking answers against solutions
7
Marius Claassen,
Java Intermediate
9. To get details about this course:
• mariusclaassen@gmail.com
or
• https://www.udemy.com/course/1133518/manage/basics/
9
Marius Claassen,
Java Intermediate
10. TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
10
Marius Claassen,
Java Intermediate
11. Lecture 2: Development tools
• JDK (SE 8 or SE 9)
http://www.oracle.com/technetwork/java/javase/downloads/index.html
• IDE (IntelliJ IDEA)
https://www.jetbrains.com/idea/
• Internet browser (Google Chrome)
11
Marius Claassen,
Java Intermediate
49. TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
49
Marius Claassen,
Java Intermediate
50. Lecture 3: Numeric types example
Numeric types problem statement:
Print the numeric data types int and float with
their names and values
50
Marius Claassen,
Java Intermediate
51. Lecture 3: Numeric types example
public class Lecture3 {
public static void main(String[] args) {
int intNumber = 308_000;
float floatNumber = 9_000f;
System.out.print(“intNumber: ” + intNumber);
System.out.print(“tfloatNumber: ” + floatNumber);
}
} // intNumber: 308000 floatNumber: 9000.0
51
Marius Claassen,
Java Intermediate
52. Lecture 3: Numeric types exercise
Print the numeric data type long initialized as
6 000 000
52
Marius Claassen,
Java Intermediate
53. Lecture 3: Numeric types exercise
public class Lecture4 {
public static void main(String[] args) {
long longNumber = 6_000_000;
// TODO: Print the name and value of ‘longNumber’
}
}
53
Marius Claassen,
Java Intermediate
54. Lecture 4: Numeric types solution
public class Lecture4 {
public static void main(String[] args) {
long longNumber = 6_000_000;
System.out.print(“longNumber: ” + longNumber);
}
} // longNumber: 6000000
54
Marius Claassen,
Java Intermediate
55. Lecture 5: Textual type example
Textual types problem statement:
Implement the textual data type ‘String’ and
print its name and value
55
Marius Claassen,
Java Intermediate
56. Lecture 5: Textual type example
public class Lecture5 {
public static void main(String[] args) {
String string = “Java is everywhere.”;
System.out.print(“Text: ” + string);
}
} // Text: Java is everywhere.
56
Marius Claassen,
Java Intermediate
57. Lecture 5: Textual type exercise
Print the textual data type ‘String’ initialized
as ‘Java continues to dominate.’
57
Marius Claassen,
Java Intermediate
58. Lecture 5: Textual type exercise
public class Lecture6 {
public static void main(String[] args) {
String myString = “Java continues to dominate.”;
// TODO: Print the name and value of ‘myString’
}
}
58
Marius Claassen,
Java Intermediate
59. Lecture 6: Textual type solution
public class Lecture6 {
public static void main(String[] args) {
String myString = “Java continues to dominate.”;
System.out.print(“myString: ” + myString);
}
} // myString: Java continues to dominate.
59
Marius Claassen,
Java Intermediate
60. Lecture 7: Convert types example
Converting types problem statement:
Convert a String to a float and a double to a
String
60
Marius Claassen,
Java Intermediate
61. Lecture 7: Convert types example
public class Lecture7 {
public static void main(String[] args) {
String stringText = “7.7”;
float floatNumber = Float.parseFloat(stringText);
System.out.print(“String to float: ” + floatNumber);
double doubleNumber = 200;
String text1 = Double.toString(doubleNumber);
System.out.print(“ndouble to String: ” + text1); } }
// String to float: 7.7
// double to String: 200.0
61
Marius Claassen,
Java Intermediate
62. Lecture 7: Convert types exercise
Convert an int to a String named ‘text’
62
Marius Claassen,
Java Intermediate
63. Lecture 7: Convert types exercise
public class Lecture8 {
public static void main(String[] args) {
int value = 1_000;
// TODO: Convert int ‘value’ to a String ‘text’
System.out.print(“int to String: ” + text);
}
}
63
Marius Claassen,
Java Intermediate
64. Lecture 8: Convert types solution
public class Lecture8 {
public static void main(String[] args) {
int value = 1_000;
String text = Integer.toString(value);
System.out.print(“int to String: ” + text);
}
} // int to String: 1000
64
Marius Claassen,
Java Intermediate
65. Lecture 9: Keyboard input example
Keyboard input problem statement:
Implement keyboard input to print the name
of the world’s largest economy
65
Marius Claassen,
Java Intermediate
66. Lecture 9: Keyboard input example
public class Lecture9 {
public static void main(String[] args) {
System.out.print(“Name the world’s largest economy ”);
Scanner scanner = new Scanner(System.in);
String largestEconomy = scanner.next;
System.out.print(largestEconomy+“ has the largest economy.”);}}
// Name the world’s largest economy ***?
// USA has the largest economy.
66
Marius Claassen,
Java Intermediate
67. Lecture 9: Keyboard input exercise
Create a Scanner object named ‘scanner1’
67
Marius Claassen,
Java Intermediate
68. Lecture 9: Keyboard input exercise
public class Lecture10 {
public static void main(String[] args) {
System.out.print(“Name the world’s most populous country ”);
// TODO: Create a Scanner object named ‘scanner1’
String largestPopulation = scanner1.next;
System.out.print(largestPopulation+“ has the largest population”); } }
68
Marius Claassen,
Java Intermediate
69. Lecture 10: Keyboard input solution
public class Lecture10 {
public static void main(String[] args) {
System.out.print(“Name the world’s most populous country ”);
Scanner scanner1 = new Scanner(System.in);
String largestPopulation = scanner1.next;
System.out.print(largestPopulation+“ has the largest population”); } }
// Name the world’s most populous country ***?
// China has the largest population
69
Marius Claassen,
Java Intermediate
70. TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
Marius Claassen,
Java Intermediate
71. Lecture 11: Import example
Import problem statement:
Implement import statements for the
‘LocalDate’ and ‘Month’ classes
71
Marius Claassen,
Java Intermediate
72. Lecture 11: Import example
import java.time.LocalDate;
import java.time.Month;
public class Lecture11 {
public static void main(String[] args) {
LocalDate jdk1Release = LocalDate.of(1996, Month.JANUARY, 21);
System.out.print(“JDK 1.0 release date: ” + jdk1Release); } }
// JDK 1.0 release date: 1996-01-21
72
Marius Claassen,
Java Intermediate
73. Lecture 11: Import exercise
Implement an import statement for the
‘LocalDate’ class
73
Marius Claassen,
Java Intermediate
74. Lecture 11: Import exercise
// TODO: Import the ‘LocalDate’ class
import java.time.Month;
public class Lecture12 {
public static void main(String[] args) {
LocalDate jdk9ReleaseDate
= LocalDate.of(2017, Month. SEPTEMBER, 21);
System.out.print(“JDK SE 9 release date: ” + jdk9ReleaseDate); } }
74
Marius Claassen,
Java Intermediate
75. Lecture 12: Import solution
import java.time.LocalDate;
import java.time.Month;
public class Lecture12 {
public static void main(String[] args) {
LocalDate jdk9ReleaseDate
= LocalDate.of(2017, Month. SEPTEMBER, 21);
System.out.print(“JDK SE 9 release date: ” + jdk9ReleaseDate); } }
75
Marius Claassen,
Java Intermediate
76. Lecture 13: Package example
Package statement problem statement:
Implement the package statement ‘courses’
76
Marius Claassen,
Java Intermediate
77. Lecture 13: Package example
package courses;
public class Lecture13 {
public static void main(String[] args) {
BeginnerJava beginnerJava = new BeginnerJava();
beginnerJava.printBeginnerJava();
IntermediateJava intermediateJava = new IntermediateJava();
intermediateJava.printIntermediateJava(); } }
// Java Beginner course published
// Java Intermediate course completed
77
Marius Claassen,
Java Intermediate
78. package courses;
public class BeginnerJava {
public void printBeginnerJava() {
System.out.print(“Java Beginner course published”);
}
}
78
Marius Claassen,
Java Intermediate
79. package courses;
public class IntermediateJava {
public void printIntermediateJava() {
System.out.print(“nJava Intermediate course completed”);
}
}
79
Marius Claassen,
Java Intermediate
80. Lecture 13: Package exercise
Implement the package statement
‘intermediatejava’
80
Marius Claassen,
Java Intermediate
81. Lecture 13: Package exercise
// TODO: Implement the package statement ‘intermediatejava’
public class Lecture14 {
public static void main(String[] args) {
Introduction introduction = new Introduction();
introduction.printIntroduction();
Contents contents = new Contents();
contents.printContents(); } }
81
Marius Claassen,
Java Intermediate
82. // TODO: Implement the package statement ‘intermediatejava’
public class Introduction {
public void printIntroduction() {
System.out.print(“Introduction completed, ”);
}
}
82
Marius Claassen,
Java Intermediate
83. // TODO: Implement the package statement ‘intermediatejava’
public class Contents {
public void printContents() {
System.out.print(“Contents started”);
}
}
83
Marius Claassen,
Java Intermediate
84. Lecture 14: Package solution
package intermediatejava;
public class Lecture14 {
public static void main(String[] args) {
Introduction introduction = new Introduction();
introduction.printIntroduction();
Contents contents = new Contents();
contents.printContents(); } }
// Introduction completed, Contents started
84
Marius Claassen,
Java Intermediate
85. package intermediatejava;
public class Introduction {
public void printIntroduction() {
System.out.print(“Introduction completed, ”);
}
}
85
Marius Claassen,
Java Intermediate
86. package intermediatejava;
public class Contents {
public void printContents() {
System.out.print(“Contents started”);
}
}
86
Marius Claassen,
Java Intermediate
87. Lecture 15: String class example
String class problem statement:
Declare a String named ‘businessName’
87
Marius Claassen,
Java Intermediate
88. Lecture 15: String class example
public class Lecture15 {
public static void main(String[] args) {
String businessName = “AlefTav Coding”;
System.out.print(“My business is named ” + businessName);
}
}
// My business is named AlefTav Coding
88
Marius Claassen,
Java Intermediate
89. Lecture 15: String class exercise
Declare a String, ‘courseTitle’ initialized as
‘Java for Intermediate Users’
89
Marius Claassen,
Java Intermediate
90. Lecture 15: String class exercise
public class Lecture16 {
public static void main(String[] args) {
// TODO: Declare a String, ‘courseTitle’ initialized as ‘Java for
// Intermediate Users’
System.out.print(“The title of this course: ” + courseTitle);
}
}
90
Marius Claassen,
Java Intermediate
91. Lecture 16: String class solution
public class Lecture16 {
public static void main(String[] args) {
String courseTitle = “Java for Intermediate Users”;
System.out.print(“The title of this course is ” + courseTitle);
}
}
// The title of this course is Java for Intermediate Users
91
Marius Claassen,
Java Intermediate
92. Lecture 17: Random class example
Random class problem statement:
Generate 4 random integers between 1 and
600, with a seed value of 3
92
Marius Claassen,
Java Intermediate
93. Lecture 17: Random class example
public class Lecture17 {
public static void main(String[] args) {
Random random = new Random(3);
System.out.print(“Four random integers between 1 and 600: ”);
for (int i = 0; i < 4; i++) { System.out.print(random.nextInt(600) + “ ”); }
}
}
// Four random integers between 1 and 600: 134 260 210 181
93
Marius Claassen,
Java Intermediate
94. Lecture 17: Random class exercise
Implement an object named ‘random’ with
seed value 9
94
Marius Claassen,
Java Intermediate
95. Lecture 17: Random class exercise
public class Lecture18 {
public static void main(String[] args) {
// TODO: Implement an object named, ‘random’ with seed value 9
System.out.print(“Five random integers between 1 and 200: ”);
for (int i = 0; i < 5; i++) { System.out.print(random.nextInt(200) + “ ”); }
}
}
95
Marius Claassen,
Java Intermediate
96. Lecture 17: Random class exercise
Implement an object named ‘random’ with
seed value 9
96
Marius Claassen,
Java Intermediate
97. Lecture 17: Random class exercise
public class Lecture18 {
public static void main(String[] args) {
// TODO: Implement an object named, ‘random’ with seed value 9
System.out.print(“Five random integers between 1 and 200: ”);
for (int i = 0; i < 5; i++) { System.out.print(random.nextInt(200) + “ ”); }
}
}
97
Marius Claassen,
Java Intermediate
98. Lecture 18: Random class solution
public class Lecture18 {
public static void main(String[] args) {
Random random = new Random(9);
System.out.print(“Five random integers between 1 and 200: ”);
for (int i = 0; i < 5; i++) { System.out.print(random.nextInt(200) + “ ”); }
}
}
// Five random integers between 1 and 200: 189 196 148 135 159
98
Marius Claassen,
Java Intermediate
99. Lecture 19: Math class example
Math class problem statement:
Print the answer to ‘baseValue’ 15.6, raised
to ‘powerValue’ 4.7
99
Marius Claassen,
Java Intermediate
100. Lecture 19: Math class example
public class Lecture19 {
public static void main(String[] args) {
double baseValue = 15.6;
double powerValue = 4.7;
System.out.printf(“15.6⁴˙⁷: %.3f” + Math.pow(baseValue, powerValue));
}
}
// 405215.092
100
Marius Claassen,
Java Intermediate
101. Lecture 19: Math class exercise
Print to four decimal places the square root
of 1511
101
Marius Claassen,
Java Intermediate
102. Lecture 19: Math class exercise
public class Lecture20 {
public static void main(String[] args) {
double x = 1511;
// TODO: Print to 4 decimal places the square root of 1511
}
}
102
Marius Claassen,
Java Intermediate
103. Lecture 20: Math class solution
public class Lecture20 {
public static void main(String[] args) {
double x = 1511;
System.out.printf(“%.4f”, Math.sqrt(x));
}
}
// 38.8716
103
Marius Claassen,
Java Intermediate
104. TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
104
Marius Claassen,
Java Intermediate
105. Lecture 21: Java class example
Java class problem statement:
Implement a class named ‘Dog’
105
Marius Claassen,
Java Intermediate
106. Lecture 21: Java class example
public class Lecture21 {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.getName();
}
}
// The dog’s name is ‘Faithful’.
106
Marius Claassen,
Java Intermediate
107. public class Dog {
public void getName() {
System.out.print(“The dog’s name is ‘Faithful’.” );
}
}
107
Marius Claassen,
Java Intermediate
108. Lecture 21: Java class exercise
Implement a class named ‘Person’
108
Marius Claassen,
Java Intermediate
109. Lecture 21: Java class exercise
public class Lecture22 {
public static void main(String[] args) {
Person person1 = new Person();
person1.saySomething();
}
}
109
Marius Claassen,
Java Intermediate
110. // TODO: Implement a class named ‘Person’
{
public void saySomething() {
System.out.print(“I am a person.” );
}
}
110
Marius Claassen,
Java Intermediate
111. Lecture 22: Java class solution
public class Lecture22 {
public static void main(String[] args) {
Person person1 = new Person();
person1.saySomething();
}
}
// I am a person.
111
Marius Claassen,
Java Intermediate
112. public class Person
{
public void saySomething() {
System.out.print(“I am a person.” );
}
}
112
Marius Claassen,
Java Intermediate
113. Lecture 23: Java Object example
Instantiate an object problem statement:
Instantiate an object named ‘mobilePhone’
113
Marius Claassen,
Java Intermediate
114. Lecture 23: Java Object example
public class Lecture23 {
public static void main(String[] args) {
MobilePhone mobilePhone = new MobilePhone();
mobilePhone.getName();
}
}
// Samsung or iPhone
114
Marius Claassen,
Java Intermediate
115. public class MobilePhone {
public void getName() {
System.out.print(“Samsung or iPhone” );
}
}
115
Marius Claassen,
Java Intermediate
116. Lecture 23: Java Object exercise
Instantiate an object named ‘person’
116
Marius Claassen,
Java Intermediate
117. Lecture 23: Java Object exercise
public class Lecture24 {
public static void main(String[] args) {
// TODO: Instantiate an object named ‘person’
person.speak();
}
}
117
Marius Claassen,
Java Intermediate
118. public class Person {
public void speak() {
System.out.print(“What a wonderful world!” );
}
}
118
Marius Claassen,
Java Intermediate
119. Lecture 24: Java Object solution
public class Lecture24 {
public static void main(String[] args) {
Person person = new Person();
person.speak();
}
}
// What a wonderful world!
119
Marius Claassen,
Java Intermediate
120. public class Person {
public void speak() {
System.out.print(“What a wonderful world!” );
}
}
120
Marius Claassen,
Java Intermediate
121. Lecture 25: Constructor example
Constructor problem statement:
Implement the constructor of the class
‘MobilePhone’
121
Marius Claassen,
Java Intermediate
122. Lecture 25: Constructor example
public class Lecture25 {
public static void main(String[] args) {
MobilePhone mobilePhone = new MobilePhone(“Samsung”, 100);
}
}
// NAME: Samsung
// PRICE: $100.00
122
Marius Claassen,
Java Intermediate
123. public class MobilePhone {
String name;
double price;
public MobilePhone(String newName, double newPrice) { // constructor
name = newName;
price = newPrice;
System.out.printf(“NAME: %snPRICE: $%.2f”, name, price);
}
}
123
Marius Claassen,
Java Intermediate
124. Lecture 25: Constructor exercise
Implement the constructor of the ‘Person’
class
124
Marius Claassen,
Java Intermediate
125. Lecture 25: Constructor exercise
public class Lecture26 {
public static void main(String[] args) {
Person person = new Person(“Sarah”, 50);
}
}
125
Marius Claassen,
Java Intermediate
126. public class Person {
String name;
int age;
// TODO: Implement the constructor of the ‘Person’ class
{
name = newName;
age = newAge;
System.out.print(“NAME: ” + name + “, ” + “AGE: ” + age); } }
126
Marius Claassen,
Java Intermediate
127. Lecture 26: Constructor solution
public class Lecture26 {
public static void main(String[] args) {
Person person = new Person(“Sarah”, 50);
}
}
// NAME: Sarah , AGE: 50
127
Marius Claassen,
Java Intermediate
128. public class Person {
String name;
int age;
public Person(String newName, int newAge)
{
name = newName;
age = newAge;
System.out.print(“NAME: ” + name + “, ” + “AGE: ” + age); } }
128
Marius Claassen,
Java Intermediate
129. Lecture 27: Encapsulation example
Encapsulation problem statement:
Declare a private field ‘price’ and implement
a public method ‘setPrice’ with one
parameter ‘newPrice’
129
Marius Claassen,
Java Intermediate
130. Lecture 27: Encapsulation example
public class Lecture27 {
public static void main(String[] args) {
MobilePhone mobilePhone = new MobilePhone(300);
}
}
// Mobile phone price: $300.00
130
Marius Claassen,
Java Intermediate
131. public class MobilePhone {
private double price;
public void setPrice(double newPrice) {
price = newPrice;
System.out.printf(“Mobile phone price: $%.2f”, price);
}
}
131
Marius Claassen,
Java Intermediate
132. Lecture 27: Encapsulation exercise
Declare a private field ‘name’ and implement
a public method ‘setName’ with one
parameter ‘newName’
132
Marius Claassen,
Java Intermediate
133. Lecture 27: Encapsulation exercise
public class Lecture28 {
public static void main(String[] args) {
Person person = new Person();
person.setName(“David”);
}
}
133
Marius Claassen,
Java Intermediate
134. public class Person {
// TODO: Declare a private field ‘name’
// TODO: Implement a public method ‘setName’ with one parameter
// ‘newName’
{
name = newName;
System.out.printf(“NAME: %s”, name); } }
134
Marius Claassen,
Java Intermediate
135. Lecture 28: Encapsulation solution
public class Lecture28 {
public static void main(String[] args) {
Person person = new Person();
person.setName(“David”);
}
}
// NAME: David
135
Marius Claassen,
Java Intermediate
136. public class Person {
private String name;
public void setName(String newName)
{
name = newName;
System.out.printf(“NAME: %s”, name); } }
136
Marius Claassen,
Java Intermediate
137. Lecture 29: ArrayList example
ArrayList problem statement:
Implement an ArrayList of type String, named
‘planets’
137
Marius Claassen,
Java Intermediate
138. Lecture 29: ArrayList example
public class Lecture29 {
public static void main(String[] args) {
ArrayList<String> planets = new ArrayList<>();
planets.add(“Mercury”);
planets.add(“Venus”);
planets.add(“Earth”);
planets.add(“Mars”);
for (String planet : planets) { System.out.print(planet + “ ”); } } }
// Mercury Venus Earth Mars
138
Marius Claassen,
Java Intermediate
139. Lecture 29: ArrayList exercise
Implement an ArrayList of type String, named
‘currencies’
139
Marius Claassen,
Java Intermediate
140. Lecture 29: ArrayList exercise
public class Lecture30 {
public static void main(String[] args) {
// TODO: Implement an ArrayList of type String , named ‘currencies’
currencies.add(“US Dollar”);
currencies.add(“Indian Rupee”);
currencies.add(“Thai Baht”);
for (String currency : currencies) { System.out.print(currency + “ ”); } } }
140
Marius Claassen,
Java Intermediate
141. Lecture 30: ArrayList solution
public class Lecture30 {
public static void main(String[] args) {
ArrayList<String> currencies = new ArrayList<>();
currencies.add(“US Dollar”);
currencies.add(“Indian Rupee”);
currencies.add(“Thai Baht”);
for (String currency : currencies) { System.out.print(currency + “ ”); } } }
// US Dollar Indian Rupee Thai Baht
141
Marius Claassen,
Java Intermediate
142. Lecture 31: Debugging example
Debugging problem statement:
Implement debugging to handle the
exception, StackOverflowError
142
Marius Claassen,
Java Intermediate
143. Lecture 31: Debugging example
public class Lecture31 {
public static void main(String[] args) {
int y = 8;
System.out.print(“Fibonacci value at index ” + y + “: ” + getFibonacci(y));
}
143
Marius Claassen,
Java Intermediate
144. private static long getFibonacci(int x) {
return x <= 2 ? 1 : getFibonacci(x - 1) + getFibonacci(x - 2) ;
}
}
// Fibonacci value at index 8: 21
144
Marius Claassen,
Java Intermediate
145. Lecture 31: Debugging exercise
Implement debugging to handle the
exception, StackOverflowError
145
Marius Claassen,
Java Intermediate
146. Lecture 31: Debugging exercise
public class Lecture32 {
public static void main(String[] args) {
int y = 10;
System.out.print(“Fibonacci value at index ” + y + “: ” + getFibonacci(y));
}
146
Marius Claassen,
Java Intermediate
147. private static long getFibonacci(int x) {
// TODO: Implement debugging to handle the exception,
// StackOverflowError
return getFibonacci(x - 1) + getFibonacci(x - 2);
}
}
147
Marius Claassen,
Java Intermediate
148. Lecture 32: Debugging solution
public class Lecture32 {
public static void main(String[] args) {
int y = 10;
System.out.print(“Fibonacci value at index ” + y + “: ” + getFibonacci(y));
}
148
Marius Claassen,
Java Intermediate
149. private static long getFibonacci(int x) {
return x <= 2 ? 1 : getFibonacci(x - 1) + getFibonacci(x - 2) ;
}
}
// Fibonacci value at index 10: 55
149
Marius Claassen,
Java Intermediate
150. TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
150
Marius Claassen,
Java Intermediate
151. Lecture 33: Lambda Part 1 example
Lambda part 1 problem statement:
Implement a lambda expression to print
‘Hello, lambda’
151
Marius Claassen,
Java Intermediate
152. Lecture 33: Lambda Part 1 example
public class Lecture33 {
public static void main(String[] args) {
Thread thread1 = new Thread( () -> System.out.print(“Hello, lambda”); )
thread1.run();
}
}
// Hello, lambda
152
Marius Claassen,
Java Intermediate
153. Lecture 33: Lambda Part 1 exercise
Implement a lambda object named ‘thread’ to
print ‘My first lambda’
153
Marius Claassen,
Java Intermediate
154. Lecture 33: Lambda Part 1 exercise
public class Lecture34 {
public static void main(String[] args) {
// TODO: Implement a lambda object named ‘thread’ to print,
// ‘My first lambda’
thread.run();
}
}
154
Marius Claassen,
Java Intermediate
155. Lecture 34: Lambda Part 1 solution
public class Lecture34 {
public static void main(String[] args) {
Thread thread = new Thread( () -> System.out.print(“My first lambda”); )
thread.run();
}
}
// My first lambda
155
Marius Claassen,
Java Intermediate
156. Lecture 35: Lambda Part 2 example
Lambda Part 2 problem statement:
Implement a lambda to remove even numbers
from a list
156
Marius Claassen,
Java Intermediate
157. Lecture 35: Lambda Part 2 example
import java.util.ArrayList; import java.util.Arrays;
public class Lecture35 {
public static void main(String[] args) {
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(
1, 2, 3, 4, 5, 6, 7) );
values.removeIf(i -> i % 2 == 0);
values.forEach(i -> System.out.print(i + “ ”) ); } }
// 1 3 5 7
157
Marius Claassen,
Java Intermediate
158. Lecture 35: Lambda Part 2 exercise
Implement a lambda to remove odd numbers
from a list
158
Marius Claassen,
Java Intermediate
159. Lecture 35: Lambda Part 2 exercise
import java.util.ArrayList; import java.util.Arrays;
public class Lecture36 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(
5, 6, 7, 8, 9, 10) );
// TODO: Implement a lambda to remove odd numbers from a list
numbers.forEach(i -> System.out.print(i + “ ”) ); } }
159
Marius Claassen,
Java Intermediate
160. Lecture 36: Lambda Part 2 solution
import java.util.ArrayList; import java.util.Arrays;
public class Lecture36 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(
5, 6, 7, 8, 9, 10) );
numbers.removeIf(i -> i % 2 != 0);
numbers.forEach(i -> System.out.print(i + “ ”) ); } }
// 6 8 10
160
Marius Claassen,
Java Intermediate
161. Lecture 37: Streams Part 1 example
Streams Part 1 problem statement:
Implement the stream() to print words in
alphabetical order
161
Marius Claassen,
Java Intermediate
162. Lecture 37: Streams Part 1 example
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator;
public class Lecture37 {
public static void main(String[] args) {
ArrayList<String> cities = new ArrayList<>(Arrays.asList(“Mumbai”,
“Karachi”, “Los Angeles”, “Nonthaburi City”, “Kolkata”, “New York”) );
cities.stream().sorted(Comparator.naturalOrder())
.forEach(s -> System.out.print(s + “ ”) ); } }
// Karachi Kolkata Los Angeles Mumbai New York Nonthaburi City
162
Marius Claassen,
Java Intermediate
163. Lecture 37: Streams Part1 exercise
Implement the stream() to print words in
reverse order
163
Marius Claassen,
Java Intermediate
164. Lecture 37: Streams Part1 exercise
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator;
public class Lecture38 {
public static void main(String[] args) {
ArrayList<String> capitalCities = new ArrayList<>(Arrays.asList(
“Delhi”, “Islamabad”, “Bangkok”, “Washington”, “Jakarta”) );
// TODO: Implement the stream()
// TODO: to print words
// TODO: in reverse order
} }
164
Marius Claassen,
Java Intermediate
165. Lecture 38: Streams Part 1 solution
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator;
public class Lecture38 {
public static void main(String[] args) {
ArrayList<String> capitalCities = new ArrayList<>(Arrays.asList(
“Delhi”, “Islamabad”, “Bangkok”, “Washington”, “Jakarta”) );
capitalCities.stream().sorted(Comparator.reverseOrder())
.forEach(s -> System.out.print(s + “ ”) ); } }
// Washington Jakarta Islamabad Delhi Bangkok
165
Marius Claassen,
Java Intermediate
166. Lecture 39: Streams Part 2 example
Streams Part 2 problem statement:
Implement the stream() to print an int using
the filter, map, reduce pattern
166
Marius Claassen,
Java Intermediate
167. Lecture 39: Streams Part 2 example
import java.util.ArrayList; import java.util.Arrays;
public class Lecture39 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(
6, 7, 8, 9) );
int sum = numbers.stream().filter(i -> i < 8).map(e -> e * 3)
.reduce(0, (partAnswer, y) -> partAnswer + y);
System.out.print(sum); } }
// 39
167
Marius Claassen,
Java Intermediate
168. Lecture 39: Streams Part2 exercise
Implement the stream() to print an int using
the filter, map, reduce pattern
168
Marius Claassen,
Java Intermediate
169. Lecture 39: Streams Part2 exercise
import java.util.ArrayList; import java.util.Arrays;
public class Lecture40 {
public static void main(String[] args) {
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(
1, 2, 3, 4, 5) );
// TODO: Square each value larger than 3 and get their sum
// TODO:
// TODO:
System.out.print(sum); } }
169
Marius Claassen,
Java Intermediate
170. Lecture 40: Streams Part 2 solution
import java.util.ArrayList; import java.util.Arrays;
public class Lecture40 {
public static void main(String[] args) {
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(
1, 2, 3, 4, 5) );
int sum = values.stream().filter(i -> i > 3).map(e -> e * e)
.reduce(0, (partAnswer, y) -> partAnswer + y);
System.out.print(sum); } }
// 41
170
Marius Claassen,
Java Intermediate
171. TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
Marius Claassen,
Java Intermediate
172. Lecture 41: JavaFX Colors example
JavaFX colors problem statement:
Implement the ColorPicker class to display
colours and their web # values
172
Marius Claassen,
Java Intermediate
176. Lecture 41: JavaFX Colors exercise
Implement an object named ‘colorPicker’ to
display colours and their web # values
176
Marius Claassen,
Java Intermediate
178. public class Lecture42 extends Application {
@Override public void start(Stage stage) {
// TODO: Implement an object named ‘colorPicker’
// TODO: to display colours and their # web values
HBox hBox = new HBox(colorPicker);
hBox.setStyle(“-fx-background-color: #d3d3d3”);
stage.setTitle(“JavaFX Colors Solution”);
stage.setScene(new Scene(hBox, 400, 400) );
stage.show(); } }
178
Marius Claassen,
Java Intermediate
182. Lecture 43: JavaFX shape example
JavaFX shapes problem statement:
Create a gray rectangle with dimensions 0, 0,
230, 150
182
Marius Claassen,
Java Intermediate
192. Lecture 45: JavaFX 3D graphics
JavaFX graphics problem statement:
Create an object named ‘sphere’ with radius
of 200. Wrap the ‘phongMaterial’ object onto
the sphere and add the sphere to the ‘vBox’
object
https://en.wikipedia.org/wiki/Behrmann_projection#/media/File:Behrmann_projection_SW.jpg
192
Marius Claassen,
Java Intermediate
197. Lecture 45: JavaFX graphics exercise
Create an object named ‘sphere’ with radius
of 200. Wrap the ‘phongMaterial’ object onto
the sphere and add the sphere to the ‘vBox’
object
https://en.wikipedia.org/wiki/List_of_map_projections#/media/File:Equirectangular_projection_SW.jpg
197
Marius Claassen,
Java Intermediate
199. public class Lecture46 extends Application {
@Override public void start(Stage stage) {
VBox vBox = new VBox();
Image image = new Image(Lecture45.class.getResource(
“worldmap2.jpg”).toExternalForm() );
PhongMaterial phongMaterial = new PhongMaterial();
phongMaterial.setDiffuseColor(Color.WHITE);
phongMaterial.setDiffuseMap(image);
199
Marius Claassen,
Java Intermediate
200. // TODO: Create an object named ‘sphere’ with radius of 200.
// TODO: Wrap the ‘phongMaterial’ object onto the sphere
// TODO: and add the sphere to the vBox
stage.setTitle(“JavaFX Graphics Example”);
stage.setScene(new Scene(vBox, 400, 400) );
stage.show(); } }
200
Marius Claassen,
Java Intermediate
205. Lecture 47: JavaFX video example
JavaFX video problem statement:
Create a video player by implementing the 3
objects ‘media1’, ‘mediaPlayer1’ and
‘mediaView1’.
https://www.youtube.com/watch?v=zg79C7XM1Xs
205
Marius Claassen,
Java Intermediate
206. Lecture 47: JavaFX video example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.io.File;
206
Marius Claassen,
Java Intermediate
207. public class Lecture47 extends Application {
final String PATH = “C:/videos/Java is what Java does video.mp4”;
@Override public void start(Stage stage) {
File file = new File(PATH);
Media media1 = new Media(file.toURI().toString() );
MediaPlayer mediaPlayer1 = new MediaPlayer(media1);
mediaPlayer1.setAutoPlay(true);
MediaView mediaView1 = new MediaView(mediaPlayer1);
207
Marius Claassen,
Java Intermediate
210. Lecture 47: JavaFX video exercise
Create a video player by implementing the 3
objects ‘media’, ‘mediaPlayer’ and
‘mediaView’
https://www.youtube.com/watch?v=b-Cr0EWwaTk
210
Marius Claassen,
Java Intermediate
212. public class Lecture48 extends Application {
final String PATH = “C:/videos/Java life video.mp4”;
@Override public void start(Stage stage) {
File file = new File(PATH);
// TODO: Create a media player by implementing the 3 objects ‘media’
// TODO: ‘mediaPlayer’
mediaPlayer.setAutoPlay(true);
// TODO: and ‘mediaView’
212
Marius Claassen,
Java Intermediate
215. public class Lecture48 extends Application {
final String PATH = “C:/videos/Java life video.mp4”;
@Override public void start(Stage stage) {
File file = new File(PATH);
Media media = new Media(file.toURI().toString() );
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView(mediaPlayer);
215
Marius Claassen,
Java Intermediate
218. Lecture 49: KeyPressed example
JavaFX KeyPressed problem statement:
Implement the setOnKeyPressed() method to
print whether or not the letter ‘B’ is pressed
218
Marius Claassen,
Java Intermediate
220. public class Lecture49 extends Application {
@Override public void start(Stage stage) {
HBox hBox = new HBox();
Text text = new Text();
hBox.getChilden().add(text);
hBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(hBox, 400, 400);
scene.setFill(Color.LIME);
220
Marius Claassen,
Java Intermediate
223. Lecture 49: keyPressed exercise
Implement the setOnKeyPressed() method to
print whether or not the TAB key is pressed
223
Marius Claassen,
Java Intermediate
225. public class Lecture50 extends Application {
@Override public void start(Stage stage) {
HBox hBox = new HBox();
Text text = new Text();
hBox.getChilden().add(text);
hBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(hBox, 400, 400);
scene.setFill(Color.GREENYELLOW);
225
Marius Claassen,
Java Intermediate
226. // TODO: Implement the setOnKeyPressed() to print whether or not
// TODO: the TAB key is pressed
stage.setTitle(“JavaFX KeyPressed Event Solution”);
stage.setScene(scene);
stage.show(); } }
226
Marius Claassen,
Java Intermediate
228. public class Lecture50 extends Application {
@Override public void start(Stage stage) {
HBox hBox = new HBox();
Text text = new Text();
hBox.getChilden().add(text);
hBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(hBox, 400, 400);
scene.setFill(Color.GREENYELLOW);
228
Marius Claassen,
Java Intermediate
233. To get details about this course:
• mariusclaassen@gmail.com
or
• https://www.udemy.com/course/1133518/manage/basics/
233
Marius Claassen,
Java Intermediate