CH 01 Advanced Programming
CH 01 Advanced Programming
No Materials
Herbert Schildt (2018), ”Java: The complete Reference”, Tata
Text Books and References 1
McGraw-Hill Education, 11th Ed.
Jan Graba (2013), “An Introduction to Network Programming with
2
Java: Java 7 Compatible”, Springer, 3rd edition.
R. Harold (2013), “Java Network Programming”, O’Reilly, 4th Edi-
3
tion
Mark Lutz (2013), David Ascher, “Learning Python”, O’Reilly, 5th
4
Edition
▶ Group of 6:
Chapters
01 Functional Programming
02 Systems Programming
03 Persistence and Databases
04 Network Programming
05 Remote Procedure Call & Remote Method Invocation
06 Threads
07 GUI
08 Web Programming
”COMPLEXITY!”
→ ↓
Any Solution?
7→ Programming Paradigm?
7→ Programming Paradigm?
1. Imperative Programming
are command driven
”consists of sets of detailed instructions that are given to the computer to
execute in a given order”.
Control flow in imperative programming is explicit
Example: write snippet java program that identify even positve numbers
from list 1, -4, 3, 10, -6, 90, 89, -2.
2. Declarative Programming
focus on the logic of program and the end result
the control flow is not the important 7→ The main focus is achieving the
end result.
Types of Declarative Programming
Functional Programming
Logic Programming
Functional Programming
is declarative pgm Paradigm
Alike other, is problem solving Paradigm 7→ How ?
Functional Programming
is declarative pgm Paradigm
Alike other, is problem solving Paradigm 7→ How ?
⇓
based on the concept of Function / attempts by composing function
What are the key concepts ?
Functional Programming
is declarative pgm Paradigm
Alike other, is problem solving Paradigm 7→ How ?
⇓
based on the concept of Function / attempts by composing function
What are the key concepts ?
1. Functions as first class citizens
2. Pure functions
3. Higher order functions
Functional Programming
is declarative pgm Paradigm
Alike other, is problem solving Paradigm 7→ How ?
⇓
based on the concept of Function / attempts by composing function
What are the key concepts ?
1. Functions as first class citizens
2. Pure functions
3. Higher order functions
Functional Programming
is declarative pgm Paradigm
Alike other, is problem solving Paradigm 7→ How ?
⇓
based on the concept of Function / attempts by composing function
What are the key concepts ?
1. Functions as first class citizens
2. Pure functions
3. Higher order functions
Package Description
The Java Utilities Package contains utility classes and interfaces that enable such actions as date and time
java.util manipulations, random-number processing (class Random) and the storing and processing of large amounts of
data.
The Java Applet Package contains a class and several interfaces required to create Java applets—programs that
java.applet
execute in web browsers
The Java Abstract Window Toolkit Package contains the classes and interfaces required to create and manipulate
java.awt
GUIs
The Java Swing GUI Components Package contains classes and interfaces for Java’s Swing GUI components
javax.swing
that provide support for porta- ble GUIs
The Java Concurrency Package contains utility classes and interfaces for implementing programs that can
java.util.concurrent
perform multiple tasks in parallel.
java.lang The Java Language Package contains classes and interfaces that are required by many Java programs
The Java Networking Package contains classes and interfaces that enable programs to communicate via computer
java.net
networks like the Internet
java.sql The The JDBC Package contains classes and interfaces for working with databases
The The Java Media Framework Package contains classes and interfaces for working with Java’s multimedia
javax.media
capabilities
The Java Swing Event Package contains classes and interfaces thate nable event handling for GUI components
javax.swing.event
in package javax.swing
The The Java Input/Output Package contains classes and interfaces that enable programs to input and output
java.io
data.
User-defined Packages
Java also allows you to create packages as per your need.
Syntax:
use package keyword followd by package name
package package_name
package authentication;
public interfcae Authentication {
User-defined Packages
Java also allows you to create packages as per your need.
Syntax:
use package keyword followd by package name
package package_name
package authentication;
public interfcae Authentication {
import authentication.Login;
import authentication.authenticate;
import java.util.Scanner;
public class LoginAuthentication implements Authentication{
}
public void authenticate(){
// Logic for authenication
}
}
By: Mule Ta – – Advanced Programming 17
CH01: Functional Programming
User-defined Packages
Java also allows you to create packages as per your need.
Syntax:
use package keyword followd by package name
package package_name
package authentication;
public interfcae Authentication {
}
public String Login() {
}
// Logic for receiving information
}
public void authenticate(){
// Logic for authenication
}
}
By: Mule Ta – – Advanced Programming 17
CH01: Functional Programming
Iterator interface
Iterator interface
iterates the elements used traverse the list and modify the elements.
is part of the java.util package api
iterator interface has three abstract method:
public boolean hasNext()
public object next()
public void remove()
forEachRemaining()
Syntax:
import java.util.Iterator // importing the Iterrator Intreface
int[] arrayElemets = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 0; i < arrayElemets.length; i++) {
System.out.println(arrayElemets[i]);
}
}
}
int[] arrayElemets = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 0; i < arrayElemets.length; i++) {
System.out.println(arrayElemets[i]);
}
}
}
// with Iterator
import java.util.ArrayList;
import java.util.Iterator;
Collection Interface
the root interface of the Java collections framework
not directly implement methods
↓
implemented via its sub-interfaces!
Collection Interface
the root interface of the Java collections framework
not directly implement methods
↓
implemented via its sub-interfaces! 7→ List, set, and Queue
List : is add and remove elements like an array.
Set : is to store elelements in different sets similar to the set in mathematics
Queue: is interface used to store and access elements in FIFO, LIFO and etc.
Collection has methods to perform on Objects:
add() - inserts the specified element to the collection
size() - returns the size of the collection
remove() - removes the specified element from the collection
iterator() - returns an iterator to access elements of the collection.
addAll() - adds all the elements of a specified collection to the collection
removeAll() - removes all the elements of the specified collection from the
collection
clear() - removes all the elements of the collection
List Interface
is collection that allows us to store and access elements sequentially.
is it possible to create an Object for the List ?
List Interface
is collection that allows us to store and access elements sequentially.
is it possible to create an Object for the List ? NO!
To use its functionality 7→ ArrayList, LinkedList,Vector and Stack
Additional Methods:
get(): helps to randomly access elements from lists
set(): changes elements of lists
toArray() : converts a list into an array
contains(): returns true if a list contains specified element
Example: Create a List of temprature of 3 countires 10,2, 37,7, 45,6:
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
while (iterate.hasNext()) {
System.out.println(iterate.next());
}
}
}
By: Mule Ta – – Advanced Programming 23
CH01: Functional Programming
Linked List:
Linked List is a sequence of links which contains items.
Each link contains a connection to another link.
Queue
The Queue interface provides the functionality of the queue data structure
Queue in Java follows a FIFO approach.
Each basic method exists in two forms:
one throws an exception if the operation fails
The other returns a special value.
ArrayDeque, LinkedList, Priority queue implements Queue interface
Syntax:
import java.util.ArrayDeque
import java.util.LinkedList
import java.util.PriorityQueue
Queue<T> arrayDeque = new ArrayDeque<>();
Queue<T> linkedList = new LinkedList<>();
Queue<T> priorityQueue = new PriorityQueue<>();
Method:
boolean add(Object): Inserts the specified element into the queue and
returns true if it is a success.
Methods...
boolean offer(object):
Object remove(): Retrieves and removes the head of the queue.
Object poll(): Retrieves and removes the head of the queue, or returns null
if the queue is empty
Object element(): Retrieves, but does not remove the head of the queue.
Object peek(): Retrieves, but does not remove the head of this queue, or
returns null if the queue is empty.
import java.util.Queue;
import java.util.LinkedList;
System.out.println(queue);
System.out.println("The default element peeked: " + lqueue.peek());
System.out.println("The second element peeked: " + lqueue.remove());
System.out.println(lqueue);
By: Mule Ta – – Advanced Programming 26
}
CH01: Functional Programming
The ArrayDeque class provides implementations for all the methods present in
Queue and Deque interface.
How to use:
import java.util.ArrayDeque;
Deque<T> dequeObj = new ArrayDeque<>();
Methods Description
add() inserts the specified element at the end of the array deque
addFirst() inserts the specified element at the beginning of the array deque
addLast() inserts the specified at the end of the array deque (equivalent to add())
offer() inserts the specified element at the end of the array deque
offerFirst() ...
offerLast() ...
Methods Description
add() inserts the specified element at the end of the array deque
addFirst() inserts the specified element at the beginning of the array deque
addLast() inserts the specified at the end of the array deque (equivalent to add())
offer() inserts the specified element at the end of the array deque
offerFirst() ...
offerLast() ...
7→ Accessing elements
Methods Description
getFirst() returns the first element of the array deque
getLast() returns the last element of the array deque
peek() returns the first element of the array deque
peekLast() returns the last element of the array deque
Methods Description
add() inserts the specified element at the end of the array deque
addFirst() inserts the specified element at the beginning of the array deque
addLast() inserts the specified at the end of the array deque (equivalent to add())
offer() inserts the specified element at the end of the array deque
offerFirst() ...
offerLast() ...
7→ Accessing elements
Methods Description
getFirst() returns the first element of the array deque
getLast() returns the last element of the array deque
peek() returns the first element of the array deque
peekLast() returns the last element of the array deque
7→ Removing elements
Methods Description
remove() returns and removes an element from the first element of the array deque
remove(element) returns and removes the specified element from the head of the array deque
removeFirst() returns and removes the first element from the array deque (equivalent to remove())
removeLast() returns and removes the last element from the array deque
Set
is interface of the JCF provides the features of the mathematical set
Sets cannot contain duplicate elements.
HashSet, LinkedHashSet, EnumSet, TreeSet 7→ Implements interfaces set
inherits method, classes, interface of Collection in JCF
How to Use:
import java.util.Set;
import java.util.Hashset;
import java.util.LinkedHashSet;
import java.util.EnumSet;
import java.util.TreeSet;
Operation in set
Recap Union, Intersection and Subset
↓ achieved via
Methods Description
returns an iterator that can be used to access elements
iterator()
of the set sequentially
adds all the elements of the specified collection to the
addAll()
set 7→ Union
retains all the elements in the set that are also present
retainAll()
in another specified set 7→ Intersection
returns true if the set contains all the elements of the
containsAll()
specified collection 7→ Subset
Hashset
Provides the functionalities of the hash table data structure.
Creating HashSet:
import java.util.HashSet;
HashSet<T> hashset_object = new HashSet<>();
TreeSet
Provides the functionalities of the hash tree data structure.
Creating HashSet:
import java.util.TreeSet;
TreeSet<T> treeSet = new TreeSet<>();
provides various methods to navigate over the elements of the tree set.
Navigation methods : first() and last() element of the set
Mythds:
ceiling() 7→ returns the lowest element among those elements that are greater
than the specified element
floor() 7→ returns the greates element among those elements that are less than
the specified element
higher() 7→ returns the lowest element among those elements that are greater
than the specified element.
lower() 7→ Returns the greatest element among those elements that are less than
the specified element.
By: Mule Ta – – Advanced Programming 31
CH01: Functional Programming
any methods?
Lambda Expressions
Foundation for Functional Interfaces!
Reduce the code length and code complexity to a greater extent.
Same with function ?
Lambda Expressions
Foundation for Functional Interfaces!
Reduce the code length and code complexity to a greater extent.
Same with function ? Yes but its Anonymous Function
Why Lambda?
Lambda converts the code segment into an argument
No need to instantiate a class to create
Its like an Object
Syntax:
() -> { //function body }
(parameter) -> { //function body }
(parameter1, parameter2) -> { //function body }
7→ Example:
// conventional Function definations
public string advancedProgramming(){
() -> System.out.println("Lambda Expressions")
System.out.println("Lambda Expressions")
}
Functional Interface
is an interface that only has one abstract method.
also contain default and static methods which do have an implementation.
Two kind:
User-defined Functional Interface
Builtin Functional Interface
syntax:
@FunctionalInterface
public interface InterfaceName {
public void functionName(); //abstract method
static void functionName(){ // static method
// implementation
}
dafault void defaultMethodName(){ // default method
// implementation
}
}
▶ Functional Composition
is technique to combine multiple functions into a single function which uses the
combined functions internally.
Two Type of Composition:
Predicate Composition
and(): used to combine two other Predicate functions
or(): method is used to combine a Predicate instance with another, to compose
a third Predicate instance.
Functional Composition
compose():
andThen():
Example:
Create username checks whether the given username %a and f%; (Using
two Predicates Function)
Object Serialization!
is the process of converting the Object into a Byte Stream,
Byte Stream can be stored, shared, and used by Java machines to
reconstruct the objects.
Why?
Why?
Why?
Syntax:
import java.io.Serializable;
public class ClassName implements Serializable {
// data members
// member function implmentation
}
// Main function
public class MainClassName {
public static void main(String[] args) {
ClassName objectName = new ClassName()
try{
FileOutputStream foutObject = new FileOutputStream("filenmae.txt");
ObjectOutputStream outputObject = new ObjectOutputStream(foutObject);
outputObject.writeObject(objectName);
outputObject.close();
foutObject();
}
catch(IOException exception){
exception.printStackTrace();
}
}
}
By: Mule Ta – – Advanced Programming 41
CH01: Functional Programming
Deserialization:
is other face of Serialization.
is the process of converting the Byte Stream into Object,
Syntax:
import java.io.Serializable;
public class ClassName implements Serializable {
// data members
// member function implmentation
}
// Main function
public class MainClassName {
public static void main(String[] args) {
ClassName objectName = new ClassName()
try{
ObjectInputStream outputIn = new ObjectInputStream(new FileOutputStream("
ClassName e3=(ClassName)outputIn.readObject();
outputIn.close();
}
catch(IOException exception){
exception.printStackTrace();
}
}
By: Mule Ta –
} – Advanced Programming 42
Done!
Question!