Date post: | 21-Jan-2018 |
Category: |
Technology |
Upload: | marius-claassen |
View: | 62 times |
Download: | 0 times |
Build on programming basics, by using
the world’s most popular language to:
• Maintain your advantage
• Refine your skills
• Write functional code
2Marius Claassen,
Java Intermediate
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.
3Marius Claassen,
Java Intermediate
Benefits
• Read Java code
• Develop useful Java applications
• Devise Java solutions when given a
problem statement
4Marius Claassen,
Java Intermediate
Major components
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
5Marius Claassen,
Java Intermediate
Lecture outline
• 51 Lectures
• 5 minutes
1. Coding exercise
2. Lecture description
3. PDF
4. Video
6Marius Claassen,
Java Intermediate
Ideal student
* Completed Java beginner course
* You want to improve your skills by:• watching live coding
• doing coding exercises
• checking answers against solutions
7Marius Claassen,
Java Intermediate
To get details about this course:
or
• https://www.udemy.com/course/1133518/manage/basics/
9Marius Claassen,
Java Intermediate
TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
10Marius Claassen,
Java Intermediate
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)
11Marius Claassen,
Java Intermediate
TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
49Marius Claassen,
Java Intermediate
Lecture 3: Numeric types example
Numeric types problem statement:
Print the numeric data types int and float with
their names and values
50Marius Claassen,
Java Intermediate
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.051
Marius Claassen,
Java Intermediate
Lecture 3: Numeric types exercise
Print the numeric data type long initialized as
6 000 000
52Marius Claassen,
Java Intermediate
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’
}
}
53Marius Claassen,
Java Intermediate
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
54Marius Claassen,
Java Intermediate
Lecture 5: Textual type example
Textual types problem statement:
Implement the textual data type ‘String’ and
print its name and value
55Marius Claassen,
Java Intermediate
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.
56Marius Claassen,
Java Intermediate
Lecture 5: Textual type exercise
Print the textual data type ‘String’ initialized
as ‘Java continues to dominate.’
57Marius Claassen,
Java Intermediate
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’
}
}
58Marius Claassen,
Java Intermediate
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.
59Marius Claassen,
Java Intermediate
Lecture 7: Convert types example
Converting types problem statement:
Convert a String to a float and a double to a
String
60Marius Claassen,
Java Intermediate
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.061
Marius Claassen,
Java Intermediate
Lecture 7: Convert types exercise
Convert an int to a String named ‘text’
62Marius Claassen,
Java Intermediate
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
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: 100064
Marius Claassen,
Java Intermediate
Lecture 9: Keyboard input example
Keyboard input problem statement:
Implement keyboard input to print the name
of the world’s largest economy
65Marius Claassen,
Java Intermediate
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.
66Marius Claassen,
Java Intermediate
Lecture 9: Keyboard input exercise
Create a Scanner object named ‘scanner1’
67Marius Claassen,
Java Intermediate
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”); } }
68Marius Claassen,
Java Intermediate
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
69Marius Claassen,
Java Intermediate
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
Lecture 11: Import example
Import problem statement:
Implement import statements for the
‘LocalDate’ and ‘Month’ classes
71Marius Claassen,
Java Intermediate
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
72Marius Claassen,
Java Intermediate
Lecture 11: Import exercise
Implement an import statement for the
‘LocalDate’ class
73Marius Claassen,
Java Intermediate
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); } }
74Marius Claassen,
Java Intermediate
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); } }
75Marius Claassen,
Java Intermediate
Lecture 13: Package example
Package statement problem statement:
Implement the package statement ‘courses’
76Marius Claassen,
Java Intermediate
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 completed77
Marius Claassen,
Java Intermediate
package courses;
public class BeginnerJava {
public void printBeginnerJava() {
System.out.print(“Java Beginner course published”);
}
}
78Marius Claassen,
Java Intermediate
package courses;
public class IntermediateJava {
public void printIntermediateJava() {
System.out.print(“\nJava Intermediate course completed”);
}
}
79Marius Claassen,
Java Intermediate
Lecture 13: Package exercise
Implement the package statement
‘intermediatejava’
80Marius Claassen,
Java Intermediate
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(); } }
81Marius Claassen,
Java Intermediate
// TODO: Implement the package statement ‘intermediatejava’
public class Introduction {
public void printIntroduction() {
System.out.print(“Introduction completed, ”);
}
}
82Marius Claassen,
Java Intermediate
// TODO: Implement the package statement ‘intermediatejava’
public class Contents {
public void printContents() {
System.out.print(“Contents started”);
}
}
83Marius Claassen,
Java Intermediate
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
84Marius Claassen,
Java Intermediate
package intermediatejava;
public class Introduction {
public void printIntroduction() {
System.out.print(“Introduction completed, ”);
}
}
85Marius Claassen,
Java Intermediate
package intermediatejava;
public class Contents {
public void printContents() {
System.out.print(“Contents started”);
}
}
86Marius Claassen,
Java Intermediate
Lecture 15: String class example
String class problem statement:
Declare a String named ‘businessName’
87Marius Claassen,
Java Intermediate
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
88Marius Claassen,
Java Intermediate
Lecture 15: String class exercise
Declare a String, ‘courseTitle’ initialized as
‘Java for Intermediate Users’
89Marius Claassen,
Java Intermediate
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);
}
}
90Marius Claassen,
Java Intermediate
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
91Marius Claassen,
Java Intermediate
Lecture 17: Random class example
Random class problem statement:
Generate 4 random integers between 1 and
600, with a seed value of 3
92Marius Claassen,
Java Intermediate
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
93Marius Claassen,
Java Intermediate
Lecture 17: Random class exercise
Implement an object named ‘random’ with
seed value 9
94Marius Claassen,
Java Intermediate
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) + “ ”); }
}
}
95Marius Claassen,
Java Intermediate
Lecture 17: Random class exercise
Implement an object named ‘random’ with
seed value 9
96Marius Claassen,
Java Intermediate
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) + “ ”); }
}
}
97Marius Claassen,
Java Intermediate
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
98Marius Claassen,
Java Intermediate
Lecture 19: Math class example
Math class problem statement:
Print the answer to ‘baseValue’ 15.6, raised
to ‘powerValue’ 4.7
99Marius Claassen,
Java Intermediate
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
100Marius Claassen,
Java Intermediate
Lecture 19: Math class exercise
Print to four decimal places the square root
of 1511
101Marius Claassen,
Java Intermediate
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
}
}
102Marius Claassen,
Java Intermediate
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
103Marius Claassen,
Java Intermediate
TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
104Marius Claassen,
Java Intermediate
Lecture 21: Java class example
Java class problem statement:
Implement a class named ‘Dog’
105Marius Claassen,
Java Intermediate
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’.
106Marius Claassen,
Java Intermediate
public class Dog {
public void getName() {
System.out.print(“The dog’s name is ‘Faithful’.” );
}
}
107Marius Claassen,
Java Intermediate
Lecture 21: Java class exercise
Implement a class named ‘Person’
108Marius Claassen,
Java Intermediate
Lecture 21: Java class exercise
public class Lecture22 {
public static void main(String[] args) {
Person person1 = new Person();
person1.saySomething();
}
}
109Marius Claassen,
Java Intermediate
// TODO: Implement a class named ‘Person’
{
public void saySomething() {
System.out.print(“I am a person.” );
}
}
110Marius Claassen,
Java Intermediate
Lecture 22: Java class solution
public class Lecture22 {
public static void main(String[] args) {
Person person1 = new Person();
person1.saySomething();
}
}
// I am a person.
111Marius Claassen,
Java Intermediate
public class Person
{
public void saySomething() {
System.out.print(“I am a person.” );
}
}
112Marius Claassen,
Java Intermediate
Lecture 23: Java Object example
Instantiate an object problem statement:
Instantiate an object named ‘mobilePhone’
113Marius Claassen,
Java Intermediate
Lecture 23: Java Object example
public class Lecture23 {
public static void main(String[] args) {
MobilePhone mobilePhone = new MobilePhone();
mobilePhone.getName();
}
}
// Samsung or iPhone
114Marius Claassen,
Java Intermediate
public class MobilePhone {
public void getName() {
System.out.print(“Samsung or iPhone” );
}
}
115Marius Claassen,
Java Intermediate
Lecture 23: Java Object exercise
Instantiate an object named ‘person’
116Marius Claassen,
Java Intermediate
Lecture 23: Java Object exercise
public class Lecture24 {
public static void main(String[] args) {
// TODO: Instantiate an object named ‘person’
person.speak();
}
}
117Marius Claassen,
Java Intermediate
public class Person {
public void speak() {
System.out.print(“What a wonderful world!” );
}
}
118Marius Claassen,
Java Intermediate
Lecture 24: Java Object solution
public class Lecture24 {
public static void main(String[] args) {
Person person = new Person();
person.speak();
}
}
// What a wonderful world!
119Marius Claassen,
Java Intermediate
public class Person {
public void speak() {
System.out.print(“What a wonderful world!” );
}
}
120Marius Claassen,
Java Intermediate
Lecture 25: Constructor example
Constructor problem statement:
Implement the constructor of the class
‘MobilePhone’
121Marius Claassen,
Java Intermediate
Lecture 25: Constructor example
public class Lecture25 {
public static void main(String[] args) {
MobilePhone mobilePhone = new MobilePhone(“Samsung”, 100);
}
}
// NAME: Samsung
// PRICE: $100.00
122Marius Claassen,
Java Intermediate
public class MobilePhone {
String name;
double price;
public MobilePhone(String newName, double newPrice) { // constructor
name = newName;
price = newPrice;
System.out.printf(“NAME: %s\nPRICE: $%.2f”, name, price);
}
}123
Marius Claassen,
Java Intermediate
Lecture 25: Constructor exercise
Implement the constructor of the ‘Person’
class
124Marius Claassen,
Java Intermediate
Lecture 25: Constructor exercise
public class Lecture26 {
public static void main(String[] args) {
Person person = new Person(“Sarah”, 50);
}
}
125Marius Claassen,
Java Intermediate
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); } }
126Marius Claassen,
Java Intermediate
Lecture 26: Constructor solution
public class Lecture26 {
public static void main(String[] args) {
Person person = new Person(“Sarah”, 50);
}
}
// NAME: Sarah , AGE: 50
127Marius Claassen,
Java Intermediate
public class Person {
String name;
int age;
public Person(String newName, int newAge)
{
name = newName;
age = newAge;
System.out.print(“NAME: ” + name + “, ” + “AGE: ” + age); } }
128Marius Claassen,
Java Intermediate
Lecture 27: Encapsulation example
Encapsulation problem statement:
Declare a private field ‘price’ and implement
a public method ‘setPrice’ with one
parameter ‘newPrice’
129Marius Claassen,
Java Intermediate
Lecture 27: Encapsulation example
public class Lecture27 {
public static void main(String[] args) {
MobilePhone mobilePhone = new MobilePhone(300);
}
}
// Mobile phone price: $300.00
130Marius Claassen,
Java Intermediate
public class MobilePhone {
private double price;
public void setPrice(double newPrice) {
price = newPrice;
System.out.printf(“Mobile phone price: $%.2f”, price);
}
}
131Marius Claassen,
Java Intermediate
Lecture 27: Encapsulation exercise
Declare a private field ‘name’ and implement
a public method ‘setName’ with one
parameter ‘newName’
132Marius Claassen,
Java Intermediate
Lecture 27: Encapsulation exercise
public class Lecture28 {
public static void main(String[] args) {
Person person = new Person();
person.setName(“David”);
}
}
133Marius Claassen,
Java Intermediate
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); } }
134Marius Claassen,
Java Intermediate
Lecture 28: Encapsulation solution
public class Lecture28 {
public static void main(String[] args) {
Person person = new Person();
person.setName(“David”);
}
}
// NAME: David
135Marius Claassen,
Java Intermediate
public class Person {
private String name;
public void setName(String newName)
{
name = newName;
System.out.printf(“NAME: %s”, name); } }
136Marius Claassen,
Java Intermediate
Lecture 29: ArrayList example
ArrayList problem statement:
Implement an ArrayList of type String, named
‘planets’
137Marius Claassen,
Java Intermediate
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 Mars138
Marius Claassen,
Java Intermediate
Lecture 29: ArrayList exercise
Implement an ArrayList of type String, named
‘currencies’
139Marius Claassen,
Java Intermediate
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 + “ ”); } } }
140Marius Claassen,
Java Intermediate
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
141Marius Claassen,
Java Intermediate
Lecture 31: Debugging example
Debugging problem statement:
Implement debugging to handle the
exception, StackOverflowError
142Marius Claassen,
Java Intermediate
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));
}
143Marius Claassen,
Java Intermediate
private static long getFibonacci(int x) {
return x <= 2 ? 1 : getFibonacci(x - 1) + getFibonacci(x - 2) ;
}
}
// Fibonacci value at index 8: 21
144Marius Claassen,
Java Intermediate
Lecture 31: Debugging exercise
Implement debugging to handle the
exception, StackOverflowError
145Marius Claassen,
Java Intermediate
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));
}
146Marius Claassen,
Java Intermediate
private static long getFibonacci(int x) {
// TODO: Implement debugging to handle the exception,
// StackOverflowError
return getFibonacci(x - 1) + getFibonacci(x - 2);
}
}
147Marius Claassen,
Java Intermediate
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));
}
148Marius Claassen,
Java Intermediate
private static long getFibonacci(int x) {
return x <= 2 ? 1 : getFibonacci(x - 1) + getFibonacci(x - 2) ;
}
}
// Fibonacci value at index 10: 55
149Marius Claassen,
Java Intermediate
TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
150Marius Claassen,
Java Intermediate
Lecture 33: Lambda Part 1 example
Lambda part 1 problem statement:
Implement a lambda expression to print
‘Hello, lambda’
151Marius Claassen,
Java Intermediate
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
152Marius Claassen,
Java Intermediate
Lecture 33: Lambda Part 1 exercise
Implement a lambda object named ‘thread’ to
print ‘My first lambda’
153Marius Claassen,
Java Intermediate
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();
}
}
154Marius Claassen,
Java Intermediate
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
155Marius Claassen,
Java Intermediate
Lecture 35: Lambda Part 2 example
Lambda Part 2 problem statement:
Implement a lambda to remove even numbers
from a list
156Marius Claassen,
Java Intermediate
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
157Marius Claassen,
Java Intermediate
Lecture 35: Lambda Part 2 exercise
Implement a lambda to remove odd numbers
from a list
158Marius Claassen,
Java Intermediate
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 + “ ”) ); } }
159Marius Claassen,
Java Intermediate
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
160Marius Claassen,
Java Intermediate
Lecture 37: Streams Part 1 example
Streams Part 1 problem statement:
Implement the stream() to print words in
alphabetical order
161Marius Claassen,
Java Intermediate
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
162Marius Claassen,
Java Intermediate
Lecture 37: Streams Part1 exercise
Implement the stream() to print words in
reverse order
163Marius Claassen,
Java Intermediate
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
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
165Marius Claassen,
Java Intermediate
Lecture 39: Streams Part 2 example
Streams Part 2 problem statement:
Implement the stream() to print an int using
the filter, map, reduce pattern
166Marius Claassen,
Java Intermediate
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); } }
// 39167
Marius Claassen,
Java Intermediate
Lecture 39: Streams Part2 exercise
Implement the stream() to print an int using
the filter, map, reduce pattern
168Marius Claassen,
Java Intermediate
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
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); } }
// 41170
Marius Claassen,
Java Intermediate
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
Lecture 41: JavaFX Colors example
JavaFX colors problem statement:
Implement the ColorPicker class to display
colours and their web # values
172Marius Claassen,
Java Intermediate
Lecture 41: JavaFX Colors example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
173Marius Claassen,
Java Intermediate
public class Lecture41 extends Application {
@Override public void start(Stage stage) {
ColorPicker colorPicker1 = new ColorPicker();
colorPicker1.setOnAction(event -> colorPicker1.getValue() );
HBox hBox = new HBox(colorPicker1);
hBox.setStyle(“-fx-background-color: #d3d3d3”);
stage.setTitle(“JavaFX Colors Example”);
stage.setScene(new Scene(hBox, 400, 400) );
stage.show(); } } 174
Marius Claassen,
Java Intermediate
Lecture 41: JavaFX Colors exercise
Implement an object named ‘colorPicker’ to
display colours and their web # values
176Marius Claassen,
Java Intermediate
Lecture 41: JavaFX Colors exercise
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
177Marius Claassen,
Java Intermediate
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
Lecture 42: JavaFX Colors solution
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
179Marius Claassen,
Java Intermediate
public class Lecture42 extends Application {
@Override public void start(Stage stage) {
ColorPicker colorPicker = new ColorPicker();
colorPicker.setOnAction(event -> colorPicker.getValue() );
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(); } } 180
Marius Claassen,
Java Intermediate
Lecture 43: JavaFX shape example
JavaFX shapes problem statement:
Create a gray rectangle with dimensions 0, 0,
230, 150
182Marius Claassen,
Java Intermediate
Lecture 43: JavaFX shape example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
183Marius Claassen,
Java Intermediate
public class Lecture43 extends Application {
@Override public void start(Stage stage) {
Rectangle rectangle1 = new Rectangle(0, 0, 230, 150);
rectangle1.setFill(Color.GRAY);
VBox vBox = new VBox();
vBox.getChildren().add(rectangle1);
stage.setTitle(“JavaFX Shapes Example”);
stage.setScene(new Scene(vBox, 400, 400) );
stage.show(); } } 184
Marius Claassen,
Java Intermediate
Lecture 43: JavaFX shape exercise
Create a gray rectangle named ‘rectangle’
with dimensions 0, 0, 390, 100
186Marius Claassen,
Java Intermediate
Lecture 43: JavaFX shape exercise
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
187Marius Claassen,
Java Intermediate
public class Lecture44 extends Application {
@Override public void start(Stage stage) {
// TODO: Create a gray rectangle named ‘rectangle’
// TODO: with dimensions 0, 0, 390, 100
VBox vBox = new VBox();
vBox.getChildren().add(rectangle);
stage.setTitle(“JavaFX Shapes Solution”);
stage.setScene(new Scene(vBox, 400, 400) );
stage.show(); } } 188
Marius Claassen,
Java Intermediate
Lecture 44: JavaFX shapes solution
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
189Marius Claassen,
Java Intermediate
public class Lecture44 extends Application {
@Override public void start(Stage stage) {
Rectangle rectangle = new Rectangle(0, 0, 390, 100);
rectangle.setFill(Color.GRAY);
VBox vBox = new VBox();
vBox.getChildren().add(rectangle);
stage.setTitle(“JavaFX Shapes Solution”);
stage.setScene(new Scene(vBox, 400, 400) );
stage.show(); } } 190
Marius Claassen,
Java Intermediate
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
192Marius Claassen,
Java Intermediate
Lecture 45: JavaFX 3D graphics
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
193Marius Claassen,
Java Intermediate
public class Lecture45 extends Application {
@Override public void start(Stage stage) {
VBox vBox = new VBox();
Image image = new Image(Lecture45.class.getResource(
“worldmap1.jpg”).toExternalForm() );
PhongMaterial phongMaterial = new PhongMaterial();
phongMaterial.setDiffuseColor(Color.WHITE);
phongMaterial.setDiffuseMap(image);
194Marius Claassen,
Java Intermediate
Sphere sphere = new Sphere(200);
sphere.setMaterial(phongMaterial);
vBox.getChildren().add(sphere);
stage.setTitle(“JavaFX Graphics Example”);
stage.setScene(new Scene(vBox, 400, 400) );
stage.show(); } }
195Marius Claassen,
Java Intermediate
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
197Marius Claassen,
Java Intermediate
Lecture 45: JavaFX graphics
exercise
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
198Marius Claassen,
Java Intermediate
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);
199Marius Claassen,
Java Intermediate
// 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(); } }
200Marius Claassen,
Java Intermediate
Lecture 46: JavaFX graphics
solution
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
201Marius Claassen,
Java Intermediate
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);
202Marius Claassen,
Java Intermediate
Sphere sphere = new Sphere(200);
sphere.setMaterial(phongMaterial);
vBox.getChildren().add(sphere);
stage.setTitle(“JavaFX Graphics Solution”);
stage.setScene(new Scene(vBox, 400, 400) );
stage.show(); } }
203Marius Claassen,
Java Intermediate
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
205Marius Claassen,
Java Intermediate
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;
206Marius Claassen,
Java Intermediate
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);
207Marius Claassen,
Java Intermediate
mediaView.setFitWidth(800);
mediaView.setFitHeight(500);
StackPane stackPane = new StackPane(mediaView1);
stage.setTitle(“JavaFX Video Example”);
stage.setScene(new Scene(stackPane) );
stage.show(); } } 208
Marius Claassen,
Java Intermediate
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
210Marius Claassen,
Java Intermediate
Lecture 47: JavaFX video exercise
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;
211Marius Claassen,
Java Intermediate
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’
212Marius Claassen,
Java Intermediate
mediaView.setFitWidth(800);
mediaView.setFitHeight(500);
StackPane stackPane = new StackPane(mediaView);
stage.setTitle(“JavaFX Video Solution”);
stage.setScene(new Scene(stackPane) );
stage.show(); } } 213
Marius Claassen,
Java Intermediate
Lecture 48: JavaFX video solution
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;
214Marius Claassen,
Java Intermediate
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);
215Marius Claassen,
Java Intermediate
mediaView.setFitWidth(800);
mediaView.setFitHeight(500);
StackPane stackPane = new StackPane(mediaView);
stage.setTitle(“JavaFX Video Solution”);
stage.setScene(new Scene(stackPane) );
stage.show(); } } 216
Marius Claassen,
Java Intermediate
Lecture 49: KeyPressed example
JavaFX KeyPressed problem statement:
Implement the setOnKeyPressed() method to
print whether or not the letter ‘B’ is pressed
218Marius Claassen,
Java Intermediate
Lecture 49: KeyPressed example
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
219Marius Claassen,
Java Intermediate
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);
220Marius Claassen,
Java Intermediate
scene.setOnKeyPressed(event -> text.setText(
event.getCode() == KeyCode.B ? “Letter B” : ”Not letter B”) );
stage.setTitle(“JavaFX KeyPressed Event Example”);
stage.setScene(scene);
stage.show(); } }
221Marius Claassen,
Java Intermediate
Lecture 49: keyPressed exercise
Implement the setOnKeyPressed() method to
print whether or not the TAB key is pressed
223Marius Claassen,
Java Intermediate
Lecture 49: keyPressed exercise
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
224Marius Claassen,
Java Intermediate
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);
225Marius Claassen,
Java Intermediate
// 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(); } }
226Marius Claassen,
Java Intermediate
Lecture 50: keyPressed solution
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
227Marius Claassen,
Java Intermediate
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);
228Marius Claassen,
Java Intermediate
scene.setOnKeyPressed(event -> text.setText(
event.getCode() == KeyCode.TAB ? “TAB key” : ”Not TAB key”) );
stage.setTitle(“JavaFX KeyPressed Event Solution”);
stage.setScene(scene);
stage.show(); } }
229Marius Claassen,
Java Intermediate
TOPICS
1. Introduction
2. Java data types
3. Library classes
4. Java classes
5. Lambdas and Streams
6. JavaFX
7. Conclusion
231Marius Claassen,
Java Intermediate
To get details about this course:
or
• https://www.udemy.com/course/1133518/manage/basics/
233Marius Claassen,
Java Intermediate