Skip to main content

Java

Implementing Diffie–Hellman Key Exchange Algorithm in Java

In modern cryptography, secure key exchange is essential for private communication over public channels. One of the earliest practical implementations of public key exchange is the Diffie-Hellman Key Exchange. This post will guide you through the concept and a simple Java implementation to illustrate how two parties can securely generate a shared secret key. What is Diffie-Hellman Key Exchange? The Diffie-Hellman Key Exchange (developed in 1976) is a method that allows two users to exchange cryptographic keys over a public channel securely. The beauty of this method is that both parties can compute the same secret key independently, which can then be used for encrypting future messages. Here's how it works conceptually: Two parties (say, Alice and Bob) agree on a large prime number p and a primitive root g of that number. Alice chooses a private key x, computes R1 = g^x mod p and sends R1 to Bob. Bob chooses a private key y, computes R2 = g^y mod p and sends R2 to Alice. Alice computes the secret key as k1 = R2^x mod p. Bob computes the secret key as k2 = R1^y mod p. Both k1 and k2 will be the same due to properties of modular arithmetic:g^(xy) mod p = g^(yx) mod p.

Implementation of Inverse Kinematics in Java

In this post, we implement Inverse Kinematics (IK) for a SCARA robot in Java. While Direct Kinematics computes the end-effector position from known joint angles, Inverse Kinematics does the opposite: given a desired end-effector position (W vector), it computes the joint angles required to reach that position. This is the fundamental calculation needed for robot path planning and motion control. What is Inverse Kinematics? For a SCARA robot with 4 joints, the IK solution uses geometric and trigonometric relationships derived from the robot's DH parameters. The inputs are: W — End-effector position vector: [wx, wy, wz, w4, w5, w6] d — Link offsets: [d1, d2, d3, d4] a — Link lengths: [a1, a2, a3, a4] The output is the set of joint angles q1, q2, q3, q4 that position the end-effector at the specified W coordinates.

Implementing HCTM in Java

In this post, we implement the Homogeneous Coordinate Transformation Matrix (HCTM) in Java. HCTM is a fundamental concept in robotics and computer graphics for representing rotation and translation of a coordinate frame in 3D space. Using a 4×4 matrix allows both rotation and translation to be combined into a single matrix multiplication, making it efficient to apply multiple transformations in sequence. What is HCTM? A Homogeneous Coordinate Transformation Matrix extends a standard 3D rotation matrix to 4×4 by appending a translation vector and a homogeneous row [0 0 0 1]. This program implements three rotations (Yaw around X-axis, Pitch around Y-axis, Roll around Z-axis) and one translation. Given a rotation angle (in degrees) and a 4-element coordinate frame vector, the program computes the transformed frame using matrix–vector multiplication.

Implementing Direct Kinematics in Java

In this post, we implement Direct Kinematics (Forward Kinematics) for a SCARA robot in Java using the Denavit-Hartenberg (DH) convention. Direct kinematics determines the position and orientation of the robot's end-effector (tool tip) given the joint angles and link parameters. This is the fundamental calculation in robot arm control and simulation. What is Direct Kinematics? For a multi-joint robot arm, each link between joints is described by four DH parameters: θ (theta) — Joint angle (rotation about Z-axis) d — Link offset (translation along Z-axis) a — Link length (translation along X-axis) α (alpha) — Link twist (rotation about X-axis) For each link, a 4×4 General Link Coordinate Transformation Matrix (GLCTM) is computed from its DH parameters. The final end-effector pose is obtained by multiplying all the link matrices together: T04 = T01 × T12 × T23 × T34.

Implementing DFS in Java

In this post, we implement Depth-First Search (DFS) in Java using an adjacency matrix and recursion. DFS is a fundamental graph traversal algorithm that starts at a source node and explores as far as possible along each branch before backtracking. It is used in cycle detection, topological sorting, maze solving, and AI game tree searches. What is DFS? DFS explores a graph by diving deep into one branch before exploring others. In this implementation, we use a recursive approach: starting from node 1, we immediately follow the first unvisited neighbour, then the first unvisited neighbour of that node, and so on. Only when we reach a dead end do we backtrack and try the next neighbour. The graph is represented as an adjacency matrix. An array q[] is used both as the visited set and to record the traversal order. The variable qpos tracks how many nodes have been visited so far.

Implementing BFS in Java

In this post, we implement Breadth-First Search (BFS) in Java using an adjacency matrix. BFS is a fundamental graph traversal algorithm that visits all nodes level by level, starting from a source node and exploring all its direct neighbours before moving deeper into the graph. It is widely used in shortest path problems, network analysis, and AI search strategies. What is BFS? BFS explores a graph by using a queue data structure. It starts at a chosen node (node 1 in our implementation), adds it to the queue, and then repeatedly dequeues a node, visits all its unvisited neighbours, and enqueues them. This guarantees that all nodes at depth d are visited before any node at depth d+1. The graph in this implementation is represented as an adjacency matrix — a 2D array where m[i][j] = 1 means there is an edge between node i+1 and node j+1, and 0 means no edge.

Inheritance Example in Java

Learn Java inheritance with a clear, hands-on example. See how a Box class extends Rectangle using the extends keyword and super() constructor chaining, with a full code walkthrough, sample output, and FAQs.

Polymorphism Example in Java

Learn Java polymorphism with clear examples of both method overloading (compile-time) and method overriding (runtime). Includes step-by-step walkthroughs, sample output, and FAQs covering dynamic dispatch, @Override, and static method hiding.

Finite State Machine: Check Whether a Number is Divisible by 3

Learn how to build a Finite State Machine (FSM) in Java that checks whether a decimal number is divisible by 3. Includes the theory behind the digit-sum rule, a 3-state FSM implementation, state-trace output, sample output, and a detailed explanation.