Posts

Showing posts from 2025

Expression Evaluator in java

       It is a concept which deals with parsing , operator precedence, parenthesis handling.  Let us implement this in java using Stack concept. As everyone knows, Stack is a data structure with Last in First Out concept. Consider the following statement. 8 + 5 * (4 - 3) Where (4-3) -parenthesis +,*,-   deals with Operator precedence. Parsing is done using stacks for expression evaluation. Java Implementation: There are three methods(‘eval()’,’isOperator()’ and   ‘applyOp()’). The ‘eval()’ function separates value and operator separately. Based on the expression, operator precedence and parenthesis, it executes the expression. ‘isOperator()’ checks it is a operator or not. ‘applyOp()’ is for operator execution. ‘main()’ deals with creating objects for the class and call the function ‘eval()’ to get the output. Program: import java.util.*; import java.util.Stack; public class ExprEvaluator {     publi...

Custom class loader in java

       When you want to load class into the Java Virtual Machine(JVM), Custom Class Loader is used. Why Custom Class Loader? When your class has some specific needs as follows.. Isolate modules Plugins Control class Non- standard sources(Databases, Encrypted Files) Types of Class Loaders: There are 4 types of class loaders. Here is the list. Bootstrap : This Class Loader loads the core Java classes. It uses Internal JVM resources. Platform : This type uses the platform-specific classes and modules. It Loads Java module system. System : This class loader deals with application’s classes. It includes the class path. Custom : When you want to load user defined classes like plugin, this concept is used. Let us create an example…. First, the built – in package is included. Next, the function findClass() is overridden. public class MyClassLoaderEg extends ClassLoader {     @Override ...

Multithreaded File Downloader in java

               Multiple small chunks of a large file are downloaded here. this program explores the concurrency, file input output operations and how it transferred. Let us explore the steps. Steps: First, initialize the URL. The content length is retrieved. Split the download task. By the use of Fixed Thread Pool, the parallel downloads are done.   Finally, the chunks are written. Built-in Packages used for this process: ExecutorService ,HttpURLConnection and RandomAccessFile. Program implementation: import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MThreadedDownloader {     private static final int T_COUNT = 3;     private final String fURL;     private final String opFile;     public MThreadedDownloader(String fURL, String...

ORM(Object Relational Mapping) implementation in java

               A programming technique that interacts the relational database for developers. That is called ORM(Object Relational Mapping). Actually, it is like a bridge between front end and back end code. Here, front end is your code. Back end is your relational database.   Let us create a Customer database for this project. Java implementation: Part1: Customer Class creation public class Customer {     private int c_id;     private String c_name;     private String c_email;     // let us code the constructor     public Customer() {}     public Customer(int c_id, String c_name, String c_email) {         this.c_id = c_id;         this.c_name = c_name;         this.c_email = c_email;     }   ...

Java implementation of Producer - Consumer Problem using Blocking Queue

             Producer creates data and make it into a buffer. A consumer who always take the data from the buffer. Let us consider a textile manufacturer. He/she is the producer. Customer is the consumer. Problem: When the producer creates more products or no products. When a consumer doesn’t take the product. So the solution is implemented in java as Blocking Queue. This queue provides the solution for this problem. It is available in java.util.concurrent package. Let us create the code. Java implementation: // Import the built in package import java.util.concurrent.*; //class producer declaration and function ‘run()’ defintion class PCP_Producer implements Runnable {     private BlockingQueue<Integer> queue1;     public PCP_Producer(BlockingQueue<Integer> q) {         this.queue1 = q;     }     @Override ...

Data Structures -An Introduction with Basic Examples

  Let’s consider a social media application. You create an account with your basic details and receive many follow requests, which you accept. Your follower’s list is then organized like a data structure . Data structures are used to store and manage data in an organized and efficient way. Everyday Examples: Think of a bookshelf, a wardrobe, or a parking area in a mall—each one organizes different types of items systematically. Definition: A data structure is a way of organizing, processing, and retrieving data effectively. How are data structures classified? They are broadly categorized into two types: 1. Linear Data Structures Here, data is stored sequentially and accessed one by one. Array: A basic linear data structure where elements are stored in contiguous memory locations. Linked List: Composed of nodes that are connected using pointers or links. Stack: Follows the Last-In, First-Out (LIFO) principle. Queue: ...

Java implementation of MVC Pattern

A Design pattern which separates the data logic, UI (User Interface) and processing. Here, data logic deals with Model. UI is for View. The processing can be done by Controller. This design pattern is useful for Web Application Architecture. Model-View-Controller (MVC) separates data logic (Model), UI (View), and processing (Controller). The technical implementation is given below. Java implementation: This program implements four classes namely MVC_Model, MVC_View, MVC_Controller and MVCEg. ‘Class MVC_Model’ : It creates and holds the data. Member function :get_Data() Member variable : m_data. ‘Class M_View’: It displays the data to user. Member function: displayIt( String msg). where ‘msg’ is a member variable as String. ‘Class M_Controller’: This process the data. It initialises the values to above two classes. Member function: ‘updateItView()’. Here, this function gets the data from MVC_Model and use the MVC_View function displayIt(). ‘Class MVCE...

Java implementation of Strategy Pattern

       This design pattern deals with Dynamic Behavior Selection. Without modifying the code, the pattern swaps the behavior at run time. Technical implementation: This implementation starts with creating interface. ‘interface’ PaymentMethod is developed with a member function payIt().payIt() has a member variable ‘amount’. Two classes “CCPayment” and “BankPayment” are derived from interface PaymentMethod. “payIt() method displays the amount paid message to the user. PaymentContext is a class for developing this design pattern. A private member variable’ meth’ is created for PaymentMethod interface. ‘setMethod()’ initialises the variable ‘meth’. ‘processIt()’ calls the function ‘payIt()’ with ‘amount’ value. Finally, a sample class with main () function is written. An object is created for PaymentContext. First,setMethod is initialised with CCPayment object. It calls the payIt() functions with amount value. Again,the setMethod is initialised with BankPayment ob...

Adapter Pattern implementation in java

               It is a structural pattern which organizes the classes and objects. Adapter Pattern is one of its types. This pattern makes the different interfaces to run together. Java implementation:               It connects the incompatible interfaces to work together. Let us create two interfaces. One is MPlayer (A media player). It has a play () method. Another interface is AMediaPlayer. It also has playMP4() method. A class (MP3Player) is created for MPlayer interface. ‘play()’ method is used to play Mp3 file. In the same way, another class(MP4Player) is created for AMediaPlayer interface. The ‘playMP4()’ definition is written. Adapter class is created with method. It connects both of the interfaces. A public class ‘MPPlay’ is developed with main() class. Objects are newly created for both classes. ‘play()’ and ‘playMP4()’ functions are called to display the result. ...

Factory Design Pattern in java

              It is a design pattern which creates objects at run time. It makes the user to develop objects. Even, it does not specify the exact class. Program implementation: Here, an interface Fruit is created with findColor() member function. Two more classes are created using this interface. One is for Apple class and another one is guava. A new class “FruitBasket” is developed with a member function getFruit(). The main class “FruitEg” is written with main() function. ‘findColor()’ – This function displays the colour of the fruit. ‘getFruit()’ – it gets the type of fruit. Using a if loop, check for fruit type and create the new object. ‘main()’ – this function creates the object for fruit class and call the findColor(). Program: interface Shape {     void findCorners(); }   class Square implements Shape {     public void findCorners() {         Sys...

Design Patterns Introduction and Singleton Pattern implementation in Java

What is Design pattern?      It gives you a structured approach to solve a common problem in software development. Basically, it gives you a reusable code which is easy to maintain. It is flexible too. Let us categorize the design patterns. Creational patterns Structural patterns Behavioral patterns Each of the pattern has its own features given as follows Creational Patterns:               As the name suggests, it deals with object creation. For example, Singleton, Builder and Factory design patterns. Structural Patterns:               These design patterns deal with classes.The main purpose is to structure the class and relationships are made. Eg: Adapter, Decorator and Proxy. Behavioral Patterns:               This design pattern is made for communicatio...

The producer- consumer Problem in java

     It is an evergreen synchronization problem in operating system. It has two entries. They are ·        Producer ·        Consumer Producer is the one, which generates the item and adds it to a shared region. Consumer is the one which retrieves the item from the shared region and processes it. So, synchronization plays a vital role here. To handle this problem, the method “BlockingQueue” is used. Technical implementation:               This implementation creates two threads Producer and Consumer as Runnable. A private final BlockingQueue as integer. In the constructor, initialise the value. In the run() method,insert the value for queue and make the thread to sleep for some time. Similarly, do the above steps for Consumer thread also. Finally, in the main() function, create objects for queue. Using these objects, start the Thread...

Counting connected components in a graph

               It is a basic problem in graph. A graph contains nodes and edges. Nodes can be a vertices and edges connecting the nodes. Here, connected component means every node is reached from any other node. The traversal of node may be DFS(Depth First Search) or BFS(Breadth First Search) according to it. Let us count the counted component in graph. Program implementation: This program gets the number of nodes and edges as input. It uses DFS(Depth first Search) to traverse the nodes. ‘g_dfs()’ used to traverse the graph. It gets the nodes, adjacent list and a Boolean value ‘g_visited’ to perform the traversal. It sets the ‘g_visited’ value when it reaches the node. ‘countIt()’ -This function gets the number of nodes and edges as input. It checks the ‘g_visited’   value for entire graph. Find the edges visited. Finally, gives you the count. ‘main()’ – this function assigns the input value as number of nodes and edges. It calls the ...