Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

CH 01 Advanced Programming

The document discusses an advanced programming course. The course aims to help students analyze programming languages, explore object-oriented programming concepts, develop graphical user interfaces, demonstrate network and database connectivity techniques, and solve real-world programming problems by the end. It provides details on the course content, which includes chapters on functional programming, systems programming, persistence, network programming, threads, GUIs, and web programming.

Uploaded by

Biruk Mesfin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

CH 01 Advanced Programming

The document discusses an advanced programming course. The course aims to help students analyze programming languages, explore object-oriented programming concepts, develop graphical user interfaces, demonstrate network and database connectivity techniques, and solve real-world programming problems by the end. It provides details on the course content, which includes chapters on functional programming, systems programming, persistence, network programming, threads, GUIs, and web programming.

Uploaded by

Biruk Mesfin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

Advanced Programming

Course Code: SWEG3108


Target Group: Software Engineering
Year : III
Semister: II

By: Muleta Taye T.


Email: muleta.taye@aastu.edu.et
March 31, 2023
Course Objective

▶ What is Expected at the end ?

By: Mule Ta – – Advanced Programming 2


Course Objective

▶ What is Expected at the end ?


ˆ Analyze how the programming language works

By: Mule Ta – – Advanced Programming 2


Course Objective

▶ What is Expected at the end ?


ˆ Analyze how the programming language works
ˆ Explore the basic concepts of object-oriented programming.

By: Mule Ta – – Advanced Programming 2


Course Objective

▶ What is Expected at the end ?


ˆ Analyze how the programming language works
ˆ Explore the basic concepts of object-oriented programming.
ˆ Develop Graphical User Interface.
ˆ Demonstrate the concepts of Network Fundamentals and Socket Programming,
Remote Method, Thread and HTTP services.

By: Mule Ta – – Advanced Programming 2


Course Objective

▶ What is Expected at the end ?


ˆ Analyze how the programming language works
ˆ Explore the basic concepts of object-oriented programming.
ˆ Develop Graphical User Interface.
ˆ Demonstrate the concepts of Network Fundamentals and Socket Programming,
Remote Method, Thread and HTTP services.
ˆ Explain database connectivity techniques and demonstrate the ability to write
applications that solves a real-world programming problem.

By: Mule Ta – – Advanced Programming 2


Assesment and References

Assesment Type Weight


First Test 15%
Quiz 5%
Project 20%
Assignment 10%
Final Exam 50%

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

By: Mule Ta – – Advanced Programming 3


Project Title

▶ Group of 6:

1. Customer Relationship Management


2. Human Resource Management
3. Supply Chain Management
4. Financial Management
5. Inventory Management
6. Sales order management
7. Marketing Resource Management

By: Mule Ta – – Advanced Programming 4


Course Content ...

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

By: Mule Ta – – Advanced Programming 5


CHAPTER 01
Functional Programming
ˆ Packages
ˆ Collections
ˆ Lambda Expressions
ˆ Object Serialization
ˆ Declarative Programming

By: Mule Ta – – Advanced Programming 6


CH01: Functional Programming

By: Mule Ta – – Advanced Programming 7


CH01: Functional Programming

”COMPLEXITY!”
→ ↓
Any Solution?

By: Mule Ta – – Advanced Programming 7


CH01: Functional Programming

7→ Programming Paradigm?

By: Mule Ta – – Advanced Programming 8


CH01: Functional Programming

7→ Programming Paradigm?

ˆ The preferred approach to programming that a language supports


ˆ is a style, or “way,” of programming.
ˆ It doen’t refer to a specific language, but rather it refers to the way you
program.
ˆ Common Paradigms
ˆ Rule-Based.
ˆ Structured.
ˆ Procedural.
ˆ Object-Oriented.
ˆ Functional.

By: Mule Ta – – Advanced Programming 8


CH01: Functional Programming

By: Mule Ta – – Advanced Programming 9


CH01: Functional Programming

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

By: Mule Ta – – Advanced Programming 10


CH01: Functional Programming

ˆ Functional Programming
ˆ is declarative pgm Paradigm
ˆ Alike other, is problem solving Paradigm 7→ How ?

By: Mule Ta – – Advanced Programming 11


CH01: Functional Programming

ˆ 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 ?

By: Mule Ta – – Advanced Programming 11


CH01: Functional Programming

ˆ 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

By: Mule Ta – – Advanced Programming 11


CH01: Functional Programming

ˆ 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

ˆ Functions as first class objects



• In the functional programming paradigm, functions are first class objects in
the language 7→ create an instance of a function

public class AdvancedProgramming {


public void lecture(){ //statement };
public void labSession(){ //statement }
public class void main(String args [ ]){
AdvancedProgramming ap = new Advancedprogramming()
}
}

By: Mule Ta – – Advanced Programming 11


CH01: Functional Programming

ˆ 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

ˆ Functions as first class objects



• In the functional programming paradigm, functions are first class objects in
the language 7→ create an instance of a function

public class AdvancedProgramming {


public void lecture(){ //statement };
// javacscript FFCO
public void labSession(){ //statement }
const advancedProgramming = (parameter1, parameter2) => {
public class void main(String args [ ]){
// Statements
AdvancedProgramming ap = new Advancedprogramming()
}
}
}

By: Mule Ta – – Advanced Programming 11


CH01: Functional Programming

ˆ Functions as first class objects ...


ˆ When do we say function First-Class-Function
ˆ If it can be passed as a parameter to a function.
ˆ If it can be returned from a function.
ˆ If it can be assigned to a variable and then can be used later.
ˆ Pure Function
ˆ A function is a pure function if:
ˆ The execution of the function has no side effects.
ˆ The return value of the function depends only on the input parameters passed to
the function.

// Example pure function


public class PureFunctionDemo{
public static void main(String args []){
public int compareNumbers(int firstNumber, float secondNumber) {
if(firstNumber > (int)secondNumber){
System.out.prinln("Highest is", firstNumber)
}
else {
System.out.prinln("Highest is", secondNumber)
}
}
}
}

By: Mule Ta – – Advanced Programming 12


CH01: Functional Programming

ˆ Higher Order Functions


ˆ A function is a higher order function if and only if:
ˆ The function takes one or more functions as parameters.
ˆ The function returns another function as result.

ˆ Any Rules/protocol then ? 7→

By: Mule Ta – – Advanced Programming 13


CH01: Functional Programming

ˆ Higher Order Functions


ˆ A function is a higher order function if and only if:
ˆ The function takes one or more functions as parameters.
ˆ The function returns another function as result.

ˆ Any Rules/protocol then ? 7→ YEAH!


ˆ NO STATE: 7→ no state external to the function

By: Mule Ta – – Advanced Programming 13


CH01: Functional Programming

ˆ Higher Order Functions


ˆ A function is a higher order function if and only if:
ˆ The function takes one or more functions as parameters.
ˆ The function returns another function as result.

ˆ Any Rules/protocol then ? 7→ YEAH!


ˆ NO STATE: 7→ no state external to the function

By: Mule Ta – – Advanced Programming 13


CH01: Functional Programming

ˆ Higher Order Functions


ˆ A function is a higher order function if and only if:
ˆ The function takes one or more functions as parameters.
ˆ The function returns another function as result.

ˆ Any Rules/protocol then ? 7→ YEAH!


ˆ NO STATE: 7→ no state external to the function
ˆ NO SIDE EFFECT : 7→ a function cannot change any state outside of the
function

By: Mule Ta – – Advanced Programming 13


CH01: Functional Programming

ˆ Higher Order Functions


ˆ A function is a higher order function if and only if:
ˆ The function takes one or more functions as parameters.
ˆ The function returns another function as result.

ˆ Any Rules/protocol then ? 7→ YEAH!


ˆ NO STATE: 7→ no state external to the function
ˆ NO SIDE EFFECT : 7→ a function cannot change any state outside of the
function
ˆ IMMUTABLE VARIABLES: 7→ Immutable variables makes it easier to avoid
side effects.

By: Mule Ta – – Advanced Programming 13


CH01: Functional Programming

ˆ Higher Order Functions


ˆ A function is a higher order function if and only if:
ˆ The function takes one or more functions as parameters.
ˆ The function returns another function as result.

ˆ Any Rules/protocol then ? 7→ YEAH!


ˆ NO STATE: 7→ no state external to the function
ˆ NO SIDE EFFECT : 7→ a function cannot change any state outside of the
function
ˆ IMMUTABLE VARIABLES: 7→ Immutable variables makes it easier to avoid
side effects.
ˆ RECURSION OVER LOOPING: 7→ Recursion uses function calls to achieve
looping,

By: Mule Ta – – Advanced Programming 13


CH01: Functional Programming

What are Packages ?


ˆ are container that groups related types
ˆ such as classes, interfaces, enumerations, and annotations
ˆ Key reasons:
ˆ avoids name space conflict
ˆ To write a better maintainable code
ˆ 2 Families:
1. Built-in packages
ˆ A Library of prewritten classes
2. User-defined Packages

By: Mule Ta – – Advanced Programming 14


CH01: Functional Programming

ˆ The Java library is divided into packages and classes


ˆ To use a class or a package from the library, you need to use the import keyword:
ˆ Syntax for importing a single class
import package.name.Class
ˆ Syntax Import the whole package
import package.name.*
ˆ Exmple: create a Package named section
ˆ define an interface with an abstract method called Comparer
ˆ Use custom class that implement the interface, recieve the numbers from
the user

By: Mule Ta – – Advanced Programming 15


CH01: Functional Programming

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.

Click to see more java built-in packages

By: Mule Ta – – Advanced Programming 16


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();


public void authenticate();
}

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();


public void authenticate();
}

import authentication.Login;
import authentication.authenticate;
import java.util.Scanner;
public class LoginAuthentication implements Authentication{

public String Login() {


// Logic for receiving information

}
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();


public void authenticate();
}

public class Login{


public static void main(String args []){
import authentication.Login;
import authentication.authenticate;
LoginAuthentication login = new LoginAuthen
import java.util.Scanner;
login.Login();
public class LoginAuthentication implements Authentication{
login.authenticate();

}
public String Login() {
}
// Logic for receiving information

}
public void authenticate(){
// Logic for authenication

}
}
By: Mule Ta – – Advanced Programming 17
CH01: Functional Programming

ˆ Recap Data Structure and Algorithms!


By: Mule Ta – – Advanced Programming 18


CH01: Functional Programming

ˆ Recap Data Structure and Algorithms!



• Representations → List, ArrayList, vectors, Queue, Tree, LinkedList, etc

By: Mule Ta – – Advanced Programming 18


CH01: Functional Programming

ˆ Recap Data Structure and Algorithms!



• Representations → List, ArrayList, vectors, Queue, Tree, LinkedList, etc

• Operatins → Searching, Sorting, Deletion, Inserting, Updating,
Manipulating, etc

By: Mule Ta – – Advanced Programming 18


CH01: Functional Programming

ˆ Recap Data Structure and Algorithms!



• Representations → List, ArrayList, vectors, Queue, Tree, LinkedList, etc

• Operatins → Searching, Sorting, Deletion, Inserting, Updating,
Manipulating, etc

How all these could be handled ?

By: Mule Ta – – Advanced Programming 18


CH01: Functional Programming

ˆ Recap Data Structure and Algorithms!



• Representations → List, ArrayList, vectors, Queue, Tree, LinkedList, etc

• Operatins → Searching, Sorting, Deletion, Inserting, Updating,
Manipulating, etc

How all these could be handled ? 7→ COLLECTION FRAMEWORKS!
Collections in Java
ˆ Collections refer to a collection of individual objects that are represented as a
single unit.
ˆ Collection framework provides an architecture to store and manipulate a group of
objects.
ˆ standardizes the way in which groups of objects are handled by your programs
ˆ The framework has :
1. interfaces
2. classes
3. algorithms

By: Mule Ta – – Advanced Programming 18


CH01: Functional Programming

Java Collection Framework Hierarchy

By: Mule Ta – – Advanced Programming 19


CH01: Functional Programming

ˆ Any Benefits of Using Collections ?

By: Mule Ta – – Advanced Programming 20


CH01: Functional Programming

ˆ Any Benefits of Using Collections ? YUP!


↓ Check ...

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

// creating iterator object to access the method


Iterator<T> iterator_object = element.iterator();

By: Mule Ta – – Advanced Programming 20


CH01: Functional Programming

// Traditional Array Manipulation


public class ArrayListTest {
public static void main(String args[]) {

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]);
}
}
}

By: Mule Ta – – Advanced Programming 21


CH01: Functional Programming

// Traditional Array Manipulation


public class ArrayListTest {
public static void main(String args[]) {

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;

public class ArrayListTest {


public static void main(String args[]) {
ArrayList<Integer> arrayElemets = new ArrayList<Integer>();
arrayElemets.add(1);
arrayElemets.add(2);
arrayElemets.add(3);
arrayElemets.add(4);
arrayElemets.add(5);

Iterator<Integer> iterate = arrayElemets.iterator();


while (iterate.hasNext()) {
System.out.println(iterate.next());
}
}

By: Mule Ta – – Advanced Programming 21


CH01: Functional Programming

Collection Interface
ˆ the root interface of the Java collections framework
ˆ not directly implement methods

implemented via its sub-interfaces!

By: Mule Ta – – Advanced Programming 22


CH01: Functional Programming

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

By: Mule Ta – – Advanced Programming 22


CH01: Functional Programming

ˆ List Interface
ˆ is collection that allows us to store and access elements sequentially.
ˆ is it possible to create an Object for the List ?

By: Mule Ta – – Advanced Programming 23


CH01: Functional Programming

ˆ 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;

public class ArrayListTest {


public static void main(String args[]) {
List<Float> temprature = new ArrayList<>();
Iterator<Float> iterate = temprature.iterator();
temprature.add(10.2);
temprature.add(37.7);
temprature.add(45.6);

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.

ˆ Think Signle and Double Linked List!


ˆ Syntax:
import java.util.LinkedList;
LinkedList object = new LinkedList()
ˆ Method:
ˆ boolean add(Object obj)
ˆ void add(int index, Object element):
ˆ void addFirst(Object obj):
ˆ void addLast(Object obj):
ˆ int size():
ˆ int indexOf(Object element): indx first Occurance of the elemet
ˆ int lastIndexOf(Object element): index of the last occurance of element
ˆ Example: Create list of 5 elements which stored on non-contigious memory
location
1 assign the value with add() method
2 receive the value from the user console

By: Mule Ta – – Advanced Programming 24


CH01: Functional Programming

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.

By: Mule Ta – – Advanced Programming 25


CH01: Functional Programming

ˆ 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;

public class QueueTest {


public static void main(String[] args) {
Queue<Integer> lqueue = new LinkedList<>(); // implementing using Linked List
Queue<Integer> pqueue = new PriorityQueue<>(); // implementing using PriorityQueu
lqueue.offer(1);
lqueue.offer(6);
lqueue.offer(10);
lqueue.offer(3);

System.out.println("The default element peeked: " + lqueue.peek());


System.out.println("The second element peeked: " + lqueue.remove());

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

7→ Is Possible to +/- element Both Side in Queue?



via a deque interface

By: Mule Ta – – Advanced Programming 27


CH01: Functional Programming

7→ Is Possible to +/- element Both Side in Queue?



via a deque interface

ˆ 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<>();

By: Mule Ta – – Advanced Programming 27


CH01: Functional Programming

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() ...

By: Mule Ta – – Advanced Programming 28


CH01: Functional Programming

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

By: Mule Ta – – Advanced Programming 28


CH01: Functional Programming

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

By: Mule Ta – – Advanced Programming 28


CH01: Functional Programming

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;

Set<T> hashSet = new Hashset<>();


Set<T> linkedSet = new LinkedHashSet<>();
Set<T> enumSet = new EnumSet<>();
Set<T> treeSet = new TreeSet<>();

By: Mule Ta – – Advanced Programming 29


CH01: Functional Programming

ˆ 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

ˆ Example: Create a two sets A and B


ˆ Add five Integer values for the sets
ˆ Receive the size or capacity of the sets from the user and its values
ˆ Display the Union, intersection andif A is subset of B.

By: Mule Ta – – Advanced Programming 30


CH01: Functional Programming

ˆ 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?

By: Mule Ta – – Advanced Programming 32


CH01: Functional Programming

ˆ any methods? Ofcourse review ....


ˆ LinkedHashSet
ˆ Provides functionalities of both the hashtable and the linked list data
structure.
ˆ The linked list defines the order in which elements are inserted in hash tables
ˆ How to Use:
import java.util.LinkedHashSet;
LinkedHashSet<T> treeSet = new LinkedHashSet<>();
ˆ Mythds:
ˆ Insert ops: 7→
add(), addAll()
ˆ Accessing ops: 7→ use the iterator() method
ˆ Delete ops: 7→
remove(), removeAll()
ˆ Union ops: 7→
addAll()
ˆ Intersection ops: 7→
retainAll()

By: Mule Ta – – Advanced Programming 32


CH01: Functional Programming

Lambda Expressions
ˆ Foundation for Functional Interfaces!
ˆ Reduce the code length and code complexity to a greater extent.
ˆ Same with function ?

By: Mule Ta – – Advanced Programming 33


CH01: Functional Programming

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")
}

By: Mule Ta – – Advanced Programming 33


CH01: Functional Programming

ˆ 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
}
}

ˆ Java functional interface can be implemented by a Lambda Expression


ˆ Example: Write a java program to find the area of the following Diagram.

By: Mule Ta – – Advanced Programming 34


CH01: Functional Programming

ˆ Functional Interfaces ...


ˆ are key for functional Programming!
ˆ In Java is also called Single Abstract Method (SAM) interface
ˆ Builtin Functional Interface
ˆ Runnable 7→ has run()
ˆ Comparable 7→ has compareTo()
ˆ ActionListener 7→ actionPerformed()
ˆ Callable 7→ call()
ˆ Types of Functional interfaces
1. Function
2. Consumer
3. Predicate
4. Supplier

By: Mule Ta – – Advanced Programming 35


CH01: Functional Programming

Function Functional Interfaces


ˆ receives a single argument, processes it, and returns a value
import java.util.function.Function;

public interface Function<T, R>{


R apply(T t);
}
// T is the type of the input argument
// R is the return type of the function
// apply is abstract method

Supplier Functional Interfaces


ˆ much like a functional interface.
ˆ It represents a function which does not take in any argument but produces a
value of type
import java.util.function.Supplier;
public interface Supplier<T>{
T get();
}

By: Mule Ta – – Advanced Programming 36


CH01: Functional Programming

Consumers Functional Interfaces


ˆ is a functional interface that accepts an argument and returns no result.
ˆ interface contains two methods:
ˆ accept()
ˆ andThen()
import java.util.function.Consumer;
public interface Consumer<T>{
void accept(T t);
}

Predicate Functional Interfaces


ˆ is a functional interface takes a single argument and returns a boolean value.
import java.util.function.Predicate;
public interface Predicate<T>{
boolean test(T t);
}

By: Mule Ta – – Advanced Programming 37


CH01: Functional Programming

▶ 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)

By: Mule Ta – – Advanced Programming 38


CH01: Functional Programming

▶ Do you think object destroyed after execution?


By: Mule Ta – – Advanced Programming 39


CH01: Functional Programming

▶ Do you think object destroyed after execution?



Object are recreated to use it again
| {z }

Sounds Crazy!, Any Solution

By: Mule Ta – – Advanced Programming 39


CH01: Functional Programming

▶ Do you think object destroyed after execution?



Object are recreated to use it again
| {z }

Sounds Crazy!, Any Solution 7→ Ofcourse

By: Mule Ta – – Advanced Programming 39


CH01: Functional Programming

▶ Do you think object destroyed after execution?



Object are recreated to use it again
| {z }

Sounds Crazy!, Any Solution 7→ Ofcourse

Object Serialization!

ˆ 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.

By: Mule Ta – – Advanced Programming 39


CH01: Functional Programming

ˆ Why?

ˆ Object is serializable iff:


ˆ a class or any of its parent classes implement either of the interfaces from
java.io
ˆ Serializable interface
• defines no members
• It is simply used to indicate that a class may be serialized
• writeObject() method of ObjectOutputStream 7→ Serialization
• readObject() method of ObjectOutputStream 7→ Deserialization

By: Mule Ta – – Advanced Programming 40


CH01: Functional Programming

ˆ Why?

ˆ Object is serializable iff:


ˆ a class or any of its parent classes implement either of the interfaces from
java.io
ˆ Serializable interface
• defines no members
• It is simply used to indicate that a class may be serialized
• writeObject() method of ObjectOutputStream 7→ Serialization
• readObject() method of ObjectOutputStream 7→ Deserialization
ˆ Externalizable subinterface.
• The programmer may need to have control over these processes.

By: Mule Ta – – Advanced Programming 40


CH01: Functional Programming

ˆ Why?

ˆ Object is serializable iff:


ˆ a class or any of its parent classes implement either of the interfaces from
java.io
ˆ Serializable interface
• defines no members
• It is simply used to indicate that a class may be serialized
• writeObject() method of ObjectOutputStream 7→ Serialization
• readObject() method of ObjectOutputStream 7→ Deserialization
ˆ Externalizable subinterface.
• The programmer may need to have control over these processes.

By: Mule Ta – – Advanced Programming 40


CH01: Functional Programming

ˆ 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!

By: Mule Ta – – Advanced Programming 43

You might also like